Jump to content
  • entries
    10
  • comments
    31
  • views
    7,557

LUA CPP "how to" 2/7


Charrua

1,776 views

 Share

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?

 

Next, Back

  • Upvote 1
 Share

0 Comments


Recommended Comments

There are no comments to display.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...