Please help, im trying something

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
bachadoo
Prole
Posts: 1
Joined: Mon Nov 22, 2021 3:00 am

Please help, im trying something

Post by bachadoo »

please help me, i've been trying to make the 'cash' variable go up and it wont budge, does anyone know how to fix this?







function love.load()
numberOne = 0
numberTwo = 0
numberThree = 0
cash = 0
timesSpun = 0
end

function love.update(dt)
if numberOne > 9 then
numberOne = 1
elseif numberTwo > 9 then
numberTwo = 1
elseif numberThree > 9 then
numberThree = 1
elseif numberOne == numberTwo == numberThree then
cash = cash + 1
end
end

function love.keypressed(key)
if key == "enter" then
timesSpun = timesSpun + 1
else
numberOne = numberOne + love.math.random(1, 10)
numberTwo = numberTwo + love.math.random(1, 10)
numberThree = numberThree + love.math.random(1, 10)
end

function love.draw()
love.graphics.print(cash, 10, 10)
love.graphics.print(numberOne, 200, 300)
love.graphics.print(numberTwo, 300, 300)
love.graphics.print(numberThree, 400, 300)
love.graphics.print("Press 'Enter' to Spin!", 400, 10)
end
end
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: Please help, im trying something

Post by pgimeno »

bachadoo wrote: Mon Nov 22, 2021 3:02 am please help me, i've been trying to make the 'cash' variable go up and it wont budge, does anyone know how to fix this?

Code: Select all

function love.load()
     numberOne = 0
     numberTwo = 0
     numberThree = 0
     cash = 0
     timesSpun = 0
end

function love.update(dt)
     if numberOne > 9 then
          numberOne = 1
     elseif numberTwo > 9 then
          numberTwo = 1
     elseif numberThree > 9 then
          numberThree = 1
     elseif numberOne == numberTwo == numberThree then
          cash = cash + 1
     end
end

function love.keypressed(key)
     if key == "enter" then
          timesSpun = timesSpun + 1
     else
          numberOne = numberOne + love.math.random(1, 10)
          numberTwo = numberTwo + love.math.random(1, 10)
          numberThree = numberThree + love.math.random(1, 10)
end

function love.draw()
     love.graphics.print(cash, 10, 10)
     love.graphics.print(numberOne, 200, 300)
     love.graphics.print(numberTwo, 300, 300)
     love.graphics.print(numberThree, 400, 300)
     love.graphics.print("Press 'Enter' to Spin!", 400, 10)
end
end
Please use [code]...[/code] tags for wrapping code. I've done it for you above.

The main problem is this line:

Code: Select all

     elseif numberOne == numberTwo == numberThree then
That works in Python, but not in almost every other language. Lua, like most other languages, calculates operands with the same priority from left to right (with exceptions, but == is not an exception). So that line is equivalent to:

Code: Select all

     elseif (numberOne == numberTwo) == numberThree then
That does not do what you want, because the == operator returns a boolean, so you're comparing a boolean to a number, which won't ever be true. To compare if all three are equal, you can use this instead:

Code: Select all

     elseif numberOne == numberTwo and numberTwo == numberThree then
That's not the only problem, though. In function love.keypressed, the `else` does not have a corresponding `end`, so Lua takes the next `end` (the one that you thought it would close the function) as closing the else. As a result, the function love.draw() is defined INSIDE love.keypressed. The correct way to write it is:

Code: Select all

     ...
     else
          numberOne = numberOne + love.math.random(1, 10)
          numberTwo = numberTwo + love.math.random(1, 10)
          numberThree = numberThree + love.math.random(1, 10)
     end -- ***** this is the `end` that you need to insert
end
and remove the second `end` at the end.

There will be more problems when those issues are fixed, but I leave them up to you :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 11 guests