Jump to content
Sign in to follow this  
demon cleaner

Control structure / data type weirdness

Recommended Posts

Hi!

While working on some scripting I came across some weirdness when using the switch control structure on the STRING data type. I'm using setVariable to store a variable in a game logic. The data type I'm storing is STRING I've verified that.

_object setVariable [_var,_val,_sync];

To test the data type I'm retrieving the variable value like this:

_tN = _object getVariable _varName;

Checking on the data type I'm getting what I expect.

_tst = typeName _tN;

At this point _tst is of data type STRING which is expectable and it contains whatever STRING I've stored earlier. So far so good but now I'm using a switch control structure like this:

_tst = switch (_tN) do {
   case "x" : {"A"};
   case "y" : {"B"};
};

At this point _tst contains TRUE and is of type BOOL instead of containing A or B of type STRING. Testing the contents of _tN beforhand reveals it contains either x or y of type STRING. Using the same control structure on a different data type gives me the expectable result.

_tst = switch (_tN) do {
   case "x" : {[1,1]};
   case "y" : {[2,2]};
};

At this point _tst is of type ARRAY and contains either [1,1] or [2,2] depending on the contents of _tN. DOH! Ideas anyone ?

Share this post


Link to post
Share on other sites

OK I kind of solved it but the weirdness remains. I've now exchanged the STRING type values for NUM type values (e.g 0 and 1 instead of x and y):

_tst = switch (_tN) do {
 case 0 : {"A"};
 case 1 : {"B"};
};

This appears to work as the above results in _tst either being "A" or "B" depending on the value of _tN. Still I don't get why type STRING comparisons will result in BOOL ...

Share this post


Link to post
Share on other sites

have you tried:

_tst = [];
switch (_tN) do {
 case "x": {_tst = [1,1]};
 case "y": {_tst = [2,2]};
};

it is your guess that the Switch type return will be updated to your variable (at least looking at the wiki), Well example 2 supports your syntax but try the above anyway...

Edited by gammadust

Share this post


Link to post
Share on other sites

Switch case is case-sensitive if I recall correctly.

Share this post


Link to post
Share on other sites

i've now tested:

x = "x"; tst = switch (x) do {case "x": {"A"}; case "y": {"B"};}; // typeName tst = "STRING" tst = "A"

x = ""; tst = switch (x) do {case "x": {"A"}; case "y": {"B"};}; // typeName tst = "BOOL" tst = true

x = []; tst = switch (x) do {case "x": {"A"}; case "y": {"B"};}; // typeName tst = "BOOL" tst = true

so it looks like somehow your _tN variable is lost on the way or is not found. Since Switch does not fail if no case is found it returns the success of the switch structure instead (i assume)

Share this post


Link to post
Share on other sites
Switch case is case-sensitive if I recall correctly.

Interesting since http://community.bistudio.com/wiki/switch doesn't mention it. Neither does http://community.bistudio.com/wiki/Control_Structures. I know that http://community.bistudio.com/wiki/getVariable is case sensitive. In any case I made sure to use the correct case converting all strings to lower case. Still no difference.

---------- Post added at 08:47 ---------- Previous post was at 08:34 ----------

x = ""; tst = switch (x) do {case "x": {"A"}; case "y": {"B"};}; // typeName tst = "BOOL" tst = true
x = []; tst = switch (x) do {case "x": {"A"}; case "y": {"B"};}; // typeName tst = "BOOL" tst = true

so it looks like somehow your _tN variable is lost on the way or is not found. Since Switch does not fail if no case is found it returns the success of the switch structure instead (i assume)

The explanation seems logical though http://community.bistudio.com/wiki/Control_Structures and http://community.bistudio.com/wiki/switch don't mention it. I cannot reproduce the behaviour in any other way than what you did in the above example and this is weird because I'm doing a typeName check for _tN right before the switch control structure and the variable at that point is of type STRING and contains x or y....

_tst = typeName _tN; // STRING
hint format ["_tst: %1",_tst]; // x || y
_tst = switch (_tN) do {
 case "x" : {"A"};
 case "y" : {"B"};
};
_tst = typeName _tN; // BOOL
hint format ["_tst: %1",_tst]; // TRUE

---------- Post added at 09:11 ---------- Previous post was at 08:47 ----------

Hmmm I guess I finally understood what the default case can do for me ...

_tst = switch (_tN) do {
 case "x" : {"A"};
 case "y" : {"B"};  
 default : {"C"}; // <-- Use me to figure things out quicker
};

Share this post


Link to post
Share on other sites

It actually mentions it on the discussion page for switch.

And I was about to mention the default case, I see you decided to use it already. As far as I know you shouldn't use ":" for the default case.

_tst = switch (_tN) do {
 case "x" : {"A"};
 case "y" : {"B"};  
 default  {"C"}; 
};

But to be fair, I'm not sure it's important.

Edited by BlackMamb
can't type for shit.

Share this post


Link to post
Share on other sites

_tst = switch (_tN) do {
 case "x" : {"A"};
 case "y" : {"B"};  
 default  {"C"}; 
};

You are correct. The default case goes without ":". Thanks for the link to the discussion page.

Share this post


Link to post
Share on other sites

_tst = typeName _tN;

At this point _tst is of data type STRING which is expectable and it contains whatever STRING I've stored earlier.

_tst is of data type STRING (yes because typeName returns a string), not exacly whatever string you stored earlier. Shouldn't you be verifying the contents of _tN itself in addition?

what does _tN contain immediatly before the switch structure?

Another possibility:

GameLogic and SetVariable

Anyone managed to set a variable on a GameLogic ? After all I create it using:

"GameLogic" CreateVehicle

- BarmyArmy

It's not how you create an entity that counts. It's what it's derived from. You cant use setVariable, simple because it's not considered a vehicle. Yes, it's as pedantic as that. Logics are more akin to infantry than trucks, so your out of luck. You have to use other methods. UNN 22:34, 20 April 2007 (CEST)

wiki discussion on setVariable. It is an old comment but might still be relevant.

Share this post


Link to post
Share on other sites
_tst is of data type STRING (yes because typeName returns a string), not exacly whatever string you stored earlier. Shouldn't you be verifying the contents of _tN itself in addition?

what does _tN contain immediatly before the switch structure?

Another possibility:

wiki discussion on setVariable. It is an old comment but might still be relevant.

That's exactly what I'm doing. I'm checking the contents of _tN right before the switch control structure (and with right before I mean no other code lines between the typeName and content check of _tN and the switch control structure). To illustrate it: (NB: I've converted everything to lower case values just to not run into case sensivity issues)

_res = format ["varValue:%1,varType:%2",_tn,typeName _tn]; <- results in _res=="varValue:x,varType:STRING"
_tst = switch (_tn) do {
 case "x" : {"a"};
 case "y" : {"b"};
};
_res = format ["varValue:%1,varType:%2",_tn,typeName _tn]; <- results in _res=="varValue:TRUE,varType:BOOL"

I'll spend some more time on this later today. Thanks by the way for the link to the discussion pages. I honestly never noticed the tab... :banghead:

Edited by Demon Cleaner

Share this post


Link to post
Share on other sites

Hmmm. As much as I'm surprised _tn turns into a boolean (never saw that coming, but it could kinda make sense), shouldn't you be testing _tst instead? Or is that a typo?

I tried to reproduce all that but can't, switch case is working just fine for me.

Share this post


Link to post
Share on other sites

Why is this discussion still going if switch is case sensitive? Have you tried it with case matches yet?

Share this post


Link to post
Share on other sites

Use switch true:

_tn = _this select 0;

_tst = switch (true) do {
 case (_tn == "x") : {"a"};
 case (_tn == "y") : {"b"};
};

player sideChat format ["Value is %1 from %2", _tst, _tn];

Share this post


Link to post
Share on other sites

So the original code definitely works for me. _tn doesn't change to a boolean unless there is indeed a case issue.

Now that we're there though, I'll just remind you that a if check is faster than a switch case. So unless you have a very large number of cases to check and it becomes impossible to follow an if then else architecture, you're better of with if then anyway.

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  

×