I'm new to Love and I was just wondering what sort of structure would be best to hold game entities in. I need something so that I can access an object remotely, without having to have a global variable for the object, because entities are going to be removed and added all the time. However, they need to know where each other are for collision handling.
So, if I had an "aim" function (in a player class) that asks if there is an "enemy" to the side of it by cycling through all the enemies on screen, and picking the one that is closest, what structure should I use to keep track of the enemies?
I know that LinkedLists are sometimes used to hold entities, and I have tried HashMaps in Java, but which one is the best to use for Love?
Data Structures for holding entities
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Data Structures for holding entities
In Lua, there are only tables.
They're key-value sorted, and both the key and the value can be anything.
They're key-value sorted, and both the key and the value can be anything.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Data Structures for holding entities
A table.
It is in fact the only data structure in Lua, and always fits.
In this case, you probably want to use it as a list, so that the first entity is somelist[1] and the second somelist[2], etc.
You can loop over them by:
It is in fact the only data structure in Lua, and always fits.
In this case, you probably want to use it as a list, so that the first entity is somelist[1] and the second somelist[2], etc.
You can loop over them by:
Code: Select all
for index, entity in ipairs(somelist) do
-- your code
end
Help us help you: attach a .love.
Re: Data Structures for holding entities
You can manage the list with "table.insert(TheTable, object)" and "table.remove(TheTable, position)". The position is easy to obtain with "for position,obj in ipairs(TheTable)"
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Data Structures for holding entities
Oh, I forgot to mention you can get the length of a list with the # operator, as in:
Code: Select all
length = #mylist
Help us help you: attach a .love.
Re: Data Structures for holding entities
# operator in Lua is great in "single"-tables but I feel that count "multidimensional" tables could be easier sometimes with a native command. Unless Robin unveil me that there is a way to avoid this (and probably there is). So please correct me and please tell me a better way to do this:Robin wrote:Oh, I forgot to mention you can get the length of a list with the # operator, as in:
Code: Select all
length = #mylist
Code: Select all
totalSpecies = 0
for z=1, #species do
totalSpecies = totalSpecies + #species[z]
end
Code: Select all
species = {{"Blue Rogue", "Purple Rogue", "Green Rogue"}, {"Red Beard Pirate", "Shaved Pirate", "White Beard Pirate"}, {"Red Ninja", "Black Ninja", "White Ninja"}}
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Data Structures for holding entities
No, that's probably the best way (you can use ipairs, but that's not hugely different from what you have). The problem with "getting the length multidimensional tables" is that there are so many ways it could work. How many levels would you go down? Do tables count or only non-tables? What happens if you have a table that's something like {{...}, {...}, 39}, would it need to error? Silently ignore the 39? Or maybe even add it to the total count?
There are so many possibilities that all have use cases, that Lua lets the programmer write their own implementation.
There are so many possibilities that all have use cases, that Lua lets the programmer write their own implementation.
Help us help you: attach a .love.
Re: Data Structures for holding entities
Thank you for assure me that I'm doing it wright Robin. However I'm somehow moderately surprised. I mean I did extensive research to try find a native Lua way to do it, but kind secretly expected too that was some "concatenated" variable way to do it. No problem, I then just adapted the previous function to an universal one to "totally" count from now on "any" table. But you raise an interesting question about doing or not exceptions regarding the type of element. However how I'm doing things I don't need to worry for now with exceptions. Perhaps if needed I can add to this function another argument to tell to ignore single elements or any other not imagined situation. I'm glad you talked about it. But for now I will keep in this basic way:
I know about ipairs but it's silly that I still haven't yet inner absorbed it but as you say Robin It's good that we have so many possibilities in Lua.
Code: Select all
for above example: totalSpecies = count(species)
function count(variable) -- count total table elements
local total = 0
for z=1, #variable do
total = total + #variable[z]
end
return total
end -- end function --
Who is online
Users browsing this forum: Bing [Bot] and 7 guests