Scripts / Get entity names

Author
Message
kehagiat
10
Years of Service
User Offline
Joined: 16th Jul 2013
Location:
Posted: 15th Jun 2015 23:18
I know this has been discussed previously but I cannot find the post.

Anyway, what I want is something like this: iterate through the table g_Entity and for each value of e get the name of g_Entity[e].

How to do this?
Whereof one cannot speak thereof one must be silent
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 16th Jun 2015 00:08
https://forum.game-guru.com/thread/207801?page=7#msg2515459

hth

3com
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 8.1 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics

PM
kehagiat
10
Years of Service
User Offline
Joined: 16th Jul 2013
Location:
Posted: 16th Jun 2015 09:05
Thank you for the pointer. It is an interesting piece of code.

However, after some more thought I chose another solution. To illustrate, here are two scripts.
default0.lua is:


default1.lua is

So place several DYNAMIC entities in your map and in the properties panel / AI System record change the script to
1. default0.lua for a single entity
2. default1.lua for the remaining entities

Then run the game for a few seconds and exit. In the file out.txt (it appears in the top Game Guru folder) you will find a list of index and name for all dynamic entities in the map.

Why will it not work for dynamic entities ?
Whereof one cannot speak thereof one must be silent
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 16th Jun 2015 10:24
I expect it does but they're being recorded as masters too, try checking entilist is nil in the first script before you update to "master"(might want to remove the check for cycle being 1 in the second script too)
life\'s one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, geforce gtx460, directx 11
kehagiat
10
Years of Service
User Offline
Joined: 16th Jul 2013
Location:
Posted: 16th Jun 2015 11:14
@smallg

I will check if "entilist is nil" but I see no reason that dynamic entities should be " recorded as masters". What I did was place some dynamic entities and give them the default1.lua script. So shouldn't they get EntiList[e]="Slave"..e ?

Regarding the cycle==1 check. I tried placing the EntiList[e]="Slave"..e in default1_init(e) (similarly for the Master) and it didn't work. I got a nil field error.
My guess is that things are initialized in some order such that if I try to do EntiList[e]="Slave"..e in default1_init, Guru doesn't yet know the EntiList has been created. However it has found EntiList by the tim ethe _main scripts start executing.

Note that this is true even when I make Master be the first entity (i.e., e=1). So executing init scripts does not happen in the order entities have been numbered? Or maybe all Lua scripts are sent to some stack and executed later?
Whereof one cannot speak thereof one must be silent
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 16th Jun 2015 12:00
1- After testing your code with a few dynamic boxes, this is what I get:

1 Master1
2 Slave2
3 Slave3
4 Slave4
5 Slave5

2- Maybe that's my poor English, but I do not quite understand what the purpose of your question, in other words, why to get the slave name, if the entity is called, say, box1, box2, Box3 ?

Ago some time I've write this code to get the names and ids:



Also I've writed some macro to get the same:

https://forum.game-guru.com/thread/209929

3com




Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 8.1 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics

PM
!nullptr
Forum Support
9
Years of Service
User Offline
Joined: 27th Mar 2015
Location: Australia
Posted: 16th Jun 2015 12:18 Edited at: 16th Jun 2015 12:24
This little piece creates an array, indexed by e with value "name". I use it to get the entity number and name of anything with the script attached. I have trees named "Bluegum" and "Redgum" and I can tell you what entity# it is (and where it is) in about 10 lines of code.

Easy enough to traverse sequentially by n iterations (use table.count) or even sort and binary search the array to get both e and/or corresponding name for entity.

Unless I misread your question?

AKA SisterMatic (Steam)
Development/ Gaming Rigs
Sys 1: i7-4770 (3.5)/16Gb/128 SSD/3Tb/970gtx/2 x 23, 1 x 27 LCD - Sys 2: i7/8Gb/670gtx/1.5Tb/1 x 23 LCD - Sys 3: Amd Quad/8Gb/645gtx/1Tb/30" LCD
PM
!nullptr
Forum Support
9
Years of Service
User Offline
Joined: 27th Mar 2015
Location: Australia
Posted: 16th Jun 2015 12:32
btw, if you had entities outside "300" range the script would never run for them.

If you want all entities on your map to be included in your pickup they need to be set Always Active. That's probably why it worked for 3Com if he had all the boxes close to him.

Just a thought.
AKA SisterMatic (Steam)
Development/ Gaming Rigs
Sys 1: i7-4770 (3.5)/16Gb/128 SSD/3Tb/970gtx/2 x 23, 1 x 27 LCD - Sys 2: i7/8Gb/670gtx/1.5Tb/1 x 23 LCD - Sys 3: Amd Quad/8Gb/645gtx/1Tb/30" LCD
PM
kehagiat
10
Years of Service
User Offline
Joined: 16th Jul 2013
Location:
Posted: 16th Jun 2015 13:18
@!nullptr

This is exactly what I wanted. But where does the variable "name" come from? I tried it without specifying any value (I was hoping it gets the value given in the Properties panel) and didn't work?

Also: it still does not work with static entities.

The thing I learned however is that you can define the table treeArr = {} outside the functions and it will be available in the initialization phase.

@3com

The point of these questions is to give each entity a name which can be used in later code to access entities by name. Ideally this should work with the entity number e but I have heard that Guru can on its own change entity numbers at a later stage.

These names can be obtained in at at least two ways.
1. Programmatically, by a line of code in the entity's init script.
2. Using the name assigned in the properties panel. I still have not been able to do this, but actually I like 1 better.
Whereof one cannot speak thereof one must be silent
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 16th Jun 2015 14:38
Quote: "That's probably why it worked for 3Com if he had all the boxes close to him."

Not exactly, Firts lines talking about plrinzone, comes because I attached script to trigger zone for testing pourpose.

Quote: "Ideally this should work with the entity number e but I have heard that Guru can on its own change entity numbers at a later stage."

True. for this reazon I wrote that script.

Quote: "These names can be obtained in at at least two ways.
"

There is a 3ª one; take a look to " Guru-MapEditor.log " file. I've wrote script that read all the info from this file, when I need some data.

3com

Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 8.1 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics

PM
kehagiat
10
Years of Service
User Offline
Joined: 16th Jul 2013
Location:
Posted: 16th Jun 2015 15:03
@3com

That's very cool but why can't I find a relevant line in Map-Editor.log?

Oh ... is it the lines
22689322 : Loaded 1:\Buildings\Asylum\Dead End.fpe S:2MB V:0MB (654,292)
22689354 : Loaded 2:\0MyEntities\groundentity.fpe S:0MB V:18MB (653,310)
22689354 : Loaded 3:\0MyEntities\flycam.fpe S:0MB V:0MB (654,310)

??
Whereof one cannot speak thereof one must be silent
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 16th Jun 2015 15:12
Quote: "Oh ... is it the lines"

Yes that's the point.

Quote: "Loaded 1:\Buildings\Asylum\Dead End.fpe S:2MB V:0MB (654,292) "

Meaning:
1 = entity number (id)
Dead End = entity name.

So, write script that extract info from there, if you try my macro you can understand better, what I'm talking about.

3com

Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 8.1 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics

PM
kehagiat
10
Years of Service
User Offline
Joined: 16th Jul 2013
Location:
Posted: 16th Jun 2015 17:43
This is a great straightforward solution. I assume the Map-Editor.log file is generated and available before the Lua commands (and initializations) start executing?
Whereof one cannot speak thereof one must be silent
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 16th Jun 2015 18:00
Quote: "I assume the Map-Editor.log file is generated and available before the Lua commands (and initializations) start executing?"

Right.
You can use regular expressions (RegeX), to take the info you want to, or Patterns.

http://www.lua.org/manual/5.0/manual.html#libraries

3com
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 8.1 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics

PM
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 16th Jun 2015 21:37
your initial question was why doesnt it work with dynamic entities which is what i originally tried to answer, i see you meant static - it wont because scripts aren't active on static so the variable wont get created for them.

3coms way seems cool or another solution would be to simply create the variable slave if it isn't already called master for each entity (checking they exist first) as all dynamics will be "master" and thus all remaining objects must be static
life\'s one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, geforce gtx460, directx 11
!nullptr
Forum Support
9
Years of Service
User Offline
Joined: 27th Mar 2015
Location: Australia
Posted: 16th Jun 2015 22:16 Edited at: 16th Jun 2015 22:16
Quote: "@!nullptr
This is exactly what I wanted. But where does the variable "name" come from? I tried it without specifying any value (I was hoping it gets the value given in the Properties panel) and didn't work?

Also: it still does not work with static entities."

That will pick up "name" from properties so if it didn't work it's either your distance from entity, it's set to static or name field is empty.

See smallg's comment. ie: Script not active on static.

@3com
Sorry mate. I didn't read your code. Only the result. His didn't work. Yours did. I applied short-circuit logic - and failed
AKA SisterMatic (Steam)
Development/ Gaming Rigs
Sys 1: i7-4770 (3.5)/16Gb/128 SSD/3Tb/970gtx/2 x 23, 1 x 27 LCD - Sys 2: i7/8Gb/670gtx/1.5Tb/1 x 23 LCD - Sys 3: Amd Quad/8Gb/645gtx/1Tb/30" LCD
PM
kehagiat
10
Years of Service
User Offline
Joined: 16th Jul 2013
Location:
Posted: 17th Jun 2015 11:28 Edited at: 17th Jun 2015 11:30
To summarize:

I didn't yet run 3com's excel macro, I am still on Office 2003. But I have a question. I set up a map with six barrels, saved ran the game, exited. Looking into Guru-MapEditor.log I see a single line

15678903 : Loaded 1:\Barrels\Barrel (Clean).fpe S:2MB V:0MB (694,338)

It seems like Guru records loading the model file and it only doies this once (even if it uses it for six entities). So how to get numbers for six enties from Guru-MapEditor.log ?

Still on Guru-MapEditor.log , I know regular expressions is THE way to go but it's pretty easy to get the required info with VBA string functions; I suppose that's what 3com does in his macro.

Regarding !nullptr's script, it does work for dynamic entities (so I stand corrected). But now I have a new question. I made a small variation on his code as follows.


It works as it is above. But, it crashes if I uncomment the Prompt(StrengthArr[e]) line . My question is this: if I can access the "name" property, why can't I also access "strength" or other entity properties?

One more thing: I really like !nullptr's solution but I also like mine. The difference between the two is the following: in his solution the user has the burden of giving distinct entity names from the properties panel; in my solution this must be done with Lua coding. I think each of the two solutions can be more appropriate in a different situation.

In conclusion: this has been a nice thread, I learned several new things and I thank you all very much!
Whereof one cannot speak thereof one must be silent
!nullptr
Forum Support
9
Years of Service
User Offline
Joined: 27th Mar 2015
Location: Australia
Posted: 17th Jun 2015 23:29
Your prompt crashed with nil value right? Because there is no "strength" call (that I know of) and nor can you "init" twice either so the array never initiliased.

Let's wind back a bit. There is a neat "trick" I've been playing with using NAME where I use name to simply pass an index (your own "e" so to speak) but the same index can apply to multiple items.

Scenario. A bottle of water with water.lua attached. It is named "1".

I get the "1" via init_name function and then grab the corresponding entity string. (The advantage of hardcoding the string means I have one place to manage the values of ALL water bottles on my map with a name of "1")

eg:

Note: This is my completed code. You need to fill in the bits you need.

I then parse the string and do a breakout of the values I need (a "pop") into individual local vars


You'll note the use of "0" (zero) in the name Bottle0of0Water. This is deliberate to prevent the parse creating 3 vars at the spaces. gsub modifies.


In this coding example the only thing I "carry" around is an array of item indexes and I only query the index and parse as needed. (for display, enviromental changes or to update player values etc.)

This technique can apply to *any* entity that has a name property. I use the exact same technique to have hundreds of trees on a map, each tree type has different shade values. I also use the exact same technique to manage a huge number of inventory items (as this example shows).

With boxes you could use BoxM and BoxS as indexes rather than numbers etc.... BoxM might be strength 10, BoxS might be strength 5 etc....

There's a lot of power at your disposal here. Just keep track of your indexes and manage the values of each index in script. If you want to move, delete, use, blow up, destroy any item, the entity still uses "e" but has a lot more other info as well.

Happy to answer further queries. Note this is just one way to do things - but it's dynamic, very efficient and has very low overheads.
AKA SisterMatic (Steam)
Development/ Gaming Rigs
Sys 1: i7-4770 (3.5)/16Gb/128 SSD/3Tb/970gtx/2 x 23, 1 x 27 LCD - Sys 2: i7/8Gb/670gtx/1.5Tb/1 x 23 LCD - Sys 3: Amd Quad/8Gb/645gtx/1Tb/30" LCD
PM
kehagiat
10
Years of Service
User Offline
Joined: 16th Jul 2013
Location:
Posted: 18th Jun 2015 14:49
Thank you !nullptr for a very interesting response. I will read this carefully and come back with questions and responses tomorrow. But I must ask the BIG question immediately. You can use init_name to GET the value of property "name". But is there a Lua-way to SET the same property ?
Whereof one cannot speak thereof one must be silent
!nullptr
Forum Support
9
Years of Service
User Offline
Joined: 27th Mar 2015
Location: Australia
Posted: 18th Jun 2015 23:57 Edited at: 19th Jun 2015 00:05
Quote: "But I must ask the BIG question immediately. You can use init_name to GET the value of property "name". But is there a Lua-way to SET the same property ?"

The short answer is No.

The reason is obvious. If you're carrying the name around in a variable, just change the variable. If you want the name to be APPLE at runtime you call it APPLE. ie: fruitname[1]=APPLE

If you want you APPLE to become a PEACH then simply change fruitname[1]=PEACH in code.

Next time you run the fruit is still an APPLE - otherwise you'd have one heck of a mess (game logic, not the fruitbowl)
AKA SisterMatic (Steam)
Development/ Gaming Rigs
Sys 1: i7-4770 (3.5)/16Gb/128 SSD/3Tb/970gtx/2 x 23, 1 x 27 LCD - Sys 2: i7/8Gb/670gtx/1.5Tb/1 x 23 LCD - Sys 3: Amd Quad/8Gb/645gtx/1Tb/30" LCD
PM
kehagiat
10
Years of Service
User Offline
Joined: 16th Jul 2013
Location:
Posted: 21st Jun 2015 07:37 Edited at: 21st Jun 2015 07:40
@!nullptr

I looked at your code and if I understand getItemData(itemIDX) correctly, what you are doing is defining YOUR OWN entity properties and store them in a string ; on demand you can extract the properties back from the string.

QUESTION: Why do it this way rather than adding extra fields in the g_entity table (e.g., g_entity[e]["type"]="FOOD", g_entity[e]["taste"]="SALTY" ..., ). If g_entity[e]["type"]="CLOTHE", you will never initialize g_entity[e]["taste"] so you will have g_entity[e]["taste"]=nil.

This is all interesting but my main concerns have been different.

1. Get the "true" name of an entity. This is solved by your previous code trees_init_name(e,name),
2. Do this more generally, i.e., for other "true" properties, such as Strength, Speed, If Used etc. This I still do not know how to do.
3. Even better would be to be able to to read AND write all the properties of the property panel. I know there exist Lua commands for some of these properties, but what I am looking for is a general method.
Whereof one cannot speak thereof one must be silent
!nullptr
Forum Support
9
Years of Service
User Offline
Joined: 27th Mar 2015
Location: Australia
Posted: 21st Jun 2015 09:04
Quote: "QUESTION: Why do it this way rather than adding extra fields in the g_entity table (e.g., g_entity[e]["type"]="FOOD", g_entity[e]["taste"]="SALTY" ..., ). If g_entity[e]["type"]="CLOTHE", you will never initialize g_entity[e]["taste"] so you will have g_entity[e]["taste"]=nil."

I could but then every entity would have those same attributes in the global definition. And tbh, that's exactly what I'm doing anyway - I just don't call it g_Entity.

More to the point, the method I have is a ready made script where I have complete control to my exact requirements and it's quite possibly a lot more efficient because I only carry the index (a number) and only ask what that number is if/when I need it. It's also readily customisable for future projects/demands.

Quote: "1. Get the "true" name of an entity. This is solved by your previous code trees_init_name(e,name),
2. Do this more generally, i.e., for other "true" properties, such as Strength, Speed, If Used etc. This I still do not know how to do.
3. Even better would be to be able to to read AND write all the properties of the property panel. I know there exist Lua commands for some of these properties, but what I am looking for is a general method."

1: Correct.
2: Most of them are accessible using g_entity[e]["xxxx"]. Granted, not all but I suspect they will soon enough.
3: If you can read and set a g_entity value then this is what you have. Again, they're not all there. Time perhaps.

In the interim you need to use your own techniques - which is exactly what I do and will probably continue even if we get "full access" because you will never get all of the values you want anyway - only a generic set.

eg: I also carry "deterioration ratio" in food items now. What if someone wanted color, size, weight, taste, price etc. in every food entity. The properties list becomes infinite so a custom scripted solution will always be required regardless.

It's no different to characters (weight, height, race, allegience, religion etc...) , weapons, rocks, trees and... fruit.

That's the reality of custom scripting and custom requirements unfortunately.
AKA SisterMatic (Steam)
Development/ Gaming Rigs
Sys 1: i7-4770 (3.5)/16Gb/128 SSD/3Tb/970gtx/2 x 23, 1 x 27 LCD - Sys 2: i7/8Gb/670gtx/1.5Tb/1 x 23 LCD - Sys 3: Amd Quad/8Gb/645gtx/1Tb/30" LCD
PM
kehagiat
10
Years of Service
User Offline
Joined: 16th Jul 2013
Location:
Posted: 21st Jun 2015 10:38
We are on the same page. But let me note:

Quote: "That's the reality of custom scripting and custom requirements unfortunately."


I suspect you don't really mean "unfortunate" and you actually get a kick out of it. I know I do.

On the other hand, as repeatedly pointed out in this forum, certain attributes really should available to the scripter by this stage of Guru's evolution (camera control pleaseeee). But, obviously, we have to be very patient.
Whereof one cannot speak thereof one must be silent
!nullptr
Forum Support
9
Years of Service
User Offline
Joined: 27th Mar 2015
Location: Australia
Posted: 21st Jun 2015 11:28
Quote: "I suspect you don't really mean "unfortunate" and you actually get a kick out of it. I know I do. "

Scripting can be fun yes

Quote: "
On the other hand, as repeatedly pointed out in this forum, certain attributes really should available to the scripter by this stage of Guru's evolution"

As I stated earlier, there comes point where the generic solution - which is all that can be provided - is never going to be enough. GG could never hope to provide all solutions for all people, no engine can, no matter how complicated or simple that engine is.

The best we can hope to deal with is in specifics and evolve ourselves by adapting.
AKA SisterMatic (Steam)
Development/ Gaming Rigs
Sys 1: i7-4770 (3.5)/16Gb/128 SSD/3Tb/970gtx/2 x 23, 1 x 27 LCD - Sys 2: i7/8Gb/670gtx/1.5Tb/1 x 23 LCD - Sys 3: Amd Quad/8Gb/645gtx/1Tb/30" LCD
PM

Login to post a reply

Server time is: 2024-05-03 05:17:23
Your offset time is: 2024-05-03 05:17:23