MCPcopy Create free account
hub / github.com/defold/defold / Script_Get

Function Script_Get

engine/gameobject/src/gameobject/gameobject_script.cpp:620–692  ·  view source on GitHub ↗

gets a named property of the specified game object or component * * @name go.get * @param url [type:string|hash|url] url of the game object or component having the property * @param property [type:string|hash] id of the property to retrieve * @param [options] [type:table] optional options table * - index [type:number] index into array property (1 based) * - key [

Source from the content-addressed store, hash-verified

618 * go.get("#my_component", "some_property", { keys = { "root", "child_node" } })
619 */
620 int Script_Get(lua_State* L)
621 {
622 ScriptInstance* i = ScriptInstance_Check(L);
623 Instance* instance = i->m_Instance;
624 dmMessage::URL sender;
625 dmScript::GetURL(L, &sender);
626 dmMessage::URL target;
627 dmScript::ResolveURL(L, 1, &target, &sender);
628 DM_HASH_REVERSE_MEM(hash_ctx, 256);
629 if (target.m_Socket != dmGameObject::GetMessageSocket(i->m_Instance->m_Collection->m_HCollection))
630 {
631 return luaL_error(L, "go.get can only access instances within the same collection.");
632 }
633 dmhash_t property_id = 0;
634 if (lua_isstring(L, 2))
635 {
636 property_id = dmHashString64(lua_tostring(L, 2));
637 }
638 else
639 {
640 property_id = dmScript::CheckHash(L, 2);
641 }
642 dmGameObject::HInstance target_instance = dmGameObject::GetInstanceFromIdentifier(dmGameObject::GetCollection(instance), target.m_Path);
643 if (target_instance == 0)
644 {
645 return luaL_error(L, "Could not find any instance with id '%s'.", dmHashReverseSafe64Alloc(&hash_ctx, target.m_Path));
646 }
647
648 LuaToPropertyOptionsResult options_result = {};
649
650 // Options table
651 if (lua_gettop(L) > 2)
652 {
653 dmGameObject::LuaToPropertyOptions(L, 3, &options_result);
654 }
655
656 dmGameObject::PropertyDesc property_desc;
657 dmGameObject::PropertyResult result = dmGameObject::GetProperty(target_instance, target.m_Fragment, property_id, options_result.m_Options, property_desc);
658 if (result == dmGameObject::PROPERTY_RESULT_OK && !options_result.m_IndexRequested && property_desc.m_ValueType == dmGameObject::PROP_VALUE_ARRAY && property_desc.m_ArrayLength > 1)
659 {
660 lua_newtable(L);
661
662 dmGameObject::PropertyOptions get_arrayed_property_opts;
663 dmGameObject::AddPropertyOptionsIndex(&get_arrayed_property_opts, 0);
664
665 // We already have the first value, so no need to get it again.
666 // But we do need to check the result, we could still get errors even if the result is OK
667 int handle_go_get_result = CheckGetPropertyResult(L, "go", result, property_desc, property_id, target, get_arrayed_property_opts, options_result.m_IndexRequested, options_result.m_KeysRequested);
668 if (handle_go_get_result != 1)
669 {
670 return handle_go_get_result;
671 }
672 lua_rawseti(L, -2, 1);
673
674 // Get the rest of the array elements and check each result individually
675 for (int i = 1; i < property_desc.m_ArrayLength; ++i)
676 {
677 SetPropertyOptionsByIndex(&get_arrayed_property_opts, 0, i);

Callers

nothing calls this directly

Calls 15

ScriptInstance_CheckFunction · 0.85
GetMessageSocketFunction · 0.85
luaL_errorFunction · 0.85
lua_isstringFunction · 0.85
CheckHashFunction · 0.85
GetCollectionFunction · 0.85
dmHashReverseSafe64AllocFunction · 0.85
lua_gettopFunction · 0.85
LuaToPropertyOptionsFunction · 0.85
AddPropertyOptionsIndexFunction · 0.85
CheckGetPropertyResultFunction · 0.85

Tested by

no test coverage detected