how to compare 2 values ​​of variables ...

I’m not able to compare two values ​​of variables in Corona SDK, HELP-ME PLEASE…

Here is my code used to compare:

&nbsp; &nbsp; local bestscore = 0 &nbsp; &nbsp; local paraguardar = 0 &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; local bestscore = display.newText(bestscore, 300, 0, nil, 30) &nbsp; &nbsp; bestscore.isEditable = true &nbsp; &nbsp; bestscore.bestscore = bestscore &nbsp; &nbsp; bestscore.myName = bestscore &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; local function HeMoved(event, tap) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if event.phase == "began" then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;local object = event.target &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; paraguardar = paraguardar + 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; local function Comparando() &nbsp; &nbsp; &nbsp; &nbsp;if(bestscore \< paraguardar) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bestscore = paraguardar &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bestscore.text = paraguardar &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp;&nbsp;

    After…

    

           Comparando()

I took only the parts that might be giving error … 

Help me, please

Hello pabloapdz

I think you should use basic debugging tips like you just print that both of variables value. it might be that any of variable is not having

value and error would be appearing like compare number with 0 or nil.

You just start basic debugging in code like you can do 

print(“bestscore”…bestscore);

print(“paraguardar”…paraguardar);

and wait a minute you are trying to compare which is a text.

if you want to compare text value do like this

if(tonumber(bestscore.text) < paraguardar) then

–CODE

end

or you can assign that text value to any other variable. like

local temp = bestscore.text;

if(temp< paraguardar) then

–CODE

end

I think This may helps!!!

Thanks

Neel.

No, look, I set up there as “bestscore local = 0 and 0 = Local paraguardar” So both are variable, right? I still do not understand why living giving error …

There are two ways to look at this problem, first look at your code:

    local bestscore = 0
    local paraguardar = 0
    
    local bestscore = display.newText(bestscore, 300, 0, nil, 30)

In line 1, you make a local variable bestscore, then on line 4, you define another local variable called bestscore.  The fact that you made both local means you can’t access the first one.  Secondly you can only have one variable with the same name in the same block of code.  If the second “local bestscore” was inside an if statement, for or while loop or another function, then it would let you use the same variable name for different purposes.  In your case, they are in the same block.

Next, if I have a variable called  myvariable and I assign it a number:

myvariable = 10

then later, I assign it a string:

myvariable = “Fred”

then myvariable no longer holds a number, it’s been overwritten and replaced with the string.  In your case you start with bestscore being a number, then a few lines later  you make it a table (a display object).  Then you try to do math with the and you can’t do math with a table…

Rob

From what I understand, I would just change the

 local = bestscore display.newText (bestscore, 300, 0, nil, 30)

to

 &nbsp; bestscore = display.newText (bestscore, 300, 0, nil, 30)

right? but I’ve done it and still does not work :

local = bestscore display.newText (bestscore, 300, 0, nil, 30)

is syntactically incorrect.  It would be more like:

local bestscore = display.newText (bestscore, 300, 0, nil, 30)

but that still has a problem in that you’re trying to use the variable bestscore for two different purposes.  The first instance (bolded below) will hold the display object that is the physical text on the strong.  It’s a table type, not a number.

local bestscore = display.newText (bestscore, 300, 0, nil, 30)

but you then turn around and use the variable bestscore as either a string or number (it can take either) as the actual value for display.newText() to use:

local bestscore = display.newText ( bestscore , 300, 0, nil, 30)

If you want bestscore to be the number that you’re going to do math with and compare to the current score, then you need to name the display object something different.  Perhaps you should try:
 

local bestscoreText = display.newText (bestscore, 300, 0, nil, 30)

Rob

Thank you, error solved {:

Hello pabloapdz

I think you should use basic debugging tips like you just print that both of variables value. it might be that any of variable is not having

value and error would be appearing like compare number with 0 or nil.

You just start basic debugging in code like you can do 

print(“bestscore”…bestscore);

print(“paraguardar”…paraguardar);

and wait a minute you are trying to compare which is a text.

if you want to compare text value do like this

if(tonumber(bestscore.text) < paraguardar) then

–CODE

end

or you can assign that text value to any other variable. like

local temp = bestscore.text;

if(temp< paraguardar) then

–CODE

end

I think This may helps!!!

Thanks

Neel.

No, look, I set up there as “bestscore local = 0 and 0 = Local paraguardar” So both are variable, right? I still do not understand why living giving error …

There are two ways to look at this problem, first look at your code:

    local bestscore = 0
    local paraguardar = 0
    
    local bestscore = display.newText(bestscore, 300, 0, nil, 30)

In line 1, you make a local variable bestscore, then on line 4, you define another local variable called bestscore.  The fact that you made both local means you can’t access the first one.  Secondly you can only have one variable with the same name in the same block of code.  If the second “local bestscore” was inside an if statement, for or while loop or another function, then it would let you use the same variable name for different purposes.  In your case, they are in the same block.

Next, if I have a variable called  myvariable and I assign it a number:

myvariable = 10

then later, I assign it a string:

myvariable = “Fred”

then myvariable no longer holds a number, it’s been overwritten and replaced with the string.  In your case you start with bestscore being a number, then a few lines later  you make it a table (a display object).  Then you try to do math with the and you can’t do math with a table…

Rob

From what I understand, I would just change the

 local = bestscore display.newText (bestscore, 300, 0, nil, 30)

to

 &nbsp; bestscore = display.newText (bestscore, 300, 0, nil, 30)

right? but I’ve done it and still does not work :

local = bestscore display.newText (bestscore, 300, 0, nil, 30)

is syntactically incorrect.  It would be more like:

local bestscore = display.newText (bestscore, 300, 0, nil, 30)

but that still has a problem in that you’re trying to use the variable bestscore for two different purposes.  The first instance (bolded below) will hold the display object that is the physical text on the strong.  It’s a table type, not a number.

local bestscore = display.newText (bestscore, 300, 0, nil, 30)

but you then turn around and use the variable bestscore as either a string or number (it can take either) as the actual value for display.newText() to use:

local bestscore = display.newText ( bestscore , 300, 0, nil, 30)

If you want bestscore to be the number that you’re going to do math with and compare to the current score, then you need to name the display object something different.  Perhaps you should try:
 

local bestscoreText = display.newText (bestscore, 300, 0, nil, 30)

Rob

Thank you, error solved {:

If bestScore was in a function how could I use that same value out side of the function? As well as a for loop and while loop. Could you show me in example code?

Normally with a function, to get a value out of it, you have to “return” it.

local function doSomething()

      local myVar = 10

      return myVar

end

local myVar = doSomething()

print( myVar ) – print’s 10…

Thank  you that helps allot. Should I always make it local?

How would I make the code below work? It seems as though it should but it’s not updating delay. I’m trying to make my game update the rate of spawning based on the amount of time the game has been being played. 

newLevel  = false  

function spawnGreen()

gameTime = system.getTimer( )

print( gameTime )

if(newLevel == false) then

delay = 1000

print( newLevel )

end 

print( delay )

if(spawned == true) then  – Since Spawned is true we will : 

–print(spawned)

greenLight.name = greenLight — Give image a name 

greenLight.x = display.contentCenterX + columns[mRand(4)] — Randomly choos a column

greenLight.y = display.contentCenterY/ rows[mRand(4)] — Randomly choose a row 

greenLightY = math.round(greenLight.y) – This is it’s Y position rounded , don’t want to work with insanely high numbers 

greenLightX = math.round(greenLight.x) – This is it’s X position rounded , don’t want to work with insanely high numbers 

–[[ The code below could possibly be the answer to the overlaping spawning problem with MOh2 

local greenLightCX , greenLightCY =  greenLight:localToContent( 0, 0 )

greenLightYActualPosition = mRound(greenLightCX)

greenLightXActualPosition  = mRound(greenLightCY)

–print( "The greenLights center position in screen coordinates: ", x, y )

x = 0

y = 0

prevX = greenLightXActualPosition

prevY = greenLightYActualPosition 

]]

end 

if gameTime > 2000 and gameTime < 6000 then

newLevel = true 

–print(system.getTimer( ))

delay = 500

print( newLevel )

print( “This should work” )

end 

if gameTime > 6000 then

newLevel = true 

delay = 100

print( newLevel )

print( “This should work as well” )

end 

print( score )

return delay

end

local delay = spawnGreen()

spawner = timer.performWithDelay( delay, spawnGreen, -1 ) 

If bestScore was in a function how could I use that same value out side of the function? As well as a for loop and while loop. Could you show me in example code?

Normally with a function, to get a value out of it, you have to “return” it.

local function doSomething()

      local myVar = 10

      return myVar

end

local myVar = doSomething()

print( myVar ) – print’s 10…

Thank  you that helps allot. Should I always make it local?

How would I make the code below work? It seems as though it should but it’s not updating delay. I’m trying to make my game update the rate of spawning based on the amount of time the game has been being played. 

newLevel  = false  

function spawnGreen()

gameTime = system.getTimer( )

print( gameTime )

if(newLevel == false) then

delay = 1000

print( newLevel )

end 

print( delay )

if(spawned == true) then  – Since Spawned is true we will : 

–print(spawned)

greenLight.name = greenLight — Give image a name 

greenLight.x = display.contentCenterX + columns[mRand(4)] — Randomly choos a column

greenLight.y = display.contentCenterY/ rows[mRand(4)] — Randomly choose a row 

greenLightY = math.round(greenLight.y) – This is it’s Y position rounded , don’t want to work with insanely high numbers 

greenLightX = math.round(greenLight.x) – This is it’s X position rounded , don’t want to work with insanely high numbers 

–[[ The code below could possibly be the answer to the overlaping spawning problem with MOh2 

local greenLightCX , greenLightCY =  greenLight:localToContent( 0, 0 )

greenLightYActualPosition = mRound(greenLightCX)

greenLightXActualPosition  = mRound(greenLightCY)

–print( "The greenLights center position in screen coordinates: ", x, y )

x = 0

y = 0

prevX = greenLightXActualPosition

prevY = greenLightYActualPosition 

]]

end 

if gameTime > 2000 and gameTime < 6000 then

newLevel = true 

–print(system.getTimer( ))

delay = 500

print( newLevel )

print( “This should work” )

end 

if gameTime > 6000 then

newLevel = true 

delay = 100

print( newLevel )

print( “This should work as well” )

end 

print( score )

return delay

end

local delay = spawnGreen()

spawner = timer.performWithDelay( delay, spawnGreen, -1 )