In layman's terms - case, What is it's purpose
#1
Posté 22 avril 2010 - 10:38
And, is it always used with a "switch?"
#2
Posté 22 avril 2010 - 10:52
example:
dice thrown: result is 3
switch looks at what the result of the dice throw is and compares it to the different cases--->
switch(nDiceResult)
case 1
{ this code is not executed }
case 2
{this code is not executed}
case 3
{ this code is executed}
case 4
{this code is not executed}
case 5
{this code is not executed}
case 6
{this code is not executed}
Modifié par gordonbrown82, 22 avril 2010 - 11:01 .
#3
Posté 22 avril 2010 - 11:56
The switch-case construct is one of the selection constructs in dascript (if-else and ?: being the others). It is slightly more limited than if-else as the cases must be integer values (ints) but, does allow for easy fall through logic and, in some circumstances, can make your code cleaner and easier to read/maintain.
Normally you expect it to be written as:
switch(nValue)
{
case 1: sValue = "one"; break;
case 2: sValue = "two"; break;
default: sValue = "unknown";
}or
switch(nValue)
{
case 1:
sValue = "one";
break;
case 2:
sValue = "two";
break;
default:
sValue = "unknown";
}You can add extra braces in to make it look nice (I do):
switch(nValue)
{
case 1:
{
sValue = "one";
break;
}
case 2:
{
sValue = "two";
break;
}
default:
{
sValue = "unknown";
}
}However these braces do not control the flow (as they do in, for example, if-else). The break statement is king in this regard. Without a break statement one case will fall through to the next. This feature can be very useful when you need a compound effect.
An example might be if you were giving some starting gear according to level or an attribute. Lets say at level 1 the player gets item x; at level 2 they get items x and y and at level 3 and above they get x, y and z. You could script this just as I've written:
switch(nLevel)
{
case 1:
{
GiveItem(oHero, "x");
break;
}
case 2:
{
GiveItem(oHero, "x");
GiveItem(oHero, "y");
break;
}
default:
{
GiveItem(oHero, "x");
GiveItem(oHero, "y");
GiveItem(oHero, "z");
}
}However as you can see it is not very pretty so, using the fall through logic, we can rewrite it as:
// limit the level to 3
if(nLevel > 3)
{
nLevel = 3;
}
// give gear: using fall through logic
switch(nLevel)
{
case 3:
{
GiveItem(oHero, "z");
}
case 2:
{
GiveItem(oHero, "y");
}
case 1:
{
GiveItem(oHero, "x");
}
}Note that we've reversed the order of the cases and removed the breaks. This allows a level 2 to get item y and then fall through to case for a level 1 and also get item x. We can make this code even neater by removing the braces:
// limit the level to 3
if(nLevel > 3)
{
nLevel = 3;
}
// give gear: using fall through logic
switch(nLevel)
{
case 3: GiveItem(oHero, "z");
case 2: GiveItem(oHero, "y");
case 1: GiveItem(oHero, "x");
}
Modifié par Sunjammer, 22 avril 2010 - 11:59 .
#4
Posté 23 avril 2010 - 12:01
#5
Posté 23 avril 2010 - 06:26
Nudge, nudge, wink, wink
#6
Posté 23 avril 2010 - 06:47
#7
Posté 23 avril 2010 - 07:03
#8
Posté 23 avril 2010 - 08:08
The common useage in DA is to branch an event script based on which event was passed, or to branch a plot script based on which flag was passed.
The alternative is to just use a lot of if/else if statement. The switch is generaly easier to read however, and in most programing languages is a lot faster to run if you have a large number of cases becuase it can jump to the target case rather than checking the if statement one at a time.
#9
Posté 23 avril 2010 - 10:42
#10
Posté 24 avril 2010 - 01:35
#11
Posté 24 avril 2010 - 02:38
However I would tend to agree that putting it on the wiki would be redundant but only because I will be writing it up for the Lexicon's keyword articles at some point.
Modifié par Sunjammer, 24 avril 2010 - 02:49 .
#12
Posté 24 avril 2010 - 02:47
But does it run faster in dascript?DavidSims wrote...
... in most programing languages is a lot faster to run if you have a large number of cases becuase it can jump to the target case rather than checking the if statement one at a time.
#13
Posté 24 avril 2010 - 02:48
Modifié par Sunjammer, 24 avril 2010 - 02:48 .
#14
Posté 24 avril 2010 - 03:33
Sunjammer wrote...
But does it run faster in dascript?DavidSims wrote...
... in most programing languages is a lot faster to run if you have a large number of cases becuase it can jump to the target case rather than checking the if statement one at a time.
I honestly don't know for sure, but I doubt it. Even in C you have to have a pretty big switch before it makes an appreciable difference. I think compilers generaly translate small switch statements into if/else if blocks anyway. It wouldn't suprise me if dascript did that translation for all switch statements.
If all your script is doing is integer comparisons, you'd have to be doing a pretty extreem number to notice a performance difference compared to the base overhead of loading and running any script. I don't think the the switch is going to matter. When scripts slow the game down, it tends to be an excesive number of different scripts running, an excesive number of string opperations or the script causing the game engine to do something proccessor intensive or accessing data which isn't in memory.
#15
Posté 24 avril 2010 - 04:42
#16
Posté 24 avril 2010 - 04:43
#17
Posté 24 avril 2010 - 01:24
As I understand it, this is exactly what the nwscript compilier did which was why I was asking. Thanks.DavidSims wrote...
... I think compilers generaly translate small switch statements into if/else if blocks anyway. It wouldn't suprise me if dascript did that translation for all switch statements.
#18
Posté 25 avril 2010 - 02:38
"However I would tend to agree that putting it on the wiki would be redundant but only because I will be writing it up for the Lexicon's keyword articles at some point."
Good, that's the kind of help I need. Thanks sj.
#19
Posté 26 avril 2010 - 01:47





Retour en haut






