SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 03/04/2020, 11:23 AM   #1
Bect
Big Clucker
 
Bect's Avatar
 
Join Date: Apr 2011
Posts: 198
Reputation: 1
Default A little help with " switch"

I want to change this, " if "

Code:
public OnDynamicObjectMoved(objectid)
{
    if (objectid == TheGate)
    {
        if (TGate == TheG_Closing)
        {
            TGate = TheG_Closed;
        }
        else if (TGate == TheG_Opening)
        {
            TGate = TheG_Opened;
        }
    }
    return 1;
}
into "switch"
Code:
public OnDynamicObjectMoved(objectid)
{
    switch(objectid == TheGate)
    {
        case TGate == TheG_Closing:TGate = TheG_Closed;  // error line 1
 	case TGate == TheG_Opening:TGate = TheG_Opened; // error line 2
    }
    return 1;
}
but I have 2 erros:
Code:
error 008: must be a constant expression; assumed zero
error 008: must be a constant expression; assumed zero
Bect is offline   Reply With Quote
Old 03/04/2020, 11:28 AM   #2
LazzyBoy
Big Clucker
 
Join Date: Mar 2017
Posts: 191
Reputation: 15
Default Re: A little help with " switch"

switch(objectid == TheGate)
{
case TGate == TheG_Closing
{
TGate = TheG_Closed;
}
case TGate == TheG_Opening
{
TGate = TheG_Opened;
}
}

Try like this , i wrote it on phone
__________________
LazzyBoy is offline   Reply With Quote
Old 03/04/2020, 11:31 AM   #3
Bect
Big Clucker
 
Bect's Avatar
 
Join Date: Apr 2011
Posts: 198
Reputation: 1
Default Re: A little help with " switch"

The same !
Bect is offline   Reply With Quote
Old 03/04/2020, 11:35 AM   #4
Adamoneoone
Huge Clucker
 
Join Date: Sep 2014
Posts: 468
Reputation: 35
Default Re: A little help with " switch"

Switch only works when cases are integers as far as I'm aware
BTW your syntax doesn't look correct:
pawn Code:
public OnDynamicObjectMoved(objectid)
{
    switch(objectid)
    {
        case TheGate: { do your stuff , perhaps another switch inside?}
        case somethingelse: {}
    }
    return 1;
}
^^ the code above won't work AFAIK, it's just an example

working example taken from the wiki
pawn Code:
new
    a = 5;
switch (a)
{
    case 1:
    {
    }
    case 2:
    {
    }
    case 5:
    {
    }
    default:
    {
    }
}
Adamoneoone is online now   Reply With Quote
Old 03/04/2020, 11:38 AM   #5
jasperschellekens
High-roller
 
jasperschellekens's Avatar
 
Join Date: Dec 2016
Location: Holland
Posts: 1,014
Reputation: 92
Default Re: A little help with " switch"

Adamoneoone is right, you can only use integers.

PHP Code:
switch(integer)
{
    case 
0// integer was 0
    
case 1// integer was 1
    
default: // same as else on an if statement

__________________
Coming soon (6-1-2020):

Undead Wasteland Roleplay - The year is 1996. San Andreas is a wasteland ruled by the undead. the once so glorius landscape of San Andreas, is now a shattered decayed place that is only a vague memory of the world as we used to know it.
Official Live Beta Release Date: 1st of June, 2020
Discord - Website
jasperschellekens is offline   Reply With Quote
Old 03/04/2020, 12:00 PM   #6
Bect
Big Clucker
 
Bect's Avatar
 
Join Date: Apr 2011
Posts: 198
Reputation: 1
Default Re: A little help with " switch"

Yes, but " TheGate " and the others definition are numbers.

#define TheG_Closing 8
#define TheG_Opening 9
Bect is offline   Reply With Quote
Old 03/04/2020, 12:05 PM   #7
Adamoneoone
Huge Clucker
 
Join Date: Sep 2014
Posts: 468
Reputation: 35
Default Re: A little help with " switch"

pawn Code:
public OnDynamicObjectMoved(objectid)
{
    if(objectid == TheGate)
    {
        switch(objectid)
        {
            case 8: { TGate = TheG_Closed; }
            case 9: { TGate = TheG_Opened; }
        }
    }
    return 1;
}
Adamoneoone is online now   Reply With Quote
Old 03/04/2020, 01:40 PM   #8
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,229
Reputation: 205
Default Re: A little help with " switch"

The question is Why?

Do you think that changing if statement to switch statement will make a significant difference or make your code faster? "Premature optimization is the root of all evil"

switch can only be used with constant values. Assigning object ID returned by a function is not a constant value.
__________________
Calisthenics is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error: undefine symbol "j" and expected token ";" but found "]" JEkvall95 Scripting Help 9 27/07/2013 10:09 AM
5 Dialog errors - Input line too long, undefined symbol "t","is" and "only" Deal-or-die Scripting Help 6 01/05/2012 12:08 PM
Admin script requires "ReturnUser", "strtok" and "IsNumeric" Rubennnnn Help Archive 1 02/12/2009 06:08 PM
"Bots", "AI", "NPCs"... Computer-controlled pedestrians and traffic! ssflsnoob General 2 23/11/2008 06:06 PM


All times are GMT. The time now is 09:14 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.