Hi, I am building a tower defense game, and I'd like to set some target rules between units.
Ie.
Unit A can target Unit B and unit C
Unit B can target C and D
Unit C can target A and E...
Something like that.
Everything happens on enterframe, so I figured it's best to avoid for loops (there already is a
for to loop between units, and I noticed that nested fors are performance killers).
My best option right now seems to keep a "target table" with meaningful string indices:
Target = {
AB = 1,
AC = 1,
BC = 1,
BD = 1,
CA =1,
CE = 1
...
}
Doing a check on target [unitname..unit2name] I can know if the units can target each other.
Can you think of another way (better, faster, lighter..) to do this without using string indices?
I'm worried that each time a string is created is a new one, and this is bad for lua - I've read this somewhere from best practices.
I thought about using POT numbers instead of letters, however I believe that indices would be converted to strings anyway, and reconverted to numbers when they need to be summed, to be converted again to string when checking if target [POTSUM]=1
Thank you, any suggestion is welcome
Comments
Because Lua table access is very fast and doesn't create new objects in memory. It also looks more readable and you can use string indexes of any length.
Likes: pie