Scripting: Tag Names and ID

Due to Skylines Game Objects acting like self-contained programs we have a way for object entities and the game systems to communicate with each other. This is done by the use of unique identifiers for locating specific entities. Each entity in the scene has a designated ID and is assigned in a sequential order. This way we can notify an entity that something has happened and/or send some data to the other entity. By using this we can create and control some very complex relationships between Game Objects enabling all sorts of Game Mechanic dynamics.

Identifiers – Tag or ID?

We can get the ID of an entity in the scene by using its property panel, but this can be an issue. If for whatsoever reason this entity id is changed, due to a scene reordering itself after another object has been deleted. To get around this we advise the use of “Tag Names”.

Every entity in the scene has a unique Tag property, no two entities can have the same tag name.

NOTE: You will notice the term “entity” not “object”, as all objects are entities but not all entities are objects. E.g. lights, empty nodes, meshes are entity types. Objects are generally more than an entity, a combination of entity, actions, scripts and modules.

So to know the identity of another object we use its unique Tag Name; see next Image:

Know yourself – Parent Object

Other than a scene script, most scripts are attached to an entity. This entity's ID is passed into any attached script in the form of an event argument. It’s common to see the variable “obj” in Skylines scripts. We use this as a global variable, only to this script, containing this parent entity's ID. Now we have this ID we can send it to other objects if requested by them.

The ID is passed into the script by the onInit(objID) event function, so you would see:

Obj  = -1; -- Create our global variable to store the objects id.
function onInit(objID) -- Event called after you press the play button, this scripts game entry point.
    obj = objID;    -- The id of this object at the time of pressing the play button or end game starting up.
end

Now we can use the variable “obj” to represent this entities ID anywhere in this script.

Special Event calls

To call communicate with another object we can use a special command to call a function in a target object script, and if required pass some data between these objects. To do so we have a few options (targetID is the id of the object you are calling).

  • Global Call – Sends out to all objects with a script. – sky.callGlobalFn(obj,value);
  • Call to a specified Object. - entity.callPublicFn(obj,targetID,value);
  • Call to specific objects function - entity.callFn(obj,targetID,“anyFunction”,arg1);

Ok I hear you say “You said don’t use ID’s, use tags instead!” well I did. What we need to do to obtain the “targetID” is use the target objects entity tag property and a neat Lua command to get the id from this tag:

targetID = entity.getIDFromTag("Target_Object_Tag");

More on this inter object communication on the next page →