sepperating projects
sepperating projects
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?
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?
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: sepperating projects
Of course it depends on your wishes, but it might be as easy as the following:
main.lua:
myconnectionfilethingywithaverylongname.lua:
main.lua:
Code: Select all
host = "some-host.example.com"
port = 2009 --yes, that's the current year..
require "myconnectionfilethingywithaverylongname.lua"
Code: Select all
require "LUBE.lua" --couldn't help it :P
lube.client:connect(host, port)
Re: sepperating projects
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?
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: sepperating projects
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.lua:
_______
main.lua:
code.lua:
EDIT: Another example
main.lua:
Code: Select all
require "code.lua"
Code: Select all
function load()
--works
end
main.lua:
Code: Select all
function load()
require "code.lua"
end
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: Select all
function update(dt)
--works
end
Re: sepperating projects
Thanks!
Re: sepperating projects
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
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
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: sepperating projects
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
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
states/menu.lua
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.
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
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: sepperating projects
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.
EDIT: Oh and have to add, I just spotted that you DO have a : in the second snippet.
Last edited by bartbes on Tue Jul 07, 2009 9:06 am, edited 1 time in total.
Re: sepperating projects
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
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 3 guests