Scripts / Quick entity grabber.

Author
Message
!nullptr
Forum Support
9
Years of Service
User Offline
Joined: 27th Mar 2015
Location: Australia
Posted: 14th Apr 2015 22:18 Edited at: 15th Apr 2015 08:21
My first script post here so if I messed up.....

PROBLEM
I was searching for a way to query the "health" of every mushroom dynamically in a game I created for my little boy. I needed to add up his score based on partial and total mushroom kills that were shot with a raygun. Every new level had a different count and they were added/deleted based on balancing.

After getting the entity code for every mushroom manually for 2 levels and then bugging it when I changed a mushroom I took a 5 minute walk, screamed at the moon, came back and sat back down. The solution was easier than I thought.

(Caveat, if something identical has been posted before please say so. I could not find a better way to do it anywhere so wrote this myself.)

SOLUTION
A dynamic entity getter. Sometimes a weakness can be overcome with a strength. Where GG currently doesn't address entities by name it DOES call each entities assigned script. If you have 20 mushrooms with a "mushrooms.lua" attached then the script is called 20 times.

USAGE
Place as many mushrooms on your map as you want. (Place one down, assign it the "mushroom.lua" script and copy copy). BUT Keep count. Update the value of mushy_count when you're done.

NOTES:
This code will apply to ANY entity with mushroom.lua applied. (Write 1 for trees, rocks. whatever to keep separate track)

Coders will immediately see a huge advantage in this because with dynamic access to the g_entity list you can also x,y, check distance, destroy, spawn, move entities etc.

I'm more than happy of people want to modify/change/recommend better/simpler easier ways or write snippets that use this.

Now. Devs. Entity ->names<- and I will be even happier

Queries? Ask away.


E&OE

EDIT: Note. If you have too many entities the debug dump will probably crash you. Either modify the debug script to smaller fonts / split display OR remove it.

Note also that there is a minor speed improvement to be had if you add a flag to stop the table count after the array has been filled. (In the game this was developed for, not an issue so I didn't.)
PM
!nullptr
Forum Support
9
Years of Service
User Offline
Joined: 27th Mar 2015
Location: Australia
Posted: 14th Apr 2015 23:50 Edited at: 15th Apr 2015 00:18
Here's a screen shot showing one "dead" mushroom, one partially alive and the balance.

Attachments

Login to view attachments
PM
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 15th Apr 2015 08:13
To fix the crash change
Function mushroom_init_name(e)
To
Function mushrooms_init(e)
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: 15th Apr 2015 08:20 Edited at: 15th Apr 2015 08:21
Ahhh... HUGE thanks smallg, that error must have got Ctrl_V'd in / missed by mistake.

I had "name" as a passable var I didn't need ultimately and must have missed it when editing the init()

Repairing with sheepish look.
PM
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 15th Apr 2015 17:08
i completely agree with getting the 'name' from the properties field though, the number of times i want to be able to specify a nearby entity without having to manually know it's entity number is crazy - there are workarounds of course depending on the required outcome but nothing quite as easy as simply checking the name.

your script looks good though
might want to also look into using function *scriptname*_exit(e)
it's ran whenever something dies, may have been easier using that than checking health is 0
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: 18th Apr 2015 21:53 Edited at: 18th Apr 2015 21:54
There is a better version of the array population used in this thread (see link below - tree shade).

The version as above required you to keep count of entities as they were placed. The latest version auto counts and populates all entities that have been assigned the script. (Mushrooms or otherwise )

https://forum.game-guru.com/thread/211693
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
Avram
GameGuru TGC Backer
17
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
!nullptr
Forum Support
9
Years of Service
User Offline
Joined: 27th Mar 2015
Location: Australia
Posted: 19th Apr 2015 10:23
Such elegance in the face of brute force.

Nice work Avram. Might avail myself of the best of your techniques my good Sir. Much obliged. :
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
Avram
GameGuru TGC Backer
17
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 19th Apr 2015 12:15
Actually it's still brute-force, except it's happening "under the hood", in the aEntity class code. Also, note that this does not read entity names which you've set in the editor, but you have to set the names of entities in their scripts (e.g. in mushroom.lua you'd put "local this = aEntity:new(e, 'mushroom');"). I have contacted Lee about that, asking him to put entity names in g_Entity, but he clearly stated that they have no intentions on doing that in near future.

Check the other methods of the aEntity class, you can get entities nearest to another entity, based on the exact name or part of the name (by using LUA string patterns), limit the distance or number of returned entities. You can easily read/write entity attributes (from/into g_Entity[e] array, and not only the built-in attributes, you can store/read custom data there) by using aEntity:attr() method. And on top of that, aPlayer class (encapsuling player "object") is derived from aEntity class, so you can do all of that with player too (get entities nearest to the player, read/write player attributes, etc), keeping the code as simple as possible
Pirate Myke
Forum Support
13
Years of Service
User Offline
Joined: 31st May 2010
Location: El Dorado, California
Posted: 20th Apr 2015 03:53
Yes, please let us read the entity name. +100 for that.
Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz, 2400 Mhz, 4 Core(s), 4 Logical Processor(s), 8gb RAM, Nvidia gtx660, Windows 7 Pro 64bit

smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 20th Apr 2015 16:52
just found you can actually currently find the name but you still need to apply a script to it so you can set it up to store it first - doesnt require any plugin or anything though

you set it up in the init(e) part like so (you can see it in weapon.lua)

Quote: "function *script name*_init_name(e,name)
weapon_name[e] = name
end"


no reason you cant use a new variable to store that name but i'm using weapon_name[e] as it's already a global to store names


life\'s one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, geforce gtx460, directx 11
Pirate Myke
Forum Support
13
Years of Service
User Offline
Joined: 31st May 2010
Location: El Dorado, California
Posted: 22nd Apr 2015 02:05
Nice, Thank you.
Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz, 2400 Mhz, 4 Core(s), 4 Logical Processor(s), 8gb RAM, Nvidia gtx660, Windows 7 Pro 64bit

Login to post a reply

Server time is: 2024-04-27 15:07:06
Your offset time is: 2024-04-27 15:07:06