As someone who is progressively learning LUA and messing with LÖVE constantly i feel as though i am able to teach others of what ive learned so far
You are looking to create entities that you can modify on the fly
The way i would do it (and it may not be the best way) would be to create a table to throw all of your entities into so then you have sort of a database or an array which is essentially what tables are.
Example time..
You create a table
Code: Select all
apples = { } --BASICALLY THIS LINE INITIALIZES A VARIABLE (apples) WITH AN EMPTY TABLE
a table can have numerous pieces of data in them, so lets start adding to the table
Code: Select all
apples = {} --INITIALIZED EMPTY TABLE IN VARIABLE apples
--Now you can access tables by using indexes take a look at the next line
apples[0] = { color="red", size="small", eaten=false }
So now what we have done is added data to the apples table, in the form of color, size and if it is eaten or not (true/false)
These are variables within the table which we can now access when the need arises. I will explain how to access them after i have explained a bit more about the structure of tables
Table indexes are pretty much unique and it would probably be best to do them incrementally.
For instance:
Code: Select all
apples = {}
apples[0] = { color="red", size="small", eaten=false }
apples[1] = { color="green", size="small", eaten=false }
apples[2] = { color="green", size="big", eaten=false }
apples[3] = { color="red", size="small, eaten=true }
So, As can see we keep changing the index between [ ] and it increases each time for a new entity ( apple[x] where x is the next incremented index )
Now that we have data in the apples table, We should probably start thinking about how to access these apples individually and also how to loop and display or modify them.
Say now, we wanted to find out what apple number 3's color is (bearing in mind table indexes start at 0, So apple number 3 would technically be number 2, because we counted the 0 as 1 ) we would access the apples table like so
Code: Select all
apples = {}
apples[0] = { color="red", size="small", eaten=false }
apples[1] = { color="green", size="small", eaten=false }
apples[2] = { color="green", size="big", eaten=false }
apples[3] = { color="red", size="small, eaten=true }
myapple = apples[2].color --apples is the table name, [2] is the index of the apples table, and .color is the variable inside the table, what we have done here is
--allowed myapple to take the data from the .color variable
Now that you have accessed the color of apple number 3 and copied it into a variable you can now display or use that data wherever you like.
Lets move onto modifying a table.
Say we want to modify apple number 4's eaten status
Code: Select all
apples = {}
apples[0] = { color="red", size="small", eaten=false }
apples[1] = { color="green", size="small", eaten=false }
apples[2] = { color="green", size="big", eaten=false }
apples[3] = { color="red", size="small, eaten=true }
--All we need to do is use the same table index to replace the variable inside it like so:
apples[3] .eaten = false --apples is the table, [3] is the index, and .eaten is the variable inside the table index
--Now the table structure will look like:
apples[0] = { color="red", size="small", eaten=false }
apples[1] = { color="green", size="small", eaten=false }
apples[2] = { color="green", size="big", eaten=false }
apples[3] = { color="red", size="small, eaten=false }
This is as simple as tables gets
As for being able to manage a table in terms of having a large quantity of apples. You would start to use loops
An example loop:
Code: Select all
apples = {}
apples[0] = { color="red", size="small", eaten=false }
apples[1] = { color="green", size="small", eaten=false }
apples[2] = { color="green", size="big", eaten=false }
apples[3] = { color="red", size="small, eaten=true }
--Do the Loop
for i=0, #apples do --THIS LINE MEANS START i AT 0 AND #apples COUNTS HOW MANY INDEXES THERE ARE, BUT THE LOOP DOES THE INCREMENTING
--MAKE A LOOP HANDLE SO WE DO NOT HAVE TO KEEP TYPING apples[i].color ETC.. MUCH EASIER TO ACCESS VARIABLE LIKE apple.color, apple.size
apple = apples[i]
--HERE WE DISPLAY THE APPLES VARIABLES BY DRAWING THEM
love.graphics.draw("Apple " .. i .. " is " .. apple.color .. " and is " .. apple.size .. " and it is " .. tostring(apple.eaten) .. " that is has been eaten.", 20, 20+20*i)
end
The last piece of code would produce something like so on-screen:
Apple 0 is red and is small and it is false that it has been eaten
Apple 1 is green and is small and it is false that it has been eaten
Apple 2 is green and is big and it is false that it has been eaten
Apple 3 is red and is small and it is true that it has been eaten
You mentioned that you would like to do an undetermined amount of entities
Take a look at the next piece of code based on the knowledge that you have seen above:
Code: Select all
apples = {} -- Initialize a blank table
appleamount = math.random(0, 200) --Generate a number between 0, 200 and put it into variable appleamount
for i=0, appleamount do
if math.random(0, 2 ) == 1 then --If a random number is 1 then the color variable will be red otherwise it will be green
color = "red"
else
color = "green"
end
apples[i] = { color=color } --This inserts the random color into the apples table
end
Now depending on what result comes from math.random(0, 200) you could have a table with anywhere between 0 and 200 indexes and also a random outcome of red / green apples each time
You would normally do the table generating once, in the load() function, and not the draw() or update() functions, because this would obviously keep resetting your table values with different results each time the code is parsed
As an example of what the table would contain would look like this due to randomness.
apples[0] = { color="red" }
apples[1] = { color="green" }
apples[2] = { color="red" }
apples[3] = { color="red" }
apples[4] = { color="green" }
apples[5] = { color="red" }
apples[6] = { color="green" }
....
Skip a few for typings sake..
....
apples[28] = { color="red" }
apples[29] = { color="red" }
apples[30] = { color="green" }
apples[31] = { color="red" }
I hope this gives you an insight on how tables work, but again this is partly some of my methodology and how i sometimes do things, even though it might not be the most efficient or best way, Its what i've come to know as being DIY for me
If theres anything that you want me to clarify or be more clear on, then just ask
PHew....Im done!
p.s - If you need a working example, I could provide one too if needs be