MCPcopy Create free account
hub / github.com/PolygonTek/BlueshiftEngine / ChangeScript

Method ChangeScript

Source/Runtime/Private/Components/ComScript.cpp:132–222  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

130}
131
132void ComScript::ChangeScript(const Guid &scriptGuid) {
133#if WITH_EDITOR
134 // Disconnect with previously connected script asset.
135 if (scriptAsset) {
136 scriptAsset->Disconnect(&Asset::SIG_Reloaded, this);
137 scriptAsset = nullptr;
138 }
139#endif
140
141 if (!sandboxName.IsEmpty()) {
142 state->SetToNil(sandboxName.c_str());
143 //state->ForceGC();
144 }
145
146 if (sandbox.IsValid()) {
147 sandbox = LuaCpp::Selector();
148 }
149
150 hasError = false;
151
152 ClearFunctionMap();
153
154 this->scriptGuid = scriptGuid;
155
156 if (scriptGuid.IsZero()) {
157 sandboxName = "";
158 return;
159 }
160
161 // Sandbox name is same as component GUID in string.
162 sandboxName = GetGuid().ToString();
163
164 const Str scriptPath = resourceGuidMapper.Get(scriptGuid);
165 char *text;
166 size_t size = fileSystem.LoadFile(scriptPath, true, (void **)&text);
167 if (!text) {
168 sandboxName = "";
169 BE_WARNLOG("ComScript::ChangeScript: Failed to load script '%s'\n", scriptPath.c_str());
170 return;
171 }
172
173 if (!ParsePropertyNames(scriptPath, text, propertyNames)) {
174 propertyNames.Clear();
175 }
176
177 // Load a script with sandboxed on current Lua state.
178 if (!state->LoadBuffer(scriptPath.c_str(), text, size, sandboxName)) {
179 hasError = true;
180 }
181
182 fileSystem.FreeFile(text);
183
184 if (!hasError) {
185 // Get the state of current loaded script.
186 sandbox = (*state)[sandboxName];
187
188 // Run this script.
189 state->Run();

Callers

nothing calls this directly

Calls 15

SetToNilMethod · 0.80
LoadBufferMethod · 0.80
FreeFileMethod · 0.80
RunMethod · 0.80
IsTableMethod · 0.80
ConnectMethod · 0.80
SelectorClass · 0.50
DisconnectMethod · 0.45
IsEmptyMethod · 0.45
c_strMethod · 0.45
IsValidMethod · 0.45
IsZeroMethod · 0.45

Tested by

no test coverage detected