Having trouble with Arithmetic.

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
HJW012
Prole
Posts: 8
Joined: Tue Sep 03, 2013 9:50 pm

Having trouble with Arithmetic.

Post by HJW012 »

game.love
(3.31 MiB) Downloaded 156 times
Hello. First off, I'd like to say that I am quite new to Love2D and Lua in general so bare with me.

Recently, I have been trying to make a simple game with a friend of mine. I know that I should work on some easier things before jumping the gun and making a game but I decided to jump right in. The first thing I had was a simple level where a player could walk left and right and jump. It worked fine until I made the main menu. It now shows the following error:
KMbyX9A.png
KMbyX9A.png (17.55 KiB) Viewed 348 times
The code I am using is as follows:
zFqpVQd.png
zFqpVQd.png (71.92 KiB) Viewed 348 times
Thank you in advance and if you require any more information, please let me know. Thanks!
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Having trouble with Arithmetic.

Post by Plu »

If you are inside a function of an object, like in the player.phsyics function, you cannot reference to the variable "player", you have to reference the variable "self". So it becomes self.x = self.x + self.xvel * dt
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Having trouble with Arithmetic.

Post by micha »

Plu wrote:If you are inside a function of an object, like in the player.phsyics function, you cannot reference to the variable "player", you have to reference the variable "self". So it becomes self.x = self.x + self.xvel * dt
While it is generally a good idea to use the self-shortcut, this is not the problem here. The table player can be accessed with its name.

The problem in the code is that the function player.load() is never called. Make sure that when you change to the game-state that you call player.load.

In your current implementation you seem to have a gamestate and every time you change the game state you redefine love.draw() and love.update() and also love.load(). Note that love.load() is really only called once in the very beginning. If you change state you have to call it manually.

Besides that I suggest you do not redefine the love-main functions. It might look cleaner in the code, because you save one if-condition. But debugging can get more tedious because the function name is not unique anymore (debugging is still possible, because you get the file and line number).

So better do an in-condition in the main loop:

Code: Select all

function love.update(dt)
  if state == 'menu' then
    menu.update(dt)
  elseif state == 'level1' then
    level1.update(dt)
  end
end
and have individual update functions in each file (the same goes for drawing and keyboard/mouse handling).

Oh, and I suggest next time you use the code-tags instead of pasting an image of the code. That way it is easier for us to copy the code and help you.
Last edited by micha on Thu Sep 05, 2013 7:48 am, edited 1 time in total.
HJW012
Prole
Posts: 8
Joined: Tue Sep 03, 2013 9:50 pm

Re: Having trouble with Arithmetic.

Post by HJW012 »

Plu wrote:If you are inside a function of an object, like in the player.phsyics function, you cannot reference to the variable "player", you have to reference the variable "self". So it becomes self.x = self.x + self.xvel * dt
It now says : attempt to index global 'self' (a nil value). Thanks for the quick reply!
HJW012
Prole
Posts: 8
Joined: Tue Sep 03, 2013 9:50 pm

Re: Having trouble with Arithmetic.

Post by HJW012 »

micha wrote:
Plu wrote:If you are inside a function of an object, like in the player.phsyics function, you cannot reference to the variable "player", you have to reference the variable "self". So it becomes self.x = self.x + self.xvel * dt
While it is generally a good idea to use the self-shortcut, this is not the problem here. The table player can be accessed with its name.

The problem in the code is that the function player.load() is never called. Make sure that when you change to the game-state that you call player.load.

In your current implementation you seem to have a gamestate and every time you change the game state you redefine love.draw() and love.update() and also love.load(). Note that love.load() is really only called once in the very beginning. If you change state you have to call it manually.

Besides that I suggest you do not redefine the love-main functions. It might look cleaner in the code, because you save one if-condition. But debugging can get more tedious because the function name is not unique anymore (debugging is still possible, because you get the file and line number).

So better do an in-condition in the main loop:

Code: Select all

function love.update(dt)
  if state == 'menu' then
    menu.update(dt)
  elseif state == 'level1' then
    level1.update(dt)
  end
end
and have individual update functions in each file (the same goes for drawing and keyboard/mouse handling).

Oh, and I suggest next time you use the code-tags instead of pasting an image of the code. That way it is easier for us to copy the code and help you.
Thank you so much for the help and the quick reply. It now works! Thank you!
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Having trouble with Arithmetic.

Post by Plu »

Sorry I probably misread something then :P It's still a bit early.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 9 guests