Using dofile() ?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Wombat
Prole
Posts: 31
Joined: Mon Oct 03, 2011 8:38 pm

Re: Using dofile() ?

Post by Wombat »

Ok, thanks for your help i´ll check it out and tell you if it´s work^^.
Last edited by Wombat on Thu Oct 13, 2011 7:38 pm, edited 1 time in total.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Using dofile() ?

Post by miko »

Wombat wrote:Ok, can you tell me a way to solve my problem then? ^^
I am afraid still nobody here understands your problem (certainly I do not), but let me make some guesses.

In lua (or any other interpreted language) you can have variable assignments (like "a=1"), function definitions (like "function love.load() end") and commands (like "print (2)") within a file. The "sane way" is to put all your logic within defined functions (like function stage1() ...end, function stage2() ...end), then, after each function is defined (and every file has been read in), you call every function in order (stage1(); stage2()). This is how you write a manageable code.

It is possible that you skipped the "define function" step, and execute the commands "as you go", reading and executing files one after another (dofile("stage1.lua"); dofile("stage2.lua")), and it could work in your previous game. But this is a bad style, and if you went this way, it will show up just now.

Love is not "execute as you go" environment, instead it runs its own mainloop, and calls a callback functions which you need to define in advance. So you need to define all your functions first (do all "require()'s"), and only after the main.lua script is read in and "executed", the mainloop starts working, calling your functions. So if you do (dofile("stage1.lua"); dofile("stage2.lua")) inside main.lua, you are executing your functions before the mainloop runs (i.e., the game system does not yet work).

The hackish solution would be to convert content of a script to a function (or, encapsulating a script body within a function), lets say, convert:

Code: Select all

-- file: stage1.lua
print("stage 1")
to:

Code: Select all

-- file: stage1.lua
function stage1()
  print("stage 1")
end
then you could call your newly defined stage1(), stage2(), ... functions at the desired moments (within love.update() most probably).
Still, I doubt it will work, because writing for love (or for a system with its own mainloop) is different than providing your mainloop (or not using a mainloop at all, in case of sequential executing dofile()).

Sorry for this long post, especially while I still do not understand your problem. I just hope it was close enough.
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Wombat
Prole
Posts: 31
Joined: Mon Oct 03, 2011 8:38 pm

Re: Using dofile() ?

Post by Wombat »

At first, thank you for your realy detailed answer!!! ;)

I try to explain my problem in antother way: I have mor than one main.lua called main2.lua, main3.lua, etc.

No i want to "switch" between those scripts --> when i am currently in main2.lua i want to press a button, change to main3.lua and close main2.lua.

I hope it´s more understandable now.


The only reason why i want to make my code such complicated is, that my source code originaly was made for a system in what this makes sense.

Thanks for your answers so far. :)
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Using dofile() ?

Post by tentus »

Wombat wrote:At first, thank you for your realy detailed answer!!! ;)

I try to explain my problem in antother way: I have mor than one main.lua called main2.lua, main3.lua, etc.

No i want to "switch" between those scripts --> when i am currently in main2.lua i want to press a button, change to main3.lua and close main2.lua.

I hope it´s more understandable now.


The only reason why i want to make my code such complicated is, that my source code originaly was made for a system in what this makes sense.

Thanks for your answers so far. :)
You mean like gamestates, or do you mean "run this file every time X gets pressed"?

The first is good, the second is bad.
Kurosuke needs beta testers
User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: Using dofile() ?

Post by kraftman »

yeh that's not a good way to do things, you're much better just putting in the effort to change the games layout to work with love.

assuming that you have a love.update and love.draw (for example) per mainX.lua file, change those files so that the functions are part of a table, and then have the file return that table. Store the return from each require in a table, and then call the update and draw functions that you want.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 25 guests