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?
[Problem] With Require function.
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Citizen
- Posts: 56
- Joined: Wed Feb 27, 2013 7:53 pm
Re: [Problem] With Require function.
First off all, give us your .love code, and second maybe you just not required file or something
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: [Problem] With Require function.
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 wrote: I think that the conclusion is that if you use require to a code that you use before, it does not do anything.
-
- Prole
- Posts: 16
- Joined: Wed Feb 20, 2013 7:16 pm
Re: [Problem] With Require function.
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
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.
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
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.
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:
function.lua:
Basically what I'm saying is always require at the top of your code, and make sure to use functions/variables.
example:
main.lua:
Code: Select all
require 'function'
function love.load()
load_variables()
end
Code: Select all
function load_variables()
x=100
y=100
--any other variable
end
Code: Select all
L
L Ö
Ö V
L Ö V E
Ö B E
V E
E Y
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: [Problem] With Require function.
It does.iPoisonxL wrote:require-ing files doesn't actually do the code from the file, it just accesses the function/variables.
Re: [Problem] With Require function.
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.jajasuperman wrote:I don´t think that I have nothing grong :S
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.
Who is online
Users browsing this forum: No registered users and 0 guests