Scripts / Get entity ID by name

Author
Message
Zigi
14
Years of Service
User Offline
Joined: 5th Jul 2009
Location:
Posted: 23rd Dec 2018 13:17 Edited at: 23rd Dec 2018 13:28
Hi.

I was searching in global.lua but can not find any commands to get ID of an entity by it name.
I'm trying to create a switch to toggle the visibility of other entities on and off. In FPSC we were able to use the "If Used" property with switches to toggle lights on and off and open close doors but it doesn't seem to work in GameGuru.

I can get switches to work if I enter the actual ID number of the entity but the problem is, I would like to trigger multiple instances of the same entity and also when I select an entity I don't see the ID displayed in IDE anywhere not even in the properties panel. So in order to find ID values, I need to guess. For example, I added a light to the scene and it got ID = 2 then I added an other light and it got ID = 5. So the values are not even incrementing in order which makes it impossible to work with ID numbers even with a for loop.

Could anyone please give me some idea, tips how to go about making a switch that toggles multiple instances of an entity in GameGuru with Lua or how to get ID by name or pick entities by name?

Thanks.
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 23rd Dec 2018 14:41
At the top of the script create a list:

mylist = {}

Instead of <scriptname>.init( e ) you use <scriptname>.init_name( e, name )

Then add the entity to a list with:
mylist[ name ] = e

Then you can access the entity id by name.

Note that you will have to make sure all antities have a unique name doing it this way.

Alternatively have the list keyed on the entity id and store names in it then create a function that returns a list of all entities with that name if you need to handle multiple entities with the same name.



Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 23rd Dec 2018 14:43
Btw, check out the utillib.lua library, it has a bunch of useful functions which may make your life easier.
Been there, done that, got all the T-Shirts!
PM
Zigi
14
Years of Service
User Offline
Joined: 5th Jul 2009
Location:
Posted: 23rd Dec 2018 22:37
Thank you for the tips. utillib.lua has some useful functions but I was hoping GG might have a Lua command hidden somewhere that return the ID or array of ID's by name.

If I understand the solution you offer I still need to fill up the list manually with the ID numbers and then loop through the list but here comes the problem that GG IDE don't seem to display ID numbers and I need to guess the numbers.

Is there a way to get ID values of selected entities in the IDE at least so then I can feed them in to a list manually as you suggesting?
Or I really need to go and find ID values with trial and error for all entities? I hope it is not the case, it sounds painful

Thanks.
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 23rd Dec 2018 23:48
The _init_name function gives you the name of the entity as well as the id, you just store them in a list for later use.

Look at the script attached to see how it can be used.

All entities that you need to get the ids of by name will have to have a script attached so if you are using many different scripts just make the list global and you can access it from any script. Always start global variables with g_ so that 1) it reminds you that they are indeed global, 2) there is less chance of accidentally updating a global variable instead of a local one and 3) the GG save game mechanism should save them for you.

To use a global list properly do the following in each script that needs to access it:

g_listname = g_listname or {}

What this does is either use an existing declared list or (if it has not yet been declared) declare a new one.
This way it doesn't matter which script gets called first the global variable will be declared and used safely.

If all accesses to the variable are in one script attached to many entities make the variable local as local variables are much quicker to access though.
Been there, done that, got all the T-Shirts!

Attachments

Login to view attachments
PM
Zigi
14
Years of Service
User Offline
Joined: 5th Jul 2009
Location:
Posted: 24th Dec 2018 00:05
Oh I see. Thanks a lot for the explanation.
I got it working now


PM

Login to post a reply

Server time is: 2024-04-23 23:23:39
Your offset time is: 2024-04-23 23:23:39