[SCRIPT] RandInt and Switch Statements. - Printable Version +- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum) +-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html) +--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html) +---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html) +---- Thread: [SCRIPT] RandInt and Switch Statements. (/thread-21152.html) |
RandInt and Switch Statements. - PutraenusAlivius - 04-13-2013 Hello everyone! I got a script problem (well, two of them.). I saw RandInt's and thought "Why not make a switch statement about it?". This was a great idea until i realized switch statements and RandInt's are hard. How do i use them? Help is highly appreciated. Sincerely,
JustAnotherPlayer RE: RandInt and Switch Statements. - ClayPigeon - 04-13-2013 Code: switch(RandInt(min, max)) It's quite easy really. Change min and max with your own values, of course. Switch the case values with the values you want to check (1, 3, 4.56, hope you got me) RE: RandInt and Switch Statements. - PutraenusAlivius - 04-13-2013 (04-13-2013, 02:02 PM)ClayPigeon Wrote: Like this? Code: switch(RandInt(1, 3)) RE: RandInt and Switch Statements. - VeNoMzTeamHysterical - 04-13-2013 OMG JustAnotherPlayer does not know something its a wonder Hope its working for you now i'm not so far with scripting yet xD RE: RandInt and Switch Statements. - NaxEla - 04-13-2013 (04-13-2013, 02:02 PM)ClayPigeon Wrote: You aren't actually supposed to have those curly brackets after each case. It will cause an error. RE: RandInt and Switch Statements. - Kreekakon - 04-13-2013 (04-13-2013, 05:08 PM)NaxEla Wrote: You aren't actually supposed to have those curly brackets after each case. It will cause an error.Yes you can, however you have to remember to add break; at the end of each one like this: case min: { //do stuff break; } case max: { //do stuff break; } RE: RandInt and Switch Statements. - NaxEla - 04-13-2013 Huh, I never knew that. When I use a switch statement, I do something like this Code: int x = RandInt(1, 3); Well, everyday's a school day! RE: RandInt and Switch Statements. - Kreekakon - 04-13-2013 Yeah I think your way should work fine too. Personally I like mine better because I'm used to organizing "script chunks" inside brackets. Well it comes down to habits, and whatever you prefer I suppose! RE: RandInt and Switch Statements. - TheGreatCthulhu - 04-13-2013 You can read about the switch statement in more detail here. The reason that there has to be a break; at the end of each case is because (1) it is possible to chain several cases together by omiting the "break", and (2) traditionally, in most other programming languages which use a similar syntax (code looks similar to this), there's no { and } surrounding each case. So, it is possible to do this: int x = RandInt(1, 3) PHP Code: switch(x) While this is a perfectly legitimate thing to do, it happens more often by accident, when it causes your code to misbehave, and if that happens it's somewhat hard to track down the bug and figure out why the script isn't working the way it should. So, as a rule of thumb, always double check if all the breaks are there; in fact, after each case, put the break first, before you type in anything else. Also, when two or more cases need to do the same thing, people usually extract the common code into a separate function, and then simply make a call to that function from each case, rather than letting them fall through (chain them). About enclosing the cases in code blocks (between { and }): this normally isn't done, and people who have used AngelScript (the language used for Amnesia scripting), or are familiar with other languages like C, C++, C# or Java might find your code easier to read if you use the "standard" way (if they want to learn Amnesia scripting by checking out your CS or FC); however, if you prefer enclosing the cases in {}, then go for it, is not wrong or anything. Just be consistent about it. |