Error message: attempt to perform arithmetic on field...

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
bigmac
Prole
Posts: 3
Joined: Sat Jun 02, 2012 6:21 pm

Error message: attempt to perform arithmetic on field...

Post by bigmac »

Hello guys. So i've decided to pick up on lua again after some time and i have just been trying to put a simple 2d sidescroller togheter, but i got stuck on jumping as many others, but no, not really the jumping part.Its an error i get all the time. : attempt to perform arithmetic on field 'gravSecond' ( a nil value)

so i figured i got to to be doing somthing wrong :P

this example code is from another thread: viewtopic.php?f=4&t=8831&hilit=jumping

Disclaimer: dont worry, wont use the code for real, just learning. :)

Code: Select all

function love.load()

   
  
    player = love.graphics.newImage("assets/player.png")
 
   playerx = 270
   ySpeed = 0
    playery = 400
	gravSecond = 1.5
 
   bg = love.graphics.newImage("assets/background.png")
  
   -- music
   music = love.audio.newSource( "assets/music.ogg" , "steam")
   music:setLooping(true)
   love.audio.play(music)
  
   Cloudx = 0
  
    yCloud = 900
  
   cloudimage = love.graphics.newImage("assets/cloud.gif")
   bullet = love.graphics.newImage("assets/bullet.gif")
   
   right = love.graphics.newImage("assets/player.png")
   left = love.graphics.newImage("assets/playerback.png")
  
   
end
function love.keypressed(key)
   if key == " " and player.y < 400 then -- Are we on the ground?
      player.ySpeed = -20 -- Make us add a negative, to move up
   end
end

function love.update(dt)
   frame = dt * 30 -- I do this just because I think better this way
   player.ySpeed = player.ySpeed + player.gravSecond * frame -- We add gravity to the ySpeed
   player.y = player.y + player.ySpeed * frame -- We add ySpeed to the player's y position
   if player.y > 400 then -- Are we on the ground?
      player.y = player.y - player.ySpeed * frame -- If we are, We undo that moving down
      player.ySpeed = 0 -- The ySpeed is reset
   end
   Cloudx =  Cloudx + 20*dt 
   if Cloudx >= (800 + 578) then
      Cloudx = -100
   end

   
   
   if love.keyboard.isDown("d") then
      playerx = playerx + 275*dt
      player = right
   end   

   if love.keyboard.isDown("s") then
      playery = playery + 675*dt
   end
   
   
   
   if love.keyboard.isDown("a") then
      player = left
      playerx = playerx - 275*dt
   end   
   
   
   if playery > 420 then
      playery = 420
   end   
   
      
end

function love.draw()
  
   
   
   
   love.graphics.setBackgroundColor(40,255,40)
  
  
    love.graphics.setColor(111,183,255)
   love.graphics.rectangle("fill",0,0,love.graphics.getWidth(),450)
   love.graphics.setColor(255,255,255)
  
   
   
   love.graphics.draw(cloudimage, Cloudx - 578, -200)
   
   
   
   love.graphics.draw(player, playerx, playery)
end


Thanks
Bannana97
Citizen
Posts: 77
Joined: Wed May 16, 2012 2:49 pm

Re: Error message: attempt to perform arithmetic on field...

Post by Bannana97 »

Try putting local gravSecond = 0 at the very top of your code, before the load function.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Error message: attempt to perform arithmetic on field...

Post by coffee »

Looking quickly your code you are mixing player variables that are inside "player" table and other player variables that aren't. For example you trying using player.gravSecond that is not defined before. Only gravSecond is. Or you remove in update function the "player." before gravSecond or initalize player = {} and began put your player variables inside player table.

For use player.anyvariableyouwant you should at start use player = {} and then define the sub-variables in your player table like:

Code: Select all

   player = {}
   player.x = 270
   player.ySpeed = 0
   player.y = 400
   player.gravSecond = 1.5
   player.img = love.graphics.newImage("assets/player.png")
or

Code: Select all

player = {
   x = 270
   ySpeed = 0
   y = 400
   gravSecond = 1.5
   img = love.graphics.newImage("assets/player.png")
}
Don't worry if you don't understand this. Someone will for sure post a better answer than mine.
bigmac
Prole
Posts: 3
Joined: Sat Jun 02, 2012 6:21 pm

Re: Error message: attempt to perform arithmetic on field...

Post by bigmac »

Bannana97 that didn't help very much :/

Coffee

Yeah, realised it was really messed so i cleaned it up and got the player variables right, atleast i think so. anyhow.

this is how it looks now

Code: Select all

function love.load()
   player = {}
   player.x = 270
   player.ySpeed = 0
   player.y = 380
   player.gravSecond = 1.5
   player.img = love.graphics.newImage("assets/player.png")
   
   bg = love.graphics.newImage("assets/background.png")
  
   -- music
   music = love.audio.newSource( "assets/music.ogg" , "steam")
   music:setLooping(true)
   love.audio.play(music)
  
   Cloudx = 0
  
    yCloud = 900
  
   cloudimage = love.graphics.newImage("assets/cloud.gif")
   bullet = love.graphics.newImage("assets/bullet.gif")
   
   right = love.graphics.newImage("assets/player.png")
   left = love.graphics.newImage("assets/playerback.png")
  
   
end


function love.update(dt)
   frame = dt * 30 
   player.ySpeed = player.ySpeed + player.gravSecond * frame 
   player.y = player.y + player.ySpeed * frame 
   if player.y > 380 then 
      player.y = player.y - player.ySpeed * frame 
      player.ySpeed = 0 
   end
   if love.keyboard.isDown("up") and player.ySpeed == 0 then 
      
      player.ySpeed = -20
   end
   
 if love.keyboard.isDown("d") then
      player.x = player.x + 275*dt
      player = right
   end   
    
   if love.keyboard.isDown("a") then
      player = left
      player.x = player.x - 275*dt
   end   
   
   
   if player.y > 380 then
      player.y = 380
   end   

   Cloudx =  Cloudx + 20*dt 
   if Cloudx >= (800 + 578) then
      Cloudx = -100
   end

end

function love.draw()
  
    love.graphics.setColor(40,255,40)
    love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
    love.graphics.setColor(255,255,255) 
  
    love.graphics.setColor(111,183,255)
    love.graphics.rectangle("fill",0,0,love.graphics.getWidth(),450)
    love.graphics.setColor(255,255,255)
  
    love.graphics.draw(cloudimage, Cloudx - 578, -200)
   
    love.graphics.draw(player, player.x, player.y)
end


the new error is: main.lua:77: Incorrect parameter type: expected userdata.

well, it's this part love.graphics.draw(player, player.x, player.y) i can't get right, i dont know how i should draw it when i do it like this. but other than that, i actually understood what you meant coffee, had too re-read it a few times but i blame it on the the fact that it's night :)
User avatar
Lynesth
Prole
Posts: 30
Joined: Sun May 16, 2010 8:47 am

Re: Error message: attempt to perform arithmetic on field...

Post by Lynesth »

bigmac wrote:the new error is: main.lua:77: Incorrect parameter type: expected userdata.

well, it's this part love.graphics.draw(player, player.x, player.y) i can't get right, i dont know how i should draw it when i do it like this. but other than that, i actually understood what you meant coffee, had too re-read it a few times but i blame it on the the fact that it's night :)
Hi there.

love.graphics.draw() needs at least the 3 first parameters (drawable, the x position, the y position).
In your code, the drawable you want to draw here is "player.img" not just "player".
So just change your line with :

Code: Select all

love.graphics.draw(player.img, player.x, player.y)
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Error message: attempt to perform arithmetic on field...

Post by coffee »

There's still some confusion in your code. Since you dropped in your code things from another sources that use another methodologies you need now to clean and make homogeneous your code. Still finding player variables inside/outside table.
For example

Code: Select all

   right = love.graphics.newImage("assets/player.png")
   left = love.graphics.newImage("assets/playerback.png")
You should for coherence put it also in player table.

Your introdution of "music" code with operators (:) is not helping for now (you should first learn the principles of that). Try to comment this section and focus and finish for now "player" related things. Then reorganize "cloud" variables as you doing with "player".

Instead of cut-copy paste sections of code and assemble it would be more wise if do first the LOVE tutorials. You wouldn't later be praying for that your code mixes work. And would really learn some basics that you need for now.
https://love2d.org/wiki/Category:Tutorials (Hamster tutorial is a good one to start).
User avatar
Lynesth
Prole
Posts: 30
Joined: Sun May 16, 2010 8:47 am

Re: Error message: attempt to perform arithmetic on field...

Post by Lynesth »

coffee wrote:Instead of cut-copy paste sections of code and assemble it would be more wise if do first the LOVE tutorials. You wouldn't later be praying for that your code mixes work. And would really learn some basics that you need for now.
https://love2d.org/wiki/Category:Tutorials (Hamster tutorial is a good one to start).
This is an advise you should follow ;)
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.
bigmac
Prole
Posts: 3
Joined: Sat Jun 02, 2012 6:21 pm

Re: Error message: attempt to perform arithmetic on field...

Post by bigmac »

You're right guys, thanks alot for your help, really helped. And yeah, i'll probably be doing my own code now and follow some tutorials. But i got the player to be able to jump :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 0 guests