MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / GameLoop

Method GameLoop

src/script/script_instance.cpp:185–273  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

183}
184
185void ScriptInstance::GameLoop()
186{
187 ScriptObject::ActiveInstance active(*this);
188
189 if (this->IsDead()) return;
190 if (this->engine->HasScriptCrashed()) {
191 /* The script crashed during saving, kill it here. */
192 this->Died();
193 return;
194 }
195 if (this->is_paused) return;
196 this->controller->ticks++;
197
198 if (this->suspend < -1) this->suspend++; // Multiplayer suspend, increase up to -1.
199 if (this->suspend < 0) return; // Multiplayer suspend, wait for Continue().
200 if (--this->suspend > 0) return; // Singleplayer suspend, decrease to 0.
201
202 _current_company = ScriptObject::GetCompany();
203
204 /* If there is a callback to call, call that first */
205 if (this->callback != nullptr) {
206 if (this->is_save_data_on_stack) {
207 sq_poptop(this->engine->GetVM());
208 this->is_save_data_on_stack = false;
209 }
210 try {
211 this->callback(*this);
212 } catch (Script_Suspend &e) {
213 this->suspend = e.GetSuspendTime();
214 this->callback = e.GetSuspendCallback();
215
216 return;
217 }
218 }
219
220 this->suspend = 0;
221 this->callback = nullptr;
222
223 if (!this->is_started) {
224 try {
225 {
226 ScriptObject::DisableDoCommandScope disabler{};
227 /* Run the constructor if it exists. Don't allow any DoCommands in it. */
228 if (this->engine->MethodExists(*this->instance, "constructor")) {
229 if (!this->engine->CallMethod(*this->instance, "constructor", MAX_CONSTRUCTOR_OPS) || this->engine->IsSuspended()) {
230 if (this->engine->IsSuspended()) ScriptLog::Error("This script took too long to initialize. Script is not started.");
231 this->Died();
232 return;
233 }
234 }
235 if (!this->CallLoad() || this->engine->IsSuspended()) {
236 if (this->engine->IsSuspended()) ScriptLog::Error("This script took too long in the Load function. Script is not started.");
237 this->Died();
238 return;
239 }
240 }
241 /* Start the script by calling Start() */
242 if (!this->engine->CallMethod(*this->instance, "Start", _settings_game.script.script_max_opcode_till_suspend) || !this->engine->IsSuspended()) this->Died();

Callers

nothing calls this directly

Calls 15

IsDeadMethod · 0.95
DiedMethod · 0.95
CallLoadMethod · 0.95
sq_poptopFunction · 0.85
HasScriptCrashedMethod · 0.80
GetVMMethod · 0.80
GetSuspendTimeMethod · 0.80
GetSuspendCallbackMethod · 0.80
MethodExistsMethod · 0.80
IsSuspendedMethod · 0.80
ThrowErrorMethod · 0.80
GetErrorMessageMethod · 0.80

Tested by

no test coverage detected