AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved possibly unintended assignment (https://forums.alliedmods.net/showthread.php?t=337520)

kww 04-25-2022 13:51

possibly unintended assignment
 
I have this piece of code
PHP Code:

static anim

if(anim g_models[index][draw2])
{
    switch(
random_num(1DRAW2_CHANCE))
    {
        case 
DRAW2_CHANCEplay_anim(idanim)
        default: 
play_anim(id3)
    }


I like how it looks. It even compiles but compile.exe throws me a warn 211: possibly unintended assignment.
Do I have to change it (to code below) or not?
PHP Code:

static animanim g_models[index][draw2]

if(
anim)
{
    switch(
random_num(1DRAW2_CHANCE))
    {
        case 
DRAW2_CHANCEplay_anim(idanim)
        default: 
play_anim(id3)
    }



HamletEagle 04-25-2022 14:13

Re: possibly unintended assignment
 
Use () around the expression.

kww 04-25-2022 18:08

Re: possibly unintended assignment
 
Quote:

Originally Posted by HamletEagle (Post 2777918)
Use () around the expression.

which one? Don't understand

fysiks 04-25-2022 18:55

Re: possibly unintended assignment
 
The if statement expects a conditional expression which generally is an expression to compare two or more values. A common conditional expression is to compare equality of two values using the "==" comparison symbol. So, when you try to assign a value to a variable instead of doing a typical conditional expression, it thinks that you might have mistakenly forgot to add the second equal sign.

So, to suppress the warning, you need to put your variable assignment in parentheses so that it tells the compiler that you are intentionally doing a variable assignment instead of having a typo in your conditional expression.

Example:
PHP Code:

static anim

if( (anim g_models[index][draw2]) )
{
    switch(
random_num(1DRAW2_CHANCE))
    {
        case 
DRAW2_CHANCEplay_anim(idanim)
        default: 
play_anim(id3)
    }



kww 04-26-2022 01:37

Re: possibly unintended assignment
 
Quote:

Originally Posted by fysiks (Post 2777946)
The if statement expects a conditional expression which generally is an expression to compare two or more values. A common conditional expression is to compare equality of two values using the "==" comparison symbol. So, when you try to assign a value to a variable instead of doing a typical conditional expression, it thinks that you might have mistakenly forgot to add the second equal sign.

So, to suppress the warning, you need to put your variable assignment in parentheses so that it tells the compiler that you are intentionally doing a variable assignment instead of having a typo in your conditional expression.

Example:
PHP Code:

static anim

if( (anim g_models[index][draw2]) )
{
    switch(
random_num(1DRAW2_CHANCE))
    {
        case 
DRAW2_CHANCEplay_anim(idanim)
        default: 
play_anim(id3)
    }



That makes sense. Thanks


All times are GMT -4. The time now is 21:22.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.