Page 1 of 2
sepperating projects
Posted: Sun May 31, 2009 9:56 am
by Sparx
I saw something like what i need in an example file but didn't seem too easy...
I want to have a main file where i can set options, ip adress to connect to etc.. these options should be passed to the next file which is loaded completely independent(besides the passed options..)
What is the most effective way to seperate a project into smaler logical packages?
Re: sepperating projects
Posted: Sun May 31, 2009 10:20 am
by bartbes
Of course it depends on your wishes, but it might be as easy as the following:
main.lua:
Code: Select all
host = "some-host.example.com"
port = 2009 --yes, that's the current year..
require "myconnectionfilethingywithaverylongname.lua"
myconnectionfilethingywithaverylongname.lua:
Code: Select all
require "LUBE.lua" --couldn't help it :P
lube.client:connect(host, port)
Re: sepperating projects
Posted: Sun May 31, 2009 10:35 am
by Sparx
but what about variables? If I use the same variables in the main file as in the filewiththeverylongname... then they overlap? and what about the load function? does it initialize as normal?
Re: sepperating projects
Posted: Sun May 31, 2009 10:39 am
by bartbes
As long as the variables are not local (defined/declared with the 'local' keyword), they are global (
: ), so you can reach them from any file. About callbacks, they are recognized as long as there is a version of them defined after the first run of the code (before load). If you're defining callbacks later on (for example in load) then you'll need to put some empty functions there first, so LÖVE knows there are functions supposed to be there.
EDIT: Another example
main.lua:
code.lua:
________
main.lua:
Code: Select all
function load()
require "code.lua"
end
code.lua:
Code: Select all
function update(dt)
--doesn't work
end
_______
main.lua:
Code: Select all
function load()
require "code.lua"
end
function update()
end
code.lua:
Re: sepperating projects
Posted: Sun May 31, 2009 10:58 am
by Sparx
Thanks!
Re: sepperating projects
Posted: Mon Jul 06, 2009 5:28 pm
by Sparx
Ok I integrated this in my current project, but i have some problems terminating the newly loaded file.
for example:
main.lua is the menu where you can select the servers.. i join and then I disconnect and want to get back to the menu (main.lua).. but this I wasn't able to do.
How to do this in an elegant way?
Thanks Sparx
Re: sepperating projects
Posted: Mon Jul 06, 2009 5:49 pm
by bartbes
Most people use 'states' for this, example code for a state framework:
Code: Select all
states = {}
--insert states here, for example menu
currentstate = states.menu
function switchstate(target)
currentstate = states[target]
currentstate:load()
end
function load()
currentstate:load()
end
function update(dt)
currentstate:update(dt)
end
--etc..
Re: sepperating projects
Posted: Mon Jul 06, 2009 6:52 pm
by Zorbatron
A more complete example: (Although I'm not entirley sure why people refer to them as states, I would call them whatever makes sense like MENU or ROOM)
main.lua
Code: Select all
states = {}
for k, v in ipairs(love.filesystem.enumerate("states/")) do --loop through all files in your ./states folder
local STATE = {}
local pos = v:find("%.lua$"))
if (pos) then --if the file ends in .lua
love.filesystem.include("states/"..v) --load it
end
state[v:sub(1,pos-1)] = STATE --store state in state table using filename minus .lua
end
currentstate = states.menu
function switchstate(target)
currentstate = states[target]
end
function load()
currentstate.load() -- we use `.' instead of `:' here because we want the load function to effect the global environment not just its own state table
end
states/menu.lua
Code: Select all
function STATE:Load()
--do stuff
end
Notice how in states/menu.lua the STATE variable exists because we defined it in the loop earlier before the love.filesystem.include. Include pretty much loads the file's code onto the stack at that point and so any local variables defined still exist in that scope.
Re: sepperating projects
Posted: Mon Jul 06, 2009 7:36 pm
by bartbes
Few things, it's good that you just load the entire states folder, but I really can't agree with your choice for ., using I don't like the ENTRY. stuff, I always prefer self.. (for stuff that needs to be saved in the state, and has no business in the wide world of the global table)
EDIT: Oh and have to add, I just spotted that you DO have a : in the second snippet.
Re: sepperating projects
Posted: Mon Jul 06, 2009 11:26 pm
by Sparx
Zorbatron wrote:A more complete example.....
Thanks that looks quite good (as i thougth t should be)besides the fact that i can't get it to work. I am not very used to using methods. Could you provide the same thing you wrote me without syntax errors and working...?
Thanks in advance Sparx