Page 1 of 1
[Problem] With Require function.
Posted: Sat Mar 09, 2013 9:33 am
by jajasuperman
Hi all!!
I´m doing a little game and I have this problem with require function.
In the game I have 2 menus, Main Menu and Option Menu.
When you execute main menu, the are three options. One of the options y to go to Optiom Menu. I do that with require function.
The problem is here. When i push escape key, the program requires Main Menu, but is does not do enything.
I think that the conclusion is that if you use require to a code that you use before, it does not do anything.
How i solve it?
Re: [Problem] With Require function.
Posted: Sat Mar 09, 2013 9:41 am
by TheScriptan
First off all, give us your .love code, and second maybe you just not required file or something
![Huuh... :huh:](./images/smilies/ms-huh.png)
Re: [Problem] With Require function.
Posted: Sat Mar 09, 2013 11:26 am
by bartbes
jajasuperman wrote:
I think that the conclusion is that if you use require to a code that you use before, it does not do anything.
That is correct. Then again, if you rely on code executing like that, you're
probably doing it wrong. (There are valid use cases, but this doesn't sound like one.)
Re: [Problem] With Require function.
Posted: Sat Mar 09, 2013 12:40 pm
by jajasuperman
This there are the codes of the menus:
Main menu (In line 56 I use require to go to option menu):
http://pastebin.com/UHztjNtD
Option menu (In line 48 I use require to go to Main menu):
http://pastebin.com/ziXSTBZm
I don´t think that I have nothing grong :S
Re: [Problem] With Require function.
Posted: Sat Mar 09, 2013 1:21 pm
by KaoS
I don't think that's how it works. I'm a beginner at love2d so I may be wrong but as far as I'm aware if you require a file it does not call it's love.update functions etc. it just runs through the code once and loads all the functions... perhaps it would work by overwriting the love.update function with the one in the required file...
basically I would advise just putting functions in the required file, requiring it at the top of your file and then using the functions later on
Re: [Problem] With Require function.
Posted: Sat Mar 09, 2013 3:11 pm
by iPoisonxL
require-ing files doesn't actually do the code from the file, it just accesses the function/variables. You can create variables/functions in an other file, then require it at the top of your code, then execute those function/variables in another file.
example:
main.lua:
Code: Select all
require 'function'
function love.load()
load_variables()
end
function.lua:
Code: Select all
function load_variables()
x=100
y=100
--any other variable
end
Basically what I'm saying is always require at the top of your code, and make sure to use functions/variables.
Re: [Problem] With Require function.
Posted: Sat Mar 09, 2013 3:18 pm
by bartbes
iPoisonxL wrote:require-ing files doesn't actually do the code from the file, it just accesses the function/variables.
It does.
Re: [Problem] With Require function.
Posted: Sat Mar 09, 2013 3:30 pm
by Boolsheet
jajasuperman wrote:I don´t think that I have nothing grong :S
I think you expect require to execute the code every time you call it, but it does it only the first time. Think of the code it loads as a module that gets executed once and then everyone can use it. require actually saves the return value of the module (if it returned one) and that will be returned the next time you call require with the same module name.
I recommend you don't call the functions directly love.draw and love.update, but something like menu1Draw and menu2Draw. You then assign those functions to love.draw instead when the event for the change triggers.