Initializing a specific object with arbitrary parameters
Posted: Wed Sep 24, 2008 12:02 am
In my current project I have four classes of objects, all of which are sub-classes of "object"
* players
* minions
* enemies
* obstacles
objects have a variety of attributes, including health, xpos, ypos, xvel, yvel, name and so forth.
To keep track of everything, I have five lists - one which is the "object" list which holds everything, and four which are the list indicies for each class of object. For example, my object list might look like this:
Now my level definition will hold the Load"X" functions, along with tables of the attributes. To start, my functions looked like this:
This works fine - I leverage the fact that I know how many of each thing will be initialized, so I just replace the information I want to replace directly. This allows me to initialize each thing with default values, and then specifically overwrite the values I need changed. I get that because I am modifying the elements explicitly.
However, it does require a lot of typing in the load functions. I'd much rather have a table of minions that looks like this:
Now I'm not touching the minion table explicitly, but you can see how I might easily initialize 3 minions into the object list from this data. I can procedurally create names, and replace the default xpos and ypos with the data from x and y.
But here is the problem - I don't see an easy way to do the same thing given a list with arbitrary parameters from the object list. So instead of just x and y, I might want to specify x, y, and health for one minion, and x,y, vel for another. Once the list of data in each element of "setupminion" is unknown, I lose the simplicity which makes this implementation much keener.
To sum up - mangling the initialized data gives me perfect power, at the cost of changeability and ease of understanding. Initializing from smaller tables gives me the clarity and tunability I want, but at the cost of some flexibility. I want both. How can I get it?
and no, I don't want to check each table in "setupminion" to see if each possible parameter exists, and if so overwrite the proper data in "minion."
--Mr. Strange
* players
* minions
* enemies
* obstacles
objects have a variety of attributes, including health, xpos, ypos, xvel, yvel, name and so forth.
To keep track of everything, I have five lists - one which is the "object" list which holds everything, and four which are the list indicies for each class of object. For example, my object list might look like this:
Code: Select all
-- setup variables
object = {}
minion = {}
enemy = {}
obstacle = {}
-- these numbers come from a load level function
numberOfMinions = 3
numberOfEnemies = 5
numberOfObstacles = 15
numberofObjects = numberOfMinions + numberOfEnemies + numberOfObstacles
for i = 1, numberOfObjects do
object[i] = {
type = "none",
health = 100,
xpos = 0,
ypos = 0,
xvel = 0,
yvel = 0,
name = tostring(i),
}
end
LoadMinions()
LoadEnemies()
LoadObstacles()
Code: Select all
function LoadMinions()
minion[1].xpos = 50
minion[1].ypos = 25
minion[1].name = "minion1"
minion[2].xpos = 49
minion[2].ypos = 40
minion[2].name = "minion2"
minion[3].xpos = 59
minion[3].ypos = 20
minion[3].name = "minion3"
end
However, it does require a lot of typing in the load functions. I'd much rather have a table of minions that looks like this:
Code: Select all
setupminion = {}
setupminion[1] = {x = 50, y = 25,}
setupminion[2] = {x = 49, y = 40,}
setupminion[3] = {x = 59, y = 20,}
But here is the problem - I don't see an easy way to do the same thing given a list with arbitrary parameters from the object list. So instead of just x and y, I might want to specify x, y, and health for one minion, and x,y, vel for another. Once the list of data in each element of "setupminion" is unknown, I lose the simplicity which makes this implementation much keener.
To sum up - mangling the initialized data gives me perfect power, at the cost of changeability and ease of understanding. Initializing from smaller tables gives me the clarity and tunability I want, but at the cost of some flexibility. I want both. How can I get it?
and no, I don't want to check each table in "setupminion" to see if each possible parameter exists, and if so overwrite the proper data in "minion."
--Mr. Strange