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

Method Initialize

src/script/script_instance.cpp:58–98  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

56}
57
58void ScriptInstance::Initialize(const std::string &main_script, const std::string &instance_name, CompanyID company)
59{
60 ScriptObject::ActiveInstance active(*this);
61
62 this->controller = std::make_unique<ScriptController>(company);
63
64 /* Register the API functions and classes */
65 this->engine->SetGlobalPointer(this->engine.get());
66 this->RegisterAPI();
67 if (this->IsDead()) {
68 /* Failed to register API; a message has already been logged. */
69 return;
70 }
71
72 try {
73 ScriptObject::DisableDoCommandScope disabler{};
74 /* Load and execute the script for this script */
75 if (main_script == "%_dummy") {
76 this->LoadDummyScript();
77 } else if (!this->engine->LoadScript(main_script) || this->engine->IsSuspended()) {
78 if (this->engine->IsSuspended()) ScriptLog::Error("This script took too long to load script. AI is not started.");
79 this->Died();
80 return;
81 }
82
83 /* Create the main-class */
84 this->instance = std::make_unique<SQObject>();
85 if (!this->engine->CreateClassInstance(instance_name, this->controller.get(), this->instance.get())) {
86 /* If CreateClassInstance has returned false instance has not been
87 * registered with squirrel, so avoid trying to Release it by clearing it now */
88 this->instance.reset();
89 this->Died();
90 return;
91 }
92 } catch (Script_FatalError &e) {
93 this->is_dead = true;
94 this->engine->ThrowError(e.GetErrorMessage());
95 this->engine->ResumeError();
96 this->Died();
97 }
98}
99
100void ScriptInstance::RegisterAPI()
101{

Callers

nothing calls this directly

Calls 13

RegisterAPIMethod · 0.95
IsDeadMethod · 0.95
DiedMethod · 0.95
SetGlobalPointerMethod · 0.80
LoadScriptMethod · 0.80
IsSuspendedMethod · 0.80
ThrowErrorMethod · 0.80
GetErrorMessageMethod · 0.80
ResumeErrorMethod · 0.80
getMethod · 0.45
LoadDummyScriptMethod · 0.45
CreateClassInstanceMethod · 0.45

Tested by

no test coverage detected