Aller au contenu

Photo

In layman's terms - case, What is it's purpose


18 réponses à ce sujet

#1
Sonmeister

Sonmeister
  • Members
  • 167 messages
The parameter "case" determines what, in a script?

And, is it always used with a "switch?"

#2
gordonbrown82

gordonbrown82
  • Members
  • 544 messages
you throw a dice.. the switch is like a camera that records what the result of the dice throw is and then sends that result to the cases  the case that corresponds with the recorded dice result will execute (ie the code between the {})
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
Sunjammer

Sunjammer
  • Members
  • 925 messages
Actually in that example cases 3 to 6 would be executed because there is no break statement.

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
Sonmeister

Sonmeister
  • Members
  • 167 messages
That definitely helps me understand, thanks, hope this helps some others as well.

#5
BillHoyt

BillHoyt
  • Members
  • 109 messages
This would be really awesome in the wiki.



Nudge, nudge, wink, wink

#6
ChewyGumball

ChewyGumball
  • Members
  • 282 messages
Correct me if I am wrong, but 99% of da script is just C. In which case you might be best looking at a C tutorial or language reference for things like program flow, variable usage/declaration, etc. Certainly the wiki is useful, but there are hundreds if not thousands of references already available on the web and no need to duplicate them.

#7
Craig Graff

Craig Graff
  • Members
  • 608 messages
A lyceum section similar to the NWN Lexicon would be nice, I suspect.

#8
DavidSims

DavidSims
  • BioWare Employees
  • 196 messages
In general, a switch is an effecient way to branch code into many paths based on the value of one variable.



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
Sonmeister

Sonmeister
  • Members
  • 167 messages
FYI, to ChewyGumball, the first place I went to look for an example of "case" was the C programming description and it wasn't nearly as helpful as what I got here. I'm not a programmer so the help in understanding "layman's terms" puts it back into our language from programmer language. I know just enough programming language to be dangerous and hope to become more adept through help here on the forum. I can make some really ugly looking scripts that work from robbing Peter to pay Paul but don't always know why they are working or what statements and functions I could have left out. Part of me being a rookie and a hack programmer, at best.

#10
ChewyGumball

ChewyGumball
  • Members
  • 282 messages
Indeed, which is why this forum exists, however putting it on the wiki is redundant. That was my only point.

#11
Sunjammer

Sunjammer
  • Members
  • 925 messages
None of dascript is C. The basic syntax of dascript is similar to C (and other "curly brace" languages) however there are more than enough differences, including switch statements, to make comparisons to C unhelpful more often as not.

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
Sunjammer

Sunjammer
  • Members
  • 925 messages

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.

But does it run faster in dascript?

#13
Sunjammer

Sunjammer
  • Members
  • 925 messages
Double post.

Modifié par Sunjammer, 24 avril 2010 - 02:48 .


#14
DavidSims

DavidSims
  • BioWare Employees
  • 196 messages

Sunjammer wrote...

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.

But does it run faster in dascript?


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
gordonbrown82

gordonbrown82
  • Members
  • 544 messages
i'm trying to write a scripting guide for people who know nothing about programming and nothing about scripting. i'm unsure if it's possible to make it easier than reading and going through a good c++ tutorial.

#16
gordonbrown82

gordonbrown82
  • Members
  • 544 messages
it might be in one way because you can leave out some stuff like pointers for example.

#17
Sunjammer

Sunjammer
  • Members
  • 925 messages

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.

As I understand it, this is exactly what the nwscript compilier did which was why I was asking. Thanks.

#18
Sonmeister

Sonmeister
  • Members
  • 167 messages
sunjammer wrote:

"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
CID-78

CID-78
  • Members
  • 1 124 messages
dascript and the older nwscript languages are very simple compare to C or C++. and there is many things that actually differ or is missing. it's a very newbiee friendly language you can't screwup that bad. worst you can do is hang the toolset or game. which is rare, it usually only endup not working.