[Problem] With Require function.

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.
Post Reply
jajasuperman
Prole
Posts: 16
Joined: Wed Feb 20, 2013 7:16 pm

[Problem] With Require function.

Post 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?
TheScriptan
Citizen
Posts: 56
Joined: Wed Feb 27, 2013 7:53 pm

Re: [Problem] With Require function.

Post by TheScriptan »

First off all, give us your .love code, and second maybe you just not required file or something :huh:
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: [Problem] With Require function.

Post 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.)
jajasuperman
Prole
Posts: 16
Joined: Wed Feb 20, 2013 7:16 pm

Re: [Problem] With Require function.

Post 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
KaoS
Prole
Posts: 11
Joined: Sat Mar 02, 2013 2:06 am

Re: [Problem] With Require function.

Post 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
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: [Problem] With Require function.

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

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: [Problem] With Require function.

Post by bartbes »

iPoisonxL wrote:require-ing files doesn't actually do the code from the file, it just accesses the function/variables.
It does.
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: [Problem] With Require function.

Post 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.
Shallow indentations.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests