Jump to content

LUA can't tell the difference between bool and int/float


AcidDK

Recommended Posts

Hi.. I'm making a game mode for MTA and I've run into a problem. LUA can't tell the difference between boolean values and integers/float values.

In a function for the event onPlayerDamage I'm comparing the health loss to some float values.. like:

if loss < 10.0 then 
print("loss is under 10") 
end 

This is not an example from my game mode, though this is how I do basically. When the server executes that piece of code I get an error saying that I can't compare a boolean to a number. In some way LUA thinks that the loss parameter is a boolean value, but when I print it in the console it really is a float value, like 0.32452. It makes no sense to me, and I figured there might be some experienced LUA users on this board who has some great work-around for this.

Thanks in advance for your help!

Link to comment

I'm not the only one experiencing this problem.. Here's some snippets of code from the game mode:

function addRednessOnDamage(pID,loss) 
    fadeCamera(pID, false, 1.0, 255, 0, 0) 
    local tmpLoss = tonumber(loss) 
    if 0.0 < tmpLoss < 5.0 then 
        local tmpNumber = 150 
    elseif 5.0 < tmpLoss < 15.0 then 
        local tmpNumber = 250 
    elseif 15.0 < tmpLoss < 40.0 then 
        local tmpNumber = 450 
    elseif 40.0 < tmpLoss then 
        local tmpNumber = 700 
    else 
        local tmpNumber = 150 
    end 
    setTimer(fadeCamera, tmpNumber, 1, pID, true, 0.5) 
end 

function Script_onPlayerDamage(attacker, attackerweapon, bodypart, loss) 
    addRednessOnDamage(source,loss) 
end 
  
addEventHandler("onPlayerDamage", getRootElement(), Script_onPlayerDamage) 

It's supposed to make the camera fade for a longer period of time when losing more health at once.

Link to comment
tonumber(loss) prolly returns false or something. Try this:
outputChatBox( tostring(tonumber(loss)) ) 

And see what it outputs.

The first thing i tried was doing it without the "tonumber" function.. That is why I tried with "tonumber" and it made no difference. I was just too lazy to remove it. I'll try and do as you say anyways :)

EDIT:

Okay here are the results (I printed it instead of putting it in the chatbox):

Printed "5.5999984741211" with print(tostring(tonumber(loss))).

The error is still "attempt to compare boolean with number" on the 28th line which is where I'm comparing loss to a float the first time.

Link to comment
Huh, well that's odd. Can you round the number to be an integer (with math.ceil() or math.floor())? I'm speculating MTA somehow reads it wrong because of the dot in the middle of that number.

I've tried that in another situation just like this one (getDistanceBetweenPoints3D and comparing the same positions gives a value of 0, which it also understands as a boolean), and it didn't do any difference. Either way it would, logically, still be misunderstood by MTA if the float is rounded up/down to 0 or 1 as false or true.

Link to comment

uhrm shouldn't that be..

function addRednessOnDamage(pID,loss) 
    fadeCamera(pID, false, 1.0, 255, 0, 0) 
    local tmpLoss = tonumber(loss) 
[color=#FF0000]    local tmpNumber = nil[/color] 
[color=#FF0000]    if 0.0 < tmpLoss and tmpLoss < 5.0 then[/color] 
        tmpNumber = 150 
[color=#FF0000]    elseif 5.0 < tmpLoss and tmpLoss < 15.0 then[/color] 
        tmpNumber = 250 
[color=#FF0000]    elseif 15.0 < tmpLoss and tmpLoss < 40.0 then[/color] 
        tmpNumber = 450 
    elseif 40.0 < tmpLoss then 
        tmpNumber = 700 
    else 
        tmpNumber = 150 
    end 
    setTimer(fadeCamera, tmpNumber, 1, pID, true, 0.5) 
end 

also when you have local variables inside an 'if' they will be lost once it goes out

Link to comment
uhrm shouldn't that be..

function addRednessOnDamage(pID,loss) 
    fadeCamera(pID, false, 1.0, 255, 0, 0) 
    local tmpLoss = tonumber(loss) 
[color=#FF0000]    local tmpNumber = nil[/color] 
[color=#FF0000]    if 0.0 < tmpLoss and tmpLoss < 5.0 then[/color] 
        tmpNumber = 150 
[color=#FF0000]    elseif 5.0 < tmpLoss and tmpLoss < 15.0 then[/color] 
        tmpNumber = 250 
[color=#FF0000]    elseif 15.0 < tmpLoss and tmpLoss < 40.0 then[/color] 
        tmpNumber = 450 
    elseif 40.0 < tmpLoss then 
        tmpNumber = 700 
    else 
        tmpNumber = 150 
    end 
    setTimer(fadeCamera, tmpNumber, 1, pID, true, 0.5) 
end 

also when you have local variables inside an 'if' they will be lost once it goes out

I'll try and see.. Anyway, the reason why I wrote "if 0.0 < tmpLoss < 5.0" and not "if 0.0 < tmpLoss and tmpLoss < 5.0" is that you can do so in Pawn, C/C++, Visual Basic, PHP etc. I'm already getting tired of LUA's limitations. I'll be back soon with the results.

EDIT:

Okay your solution worked. I'm both happy and disappointed. I'm happy because I now don't have to worry about it anymore and I'm disappointed because LUA sucks ass :mrgreen:

Anyway, thanks for your help mate :wink:

Link to comment
I'll try and see.. Anyway, the reason why I wrote "if 0.0 < tmpLoss < 5.0" and not "if 0.0 < tmpLoss and tmpLoss < 5.0" is that you can do so in Pawn, C/C++, Visual Basic, PHP etc. I'm already getting tired of LUA's limitations. I'll be back soon with the results.

EDIT:

Okay your solution worked. I'm both happy and disappointed. I'm happy because I now don't have to worry about it anymore and I'm disappointed because LUA sucks ass :mrgreen:

Anyway, thanks for your help mate :wink:

tell us some of the limitations and we may be able to help

Link to comment
I'll try and see.. Anyway, the reason why I wrote "if 0.0 < tmpLoss < 5.0" and not "if 0.0 < tmpLoss and tmpLoss < 5.0" is that you can do so in Pawn, C/C++, Visual Basic, PHP etc. I'm already getting tired of LUA's limitations. I'll be back soon with the results.

EDIT:

Okay your solution worked. I'm both happy and disappointed. I'm happy because I now don't have to worry about it anymore and I'm disappointed because LUA sucks ass :mrgreen:

Anyway, thanks for your help mate :wink:

tell us some of the limitations and we may be able to help

Nah you're not the ones working on the LUA language. I'll just have to get used to it.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...