Referencing a specific function in a general way
Posted: Tue Jun 17, 2014 9:37 am
When I say general way, I mean in a way to make it convenient.
When we talk about tables, we talk about how they enable us to conveniently reference more than one block of data.
instead of
we can simply do
but what if we have a series of similar functions instead? I am working on a game that involves a lot of different maps, and I was hoping to just have a unique update function for each one. Just in case I feel like adding some special properties to certain maps that may not need to be applied to all.
I have a folder that contains all of my maps data, and ideally I would like to do something like this:
So I can simply execute it by running:
but obviously that isn't possible.
is there a way to run these functions in a organized way such as this? Each function is in it's own individual file, in case there is some way to run a function that corresponds to the file that it is in. I know that it is not that much extra effort to just not try to do something like this and have a conditional at the beginning of each script saying like..
But I figured it would be worth knowing if there is a simpler way to accomplish this.
Thanks in advanced.
PS It's two in the morning and I am extremely sleep deprived, as usual. So I apologize if it is slightly unclear what I am asking; I can try to explain myself more if it is.
When we talk about tables, we talk about how they enable us to conveniently reference more than one block of data.
instead of
Code: Select all
appleString = "apple"
bananaString = "banana"
orangeString = "orange"
print(appleString)
print(bananaString)
print(orangeString)
Code: Select all
fruit = {"apple","banana","orange"}
for i=1,#fruit do
print(fruit[i])
end
I have a folder that contains all of my maps data, and ideally I would like to do something like this:
Code: Select all
function update_map["castle"](dt)
end
Code: Select all
update_map[current_map](dt)
is there a way to run these functions in a organized way such as this? Each function is in it's own individual file, in case there is some way to run a function that corresponds to the file that it is in. I know that it is not that much extra effort to just not try to do something like this and have a conditional at the beginning of each script saying like..
Code: Select all
function update_castle(dt)
if current_map == "castle" then
--Code
end
end
Thanks in advanced.
PS It's two in the morning and I am extremely sleep deprived, as usual. So I apologize if it is slightly unclear what I am asking; I can try to explain myself more if it is.