Data Structure Organization

How should I organize my data structures?

For example

local Zombies = {}

Zombies[Zombie] = {
   Damage = 50,
   Health = 100,
   Speed = 20
}

or

local Zombies = {}
table.insert(Zombies,{Zombie=Zombie, Damage=50, Health=100, Speed=20})

Which way do you guys recommend?

1 Like

The way I prefer to do it is using dictionaries inside a table, so for example;

local Zombies = {
--["NAME"] = {DAMAGE,HEALTH,SPEED},
["ZombieNormal"]  = {50,100,20},
}

Then to access the values you simply do
local Damage = Zombies["ZombieNormal"][1]
local Health = Zombies["ZombieNormal"][2]
local speed = Zombies["ZombieNormal"][3]

Thats just how I prefer to do it, it’s completely up to you.

2 Likes

It depends on how you want to set up your game. Both methods are relatively the same with minor differences (With arrays you can get the amount of items by simply doing #array, while with dictionaries you can index via dictionary[key]).

In your scenario, I think doing what MarcusTAndreas said would be easier and work better though.

Key rule: Don’t design a datastructure and make your code work around it, make your datastructure fit your needs

This means if you require fast lookup/removal of zombie stats by zombie then use first solution, but if you are just going to be iterating through all the zombies then do 2nd

You are gonna love this :+1:t2:

All about OOP

It’s OOP, Metatables & Metamethods which are really cool for Data structuring.


It’s really just preference they do the same thing, iirc “table.insert” is slightly faster (Just a little bit)

but I like to do

Table = {
   Something = 0,
   Anotherthing = 0,
}

because it’s easy to read.

Thanks for the OOP. I learned how to do it a while back on that exact thread but now my memory of it is foggy.

Never really got around to using it, I just can’t seem to set up my game to make it convenient for it. Kind of ironic because that’s part of why people use OOP lol.

1 Like

I recommend how you did it the first way, using the model as the key for the zombie stats. You store less memory than your second method. Plus, indexing Zombies using the zombie model as the key is faster than traversing through the table until you find that an elements Zombie variable points to the particular zombie you’re looking for.

That’s just me though, and the difference in performance shouldn’t be noticeable in most cases.