sepperating projects

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Sparx
Party member
Posts: 125
Joined: Thu Apr 02, 2009 9:54 am
Location: Aachen, Germany

sepperating projects

Post 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?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: sepperating projects

Post 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)
User avatar
Sparx
Party member
Posts: 125
Joined: Thu Apr 02, 2009 9:54 am
Location: Aachen, Germany

Re: sepperating projects

Post 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?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: sepperating projects

Post by bartbes »

As long as the variables are not local (defined/declared with the 'local' keyword), they are global ( :ultrashocked:: ), 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: Select all

require "code.lua"
code.lua:

Code: Select all

function load()
--works
end
________

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:

Code: Select all

function update(dt)
--works
end
User avatar
Sparx
Party member
Posts: 125
Joined: Thu Apr 02, 2009 9:54 am
Location: Aachen, Germany

Re: sepperating projects

Post by Sparx »

Thanks!
User avatar
Sparx
Party member
Posts: 125
Joined: Thu Apr 02, 2009 9:54 am
Location: Aachen, Germany

Re: sepperating projects

Post 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
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: sepperating projects

Post 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..
User avatar
Zorbatron
Citizen
Posts: 78
Joined: Wed May 27, 2009 6:58 pm

Re: sepperating projects

Post 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: sepperating projects

Post 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.
Last edited by bartbes on Tue Jul 07, 2009 9:06 am, edited 1 time in total.
User avatar
Sparx
Party member
Posts: 125
Joined: Thu Apr 02, 2009 9:54 am
Location: Aachen, Germany

Re: sepperating projects

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 259 guests