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

Function GetComponentFromLua

engine/gameobject/src/gameobject/gameobject_script.cpp:426–487  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

424 }
425
426 void GetComponentFromLua(lua_State* L, int index, HCollection collection, const char* component_ext, dmGameObject::HComponent* out_component, dmMessage::URL* url, dmGameObject::HComponentWorld* out_world)
427 {
428 dmMessage::URL sender;
429 if (dmScript::GetURL(L, &sender))
430 {
431 dmMessage::URL receiver;
432 dmScript::ResolveURL(L, index, &receiver, &sender);
433 if (sender.m_Socket != receiver.m_Socket || sender.m_Socket != dmGameObject::GetMessageSocket(collection))
434 {
435 luaL_error(L, "function called can only access instances within the same collection.");
436 return; // Actually never reached
437 }
438
439 HInstance instance = GetInstanceFromIdentifier(collection, receiver.m_Path);
440 if (!instance)
441 {
442 luaL_error(L, "Instance %s not found", lua_tostring(L, index));
443 return; // Actually never reached
444 }
445
446 dmGameObject::HComponentWorld world;
447 uint32_t component_type_index;
448 dmGameObject::Result result = dmGameObject::GetComponent(instance, receiver.m_Fragment, &component_type_index, out_component, &world);
449 if ((component_ext != 0x0 || *out_component != 0x0) && result != dmGameObject::RESULT_OK)
450 {
451 char buffer[128];
452 luaL_error(L, "The component could not be found: '%s'", dmScript::UrlToString(&receiver, buffer, sizeof(buffer)));
453 return; // Actually never reached
454 }
455
456 if (out_world != 0)
457 {
458 *out_world = world;
459 }
460
461 if (component_ext != 0x0)
462 {
463 HResourceType resource_type;
464 dmResource::Result resource_res = dmResource::GetTypeFromExtension(dmGameObject::GetFactory(instance->m_Collection->m_HCollection), component_ext, &resource_type);
465 if (resource_res != dmResource::RESULT_OK)
466 {
467 luaL_error(L, "Component type '%s' not found", component_ext);
468 return; // Actually never reached
469 }
470 ComponentType* type = &dmGameObject::GetRegister(instance->m_Collection->m_HCollection)->m_ComponentTypes[component_type_index];
471 if (type->m_ResourceType != resource_type)
472 {
473 luaL_error(L, "Component expected to be of type '%s' but was '%s'", component_ext, type->m_Name);
474 return; // Actually never reached
475 }
476 }
477 if (url)
478 {
479 *url = receiver;
480 }
481 }
482 else
483 {

Callers 2

LuaTestCompTypeFunction · 0.50

Calls 10

GetMessageSocketFunction · 0.85
luaL_errorFunction · 0.85
GetComponentFunction · 0.85
UrlToStringFunction · 0.85
GetTypeFromExtensionFunction · 0.85
GetRegisterFunction · 0.85
ResolveURLFunction · 0.70
GetFactoryFunction · 0.70
GetURLFunction · 0.50

Tested by 2

LuaTestCompTypeFunction · 0.40