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.
mushy_arr = {}
function mushrooms_init(e)
-- if you add 20 mushrooms to your map accessing the mushroom script you NEED to say so if you want them all added to your array.
mushy_count=20
end
function mushrooms_main(e)
-- the magic happens here - pun intended
tcount=table.count(mushy_arr)
if tcount < mushy_count then -- we haven't grabbed all the id's yet
mushy_arr[tcount+1] = e
end -- if
-- array dump/test code - want to see how well your mushrooms are dying?
for i=1, tcount, 1 do
TextColor(2,i*3,3,"valE = "..mushy_arr[i].." Health = "..g_Entity[mushy_arr[i]]['health'],200,200,200)
end -- for
-- examples of use - reducing health as you shoot, killing and flagging the mushroom kill
for i=1, tcount, 1 do
if g_Entity[mushy_arr[i]]['health'] <= 0 and g_Entity[mushy_arr[i]]['health'] ~= -999 then
-- trip the health array. -999 is our "this is dead" flag
Destroy(mushy_arr[i])
g_Entity[mushy_arr[i]]['health'] = -999 -- if people use 1001 damage with 1 entity health this will bug :)
kills=kills+1
end -- if
end -- for
end
end -- function
-- ***** from LUA 5.1 reference manual ********************************************
function table.count(tbl)
local c=0
for k in pairs(tbl) do
c=c+1
end
return c
end
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.)