UGX-Mods

Call of Duty 5: World at War => Help Desk => Scripting => Topic started by: Ege115 on May 11, 2014, 09:16:33 am

Title: case statement
Post by: Ege115 on May 11, 2014, 09:16:33 am
I am just playing around with the "case" statement. I have not used this statement before so this is my first time. And I found this statement really useful. :D

Anyway, I am using this script which has a case statement.
Code Snippet
Plaintext
main
{
place1 = getEnt("place1","targetname");
place1 thread place_weapons("m9");
}

place_weapons(weap)
{
ice_model = GetEnt("ice_model","targetname");

while(1)
{
while(isDefined(self))
{
players = get_players();
for (i = 0; i < players.size; i++)
{
ice_model hide();
self waittill("trigger", player);

currentWeapon = players[i] GetCurrentWeapon();
if(currentWeapon == weap)
{
self trigger_off();

level.times_placed++;

if(level.times_placed == 1)
{
case "m9":
players[i] takeweapon("m9");
ice_model show();
ice_model setmodel("zombie_3rd_perk_bottle_doubletap");
self waittill("trigger", player);
players[i] giveweapon("m9_upgraded");
ice_model hide();
self trigger_on();
break;
                                }
                    }
          }
}
}
}
But when I am loading my map, I get a script compile error saying, "illegal case statement". What did I do wrong with the case statement?

Thanks in advance.
Title: Re: case statement
Post by: YaPh1l on May 11, 2014, 09:52:01 am
case statements are only allowed within a switch block.

- Phil.
Title: Re: case statement
Post by: Ege115 on May 11, 2014, 10:17:14 am
case statements are only allowed within a switch block.

- Phil.
Uhm, a what? So what exactly is it I need to change at the case statement? :o
Title: Re: case statement
Post by: daedra descent on May 11, 2014, 02:05:36 pm
Like this:

Code Snippet
Plaintext
main
{
place1 = getEnt("place1","targetname");
place1 thread place_weapons("m9");
}

place_weapons(weap)
{
ice_model = GetEnt("ice_model","targetname");

while(1)
{
while(isDefined(self))
{
players = get_players();
for (i = 0; i < players.size; i++)
{
ice_model hide();
self waittill("trigger", player);

currentWeapon = players[i] GetCurrentWeapon();
if(currentWeapon == weap)
{
self trigger_off();

level.times_placed++;

if(level.times_placed == 1)
{
switch(currentweapon)
{
case "m9":
players[i] takeweapon("m9");
ice_model show();
ice_model setmodel("zombie_3rd_perk_bottle_doubletap");
self waittill("trigger", player);
players[i] giveweapon("m9_upgraded");
ice_model hide();
self trigger_on();
break;
                                }
}
                    }
          }
}
}
}

EDIT: Really i'd just suggest an if statement instead of a case statement for what your trying to do. Case statements are primarily used as a way to return values, not do lengthy code.
Title: Re: case statement
Post by: Ege115 on May 11, 2014, 03:38:40 pm
Like this:

Code Snippet
Plaintext
main
{
place1 = getEnt("place1","targetname");
place1 thread place_weapons("m9");
}

place_weapons(weap)
{
ice_model = GetEnt("ice_model","targetname");

while(1)
{
while(isDefined(self))
{
players = get_players();
for (i = 0; i < players.size; i++)
{
ice_model hide();
self waittill("trigger", player);

currentWeapon = players[i] GetCurrentWeapon();
if(currentWeapon == weap)
{
self trigger_off();

level.times_placed++;

if(level.times_placed == 1)
{
switch(currentweapon)
{
case "m9":
players[i] takeweapon("m9");
ice_model show();
ice_model setmodel("zombie_3rd_perk_bottle_doubletap");
self waittill("trigger", player);
players[i] giveweapon("m9_upgraded");
ice_model hide();
self trigger_on();
break;
                                }
}
                    }
          }
}
}
}

EDIT: Really i'd just suggest an if statement instead of a case statement for what your trying to do. Case statements are primarily used as a way to return values, not do lengthy code.
Okey thank you, that seems to have fixed it. Now an another error pops up. The script compile error is saying,
"missing case statement"
And developer 1 says that it is something regarding those first three lines you often have in scripts.
Code Snippet
Plaintext
#include, etc
Is it an another file I need to include there? If so which one?
Title: Re: case statement
Post by: PROxFTW on May 11, 2014, 04:43:31 pm
No, nothing wrong with that more than likely. But make sure you add a default case statement. Most likely why that is happening is there is a value being returned that is not listed down.
Title: Re: case statement
Post by: n123q45 on May 11, 2014, 05:47:13 pm
this is how a case statement is setup
Code Snippet
Plaintext
var = undefined; //the variable has to be set before the actual script

switch(var){ // var is the variable
  case "1": // what the first option the variable could be

  break; // how to end a case
 
  case "2": // what the second option the variable could be

  break;
}

this is used instead of lots of if else statements. you use this when the variable is set before this script. I used undefined to show that it has to be set before.
Title: Re: case statement
Post by: Ege115 on May 11, 2014, 06:01:40 pm
No, nothing wrong with that more than likely. But make sure you add a default case statement. Most likely why that is happening is there is a value being returned that is not listed down.
Hmm, well I still don't know exactly what I need to do, what value? :o I'm a noob.
Title: Re: case statement
Post by: YaPh1l on May 11, 2014, 06:05:35 pm
The value of the variable you "switch over". Although that shouldn't be causing any problems, it just wouldn't execute any case at all.

- Phil.
Title: Re: case statement
Post by: Ege115 on May 11, 2014, 06:17:51 pm
Well, then I have no idea what's wrong. I don't know how I should correct the script myself. D: I can't see anything wrong with it.
Title: Re: case statement
Post by: mrpeanut188 on May 11, 2014, 06:22:00 pm
Well, then I have no idea what's wrong. I don't know how I should correct the script myself. D: I can't see anything wrong with it.
Like said, it 'switches' to the value returned and executes the code. Let's say you have the case for 'm9' and 'thompson'. If you carry any other gun, presumably it'd pop out an error or do nothing. If it IS popping out an error, you have to add a default case so that if it isn't one of the listed guns it's execute the code in the default case.

C# Switch Example (http://msdn.microsoft.com/en-us/library/06tc147t.aspx) (Top box)
Title: Re: case statement
Post by: Ege115 on May 11, 2014, 06:50:14 pm
Like said, it 'switches' to the value returned and executes the code. Let's say you have the case for 'm9' and 'thompson'. If you carry any other gun, presumably it'd pop out an error or do nothing. If it IS popping out an error, you have to add a default case so that if it isn't one of the listed guns it's execute the code in the default case.

C# Switch Example (http://msdn.microsoft.com/en-us/library/06tc147t.aspx) (Top box)
Yay, I added this,
Code Snippet
Plaintext
default:
And the map loads without errors now. But when I hit the triggers, it doesn't take the weapon. (m9)

Here is the updated script if you wan tto see that.
Code Snippet
Plaintext
main
{
place1 = getEnt("place1","targetname");
place1 thread place_weapons("m9");
}

place_weapons(weap)
{
ice_model = GetEnt("ice_model","targetname");

while(1)
{
while(isDefined(self))
{
players = get_players();
for (i = 0; i < players.size; i++)
{
ice_model hide();
self waittill("trigger", player);

currentWeapon = players[i] GetCurrentWeapon();
if(currentWeapon == weap)
{
self trigger_off();

level.times_placed++;

if(level.times_placed == 1)
{
switch(currentweapon)
{
case "m9":
players[i] takeweapon("m9");
ice_model show();
ice_model setmodel("zombie_3rd_perk_bottle_doubletap");
self waittill("trigger", player);
players[i] giveweapon("m9_upgraded");
ice_model hide();
self trigger_on();
break;
                                        default:
                                }
}
                    }
          }
}
}
}
Thanks for the help so far, I am getting closer and closer to get it working. :D
Title: Re: case statement
Post by: mrpeanut188 on May 11, 2014, 06:55:02 pm
Not very good at scripting, but..

You call this
Code Snippet
Plaintext
place1 thread place_weapons("m9");

But the function is this
Code Snippet
Plaintext
place_weapons(weap)

Shouldn't it be
Code Snippet
Plaintext
place1 thread place_weapons(weap);
because it switches to the m9 inside of the function itself?
Title: Re: case statement
Post by: Ege115 on May 11, 2014, 07:23:54 pm
Not very good at scripting, but..

You call this
Code Snippet
Plaintext
place1 thread place_weapons("m9");

But the function is this
Code Snippet
Plaintext
place_weapons(weap)

Shouldn't it be
Code Snippet
Plaintext
place1 thread place_weapons(weap);
because it switches to the m9 inside of the function itself?
Hmm, that can't be the issue as this script works in other cases. This time I am making it with the case statement, but it just doesn't takes the weapon.
Title: Re: case statement
Post by: PROxFTW on May 11, 2014, 07:25:57 pm
Test this
Code Snippet
Plaintext
main()
{
place1 = getEnt( "place1", "targetname" );
place1 thread place_weapons( "m9" );
}

place_weapons( Weap )
{
ice_model = GetEnt( "ice_model","targetname" );

while( 1 )
{
while( isDefined( self ) )
{
ice_model hide();
self waittill( "trigger", player );

CurrWeap = player GetCurrentWeapon();
if( CurrWeap == Weap )
{
self trigger_off();
level.times_placed++;

if( level.times_placed == 1 )
{
switch( CurrWeap )
{
case "m9":
player takeweapon( CurrWeap );
ice_model show();
ice_model setmodel( "zombie_3rd_perk_bottle_doubletap" );
self waittill( "trigger", player );
player giveweapon( CurrWeap + "_upgraded" );
ice_model hide();
self trigger_on();
break;

                        default:
                        break;
                    }
}           
        }
}
}
}
Title: Re: case statement
Post by: jjbradman on May 11, 2014, 10:13:27 pm
dont pass the string "m9" to the other funtion. instead define it inside the other funtion. i got problems with switch in that case where all the values inside the switch block would get executed
Title: Re: case statement
Post by: treminaor on May 12, 2014, 12:21:36 am
i got problems with switch in that case where all the values inside the switch block would get executed
That would be because you didn't place break statements at the end of each case.
Title: Re: case statement
Post by: Ege115 on May 12, 2014, 05:05:59 am
That would be because you didn't place break statements at the end of each case.
More case statements is exactly what I am doing. this is the complete whole script if you want to see that.
And I tried your script PROxFTW, but it didn't take the weapon either. :/
Code Snippet
Plaintext
main()
{
place1 = getEnt( "place1", "targetname" );
        place2 = getEnt( "place2", "targetname" );
        place3 = getEnt( "place3", "targetname" );
        place4 = getEnt( "place4", "targetname" );

place1 thread place_weapons( "m9" );
place2 thread place_weapons( "m40a3" );
place3 thread place_weapons( "aug" );
place4 thread place_weapons( "g36c" );
}

place_weapons(weap)
{
ice_model = GetEnt("ice_model","targetname");
fire_model = GetEnt("fire_model","targetname");
elec_model = GetEnt("elec_model","targetname");
wind_model = GetEnt("wind_model","targetname");

while(1)
{
while(isDefined(self))
{
players = get_players();
for (i = 0; i < players.size; i++)
{
ice_model hide();
fire_model hide();
elec_model hide();
wind_model hide();
self waittill("trigger", player);

currentWeapon = player GetCurrentWeapon();
if(currentWeapon == weap)
{
//self trigger_off();

level.times_placed++;

if(level.times_placed == 1)
{
currentweapon = undefined;

switch(currentweapon)
{
case "m9":
players[i] takeweapon(currentweapon);
ice_model show();
ice_model setmodel("zombie_3rd_perk_bottle_doubletap");
self waittill("trigger", player);
players[i] giveweapon(currentweapon + "_upgraded");
ice_model hide();
break;
default:
break;
}

switch(currentweapon)
{
case "m40a3":
players[i] takeweapon(currentweapon);
fire_model show();
fire_model setmodel("zombie_3rd_perk_bottle_sleight");
self waittill("trigger", player);
players[i] giveweapon(currentweapon + "_upgraded");
fire_model hide();
break;
default:
break;
}

switch(currentweapon)
{
case "aug":
elec_model show();
players[i] takeweapon(currentweapon);
elec_model setmodel("zombie_3rd_perk_bottle_revive");
self waittill("trigger", player);
players[i] giveweapon(currentweapon + "_upgraded");
elec_model hide();
break;
default:
break;
}

switch(currentweapon)
{
case "g36c":
wind_model show();
players[i] takeweapon(currentweapon);
wind_model setmodel("zombie_3rd_perk_bottle_jugg");
self waittill("trigger", player);
players[i] giveweapon(currentweapon + "_upgraded");
wind_model hide();
break;
default:
break;
}
}
}
}
}
}
}
Isn't it wierd that it just WONT work even if the script says that it will take the weapon the player has? :/
Title: Re: case statement
Post by: daedra descent on May 12, 2014, 05:16:59 am
Because your not using the case statement right. it should be something like this:

Code Snippet
Plaintext
switch(whatever)
{
case "whatever1":
// do whatever here
break;
case "whatever2":
// do whatever here
break;
case "whatever3":
// do whatever here
break;
}

again, i really suggest just using an if statement for all of this.

EDIT: Thought i'd make a little example to show why & the difference between the switch and if statements.

Code Snippet
Plaintext
switch_example(example)
{
if(!isdefined(example) )
{
example = "whatever1";
}
switch(example)
{
case "whatever1":
// do whatever here
break;
case "whatever2";
// do whatever here
break;
case "whatever3":
// do whatever here
break;
}
}
if_example(example)
{
if(!isdefined(example) )
{
example = "whatever1";
}

if(example == "whatever1")
{
// do whatever here
}
if(example == "whatever2")
{
// do whatever here
}
if(example == "whatever3")
{
// do whatever here
}
}

The cases only act as a separate if statement for each possibility. The only difference(i think) is that the switch function's different cases can apply to different entities at the same time while if statements can't.
Title: Re: case statement
Post by: treminaor on May 12, 2014, 05:39:00 am
More case statements is exactly what I am doing. this is the complete whole script if you want to see that.
And I tried your script PROxFTW, but it didn't take the weapon either. :/

Isn't it wierd that it just WONT work even if the script says that it will take the weapon the player has? :/
The point of the switch case is that there are multiple outcomes based on the value of the variable you are switching, so I combined all of your cases into one switch statement.

Code Snippet
Plaintext
 
switch(currentweapon)
{
case "m9":
players[i] takeweapon(currentweapon);
ice_model show();
ice_model setmodel("zombie_3rd_perk_bottle_doubletap");
self waittill("trigger", player);
players[i] giveweapon(currentweapon + "_upgraded");
ice_model hide();
break;
case "m40a3":
players[i] takeweapon(currentweapon);
fire_model show();
fire_model setmodel("zombie_3rd_perk_bottle_sleight");
self waittill("trigger", player);
players[i] giveweapon(currentweapon + "_upgraded");
fire_model hide();
break;
case "aug":
elec_model show();
players[i] takeweapon(currentweapon);
elec_model setmodel("zombie_3rd_perk_bottle_revive");
self waittill("trigger", player);
players[i] giveweapon(currentweapon + "_upgraded");
elec_model hide();
break;
case "g36c":
wind_model show();
players[i] takeweapon(currentweapon);
wind_model setmodel("zombie_3rd_perk_bottle_jugg");
self waittill("trigger", player);
players[i] giveweapon(currentweapon + "_upgraded");
wind_model hide();
break;
default:
break;
}
Title: Re: case statement
Post by: Ege115 on May 12, 2014, 04:01:17 pm
Oh, thank you so much it worked! I saw what I did wrong now, lol.
Well, now I have learned how "case" works, now what's next I need to learn in scripting? :D

Thank you all of you, I give you all each +1. ;)
Title: Re: case statement
Post by: n123q45 on May 12, 2014, 07:04:58 pm
the next thing you should learn is arrays