Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
td target system — Gideros Forum

td target system

piepie Member
edited October 2016 in Game & application design
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

  • n1cken1cke Maintainer
    edited October 2016 Accepted Answer
    I would make "target table" too, but used table-in-table approach instead:
    Target = {
      A = {B = 1, C = 1},
      B = {C = 1, D = 1},
      C = {A = 1, E = 1},
    To check targeting: `Target[unit1name1[unit2name]`
    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

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.