Jump to content
Sign in to follow this  
halex1316

Use of Conditionals for "case" in switch do arguments

Recommended Posts

Hello,

 

I'm back again with another question!

I've been working on building up my skills in scripting and I've hit a roadblock. The end result is a bottom-up script built by myself to spawn loot (cliche, I know). I don't want to take someone else's code - this is kind of a labor of love that I want to go through in order to learn.

 

For this part, I'm trying to create a conditional that will determine what is spawned. I've gone through the route of creating a mess of nested if ( ) then {} else {}; nonsense, and this is not how I want to do it.

The script I've created looks like this (just know that it does not work, and I realize this) :

_randomNumber = 1000;
_randomNumber = (floor(random _randomNumber));
_case = {};
_greaterThan = {if (_randomNumber > 500) then {_case = 1};};
_lessThan = {if (_randomNumber <= 500) then {_case = 2};};
_infoType = {};

switch (_case) do

{
	case 1: 	{hint format ["%1",_case];}; 
	case 2: 	{hint format ["%1",_case];}; 

};
sleep 5;
hint "Script is Over";

I've kind of drawn out the logical way I want it to do things.

 

If the number is greater than 500, do the thing that I've defined as the case for things that are greater than 500. But, it seems that case only uses a string or a numerical value... not a range of values, like I might use to determine probability.

 

I don't know exactly how to handle _case (in this example) in order to make it change based on the value of the random number being generated. I know I've got to put one of the _greaterThan or _lessThan into _case, but I'm not sure about the best way to do it.

 

Does anyone have any suggestions? I've looked at the Switch Do wiki page, and checked out the Switch Type page but there was nothing listed there.

 

The code here might not even be what is necessary to do the thing I'm trying to do, but I hope that the way I've written the code shows my intention.

 

Any help is appreciated!

Share this post


Link to post
Share on other sites
_randomNumber = floor random 1000;

switch (true) do
{	case (_randomNumber > 500):  {hint "more than 500"};
	case (_randomNumber < 500):  {hint "less than 500"};
	case (_randomNumber == 500): {hint "exactly 500"};
};

 

  • Like 1

Share this post


Link to post
Share on other sites
_greaterThan = {if (_randomNumber > 500) then {_case = 1};};

Never actually runs this code, so _case = {}, which breaks the switch as you compare it with numbers.

 

Your code fixed:

_randomNumber = 1000;
_randomNumber = floor random _randomNumber;
_case = {};
if (_randomNumber > 500) then {_case = 1};
if (_randomNumber <= 500) then {_case = 2};
_infoType = {};

switch (_case) do {
	case 1: 	{hint format ["%1",_case];}; 
	case 2: 	{hint format ["%1",_case];}; 
};
sleep 5;
hint "Script is Over";

Though Lucullus' script is cleaner.

  • Thanks 1

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×