Data Structures for holding entities

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
iLuvLove
Prole
Posts: 4
Joined: Thu Nov 24, 2011 5:32 pm

Data Structures for holding entities

Post by iLuvLove »

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?
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Data Structures for holding entities

Post by thelinx »

In Lua, there are only tables.

They're key-value sorted, and both the key and the value can be anything.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Data Structures for holding entities

Post by Robin »

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:

Code: Select all

for index, entity in ipairs(somelist) do
 -- your code
end
Help us help you: attach a .love.
User avatar
Ellohir
Party member
Posts: 235
Joined: Sat Oct 22, 2011 11:12 pm

Re: Data Structures for holding entities

Post by Ellohir »

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)" :awesome:
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Data Structures for holding entities

Post by Robin »

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.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Data Structures for holding entities

Post by coffee »

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
# 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:

Code: Select all

	totalSpecies = 0
	for z=1, #species do
		totalSpecies = totalSpecies + #species[z]
	end
in a table like this

Code: Select all

   species = {{"Blue Rogue", "Purple Rogue", "Green Rogue"}, {"Red Beard Pirate", "Shaved Pirate", "White Beard Pirate"}, {"Red Ninja", "Black Ninja", "White Ninja"}}
Yes Robin was a shameful way to try that you teach me a better way ;)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Data Structures for holding entities

Post by Robin »

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.
Help us help you: attach a .love.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Data Structures for holding entities

Post by coffee »

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:

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 --
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. :)
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests