MCPcopy Create free account
hub / github.com/OGSR/OGSR-Engine / GetScriptText

Method GetScriptText

ogsr_engine/xrGame/PhraseScript.cpp:243–267  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

241}
242
243LPCSTR CPhraseScript::GetScriptText(LPCSTR str_to_translate, const CGameObject* pSpeakerGO1, const CGameObject* pSpeakerGO2, LPCSTR dialog_id, LPCSTR phrase_id)
244{
245 if (!m_sScriptTextFunc.size())
246 return str_to_translate;
247
248 xr_string ScriptTextString(m_sScriptTextFunc.c_str()); //-V808
249 if (luabind::functor<const char*> lua_function; ai().script_engine().functor(ScriptTextString.c_str(), lua_function)) // Обычный функтор
250 return lua_function(pSpeakerGO1->lua_game_object(), pSpeakerGO2->lua_game_object(), dialog_id, phrase_id);
251 else
252 { // Функтор с аргументами
253 luabind::functor<luabind::object> loadstring_functor;
254 ai().script_engine().functor("loadstring", loadstring_functor);
255 ScriptTextString = "return " + ScriptTextString;
256 luabind::object ret_obj = loadstring_functor(ScriptTextString.c_str()); // Создаём функцию из строки через loadstring
257 auto ret_func = luabind::object_cast<luabind::functor<const char*>>(ret_obj); // Первое возвращённое loadstring значение должно быть функцией
258 ASSERT_FMT_DBG(ret_func, "Loadstring returns nil for code: %s", ScriptTextString.c_str()); // Если это не функция, значит loadstring вернул nil и что-то пошло не так
259 // Вызываем созданную функцию и передаём ей дефолтные аргументы. Они прилетят после аргументов, прописанных явно, если например сделать так:
260 // <script_text>my_script.test_func(123, true, nil, ...)</script_text>
261 // А если не указать '...' - дефолтные аргументы не будут переданы в функцию.
262 if (ret_func)
263 return ret_func(pSpeakerGO1->lua_game_object(), pSpeakerGO2->lua_game_object(), dialog_id, phrase_id);
264 }
265
266 return "";
267}

Callers 1

GetPhraseTextMethod · 0.80

Calls 5

lua_functionFunction · 0.85
functorMethod · 0.80
lua_game_objectMethod · 0.80
sizeMethod · 0.45
c_strMethod · 0.45

Tested by

no test coverage detected