LUA CPP "how to" 2/7
1) How to get objects created on the editor from Cpp
Easy as it seems, getting an editor created object from cpp code is done basically looping on the entities collection, but there are some things that is good to know.
First one simple way of doing it:
System::Print("start scanning...."); for (auto iter = world->entities.begin(); iter != world->entities.end(); iter++) { Entity* entity = *iter; System::Print(entity->GetKeyValue("name")); if (entity->GetKeyValue("name") == "findMe"){ System::Print(" found entity findMe !!!"); if (entity->script != NULL){ foundEntity = entity; } } }
It's supposed that you have a map with an entity named "findMe"
if you like you can use: SetKeyValue to give some "custom" attributes to your entities and then
instead of testing by a already existing key like name you may test by any custom key of your's
if (entity->GetKeyValue("room_number") == "8"){ System::Print(" entity of room 8"+entity->GetKeyValue("name")); if (entity->script != NULL){ foundEntity = entity; } }
entities collection has all of them, cameras, lights, models, etc but in some particular situations searching for specific class is necessary: cameras, light has special methods and to work properly it's better to use the correct object.
for example to get a camera created on the editor you may:
//now look for the editor camera: for (Camera* e : world->cameras) { System::Print(e->GetKeyValue("name")); if (e->GetKeyValue("name") == "EditorCamera"){ camera = e; System::Print("found editor camera"); } }
note "camera" is declared on app.h as: Camera* camera; so it's a global already defined
that's it, so simple, he?
- 1
0 Comments
Recommended Comments
There are no comments to display.