I looked for other questions similar to this one by searching "call function from another file", but I didn't find any that had this particular problem. It's a Lua problem. Here's the thing:
I have a series of rooms that I'm storing in a table. Inside game.lua, I create the table containing strings with the names of every room (which are also the names of the lua files for each room). I also create a variable called current_room to store the particular string with the name of the room that's supposed to be loaded. Initially, that's "room_1", which is rooms[1].
Now I want to call room_1.load() right here in game.load(). I know that it works if I write room_1.load(), but later on I may want current_room to be another room. So I want to basically do current_room.load(). I can't, because it's a string, right? So I looked around and found a few functions like loadstring() and assert(loadstring()). They didn't work, though. How do I do this?
Here's some code for the tl;dr folk:
Code: Select all
--this code is inside game.load(). there is a file called room_1.lua and it contains the function room_1.load()
rooms = {"room_1", "room_2"}
current_room = rooms[1]
-- Load the current room
assert(loadstring(current_room)).load()
EDIT: I think I found out what wasn't working. I created a variable
Code: Select all
load_current_room = current_room..".load()"
Code: Select all
assert(loadstring(load_current_room))
EDIT 2: Nope, didn't work. LOVE stopped accusing an error, but the code I put in room_1.load() doesn't get executed. Back to square one.