Jump to content
Sign in to follow this  
Spudgunner

Switch-case problem

Recommended Posts

I'm trying to figure out a way to detect civilian type, such as Worker or WorkWoman regardless of if they are 1,2...5. But it looks like you have to test for each type directly. If there is a way, I couldn't find it.

Instead, I'm using switch-case as below, but it never works.

//_pasRnd : random AI

_civClass = [url="http://community.bistudio.com/wiki/typeOf"]typeof [/url]_pasRnd;

switch (_civClass) do {
         case (("Worker1") || ("Worker2") || ("Worker3") || ("Worker4")): { 
               hint "I am a Worker"; 
         };   
         case (("Citizen1") || ("Citizen2") || ("Citizen3") || ("Citizen4")): { 
               hint "I am a citizen of Chernarus"; 
         };
// etc..
         case (("WorkWoman1") || ("WorkWoman2") || ("WorkWoman3") || ("WorkWoman4") || ("WorkWoman5")): { 
               hint "I am a Working girl"; 
         };
         default {
               hint "ERROR";
         }; 
};

Edited by Spudgunner
mistake & make code clear

Share this post


Link to post
Share on other sites
you're looking for isKindOf ;)

That would work with multiple "if" statements but I can't see how it would work for Switch-case. The return from "isKindof" is Boolean (True or False). Unless I have a blind spot - most likely :)

If only "elseIf" were implemented then I would have gone down that route.

isTypeof does return a string like "WorkWoman3" which should match test condition. How would you use "isKindof" in the switch statement?

Edited by Spudgunner

Share this post


Link to post
Share on other sites

don't think that a few cascaded ifs are such bad (ok, lots of parentheses)

but if you need your switch-statement you can do something like

_civClass = typeof _pasRnd;
if (_civClass isKindOf "Worker") then {
   _civClass = "Worker";
};
if ...
switch (...

- it's not beautiful, but it works (I hope so)

Share this post


Link to post
Share on other sites

The problem with if is that there would be many of them. The program would be testing each "if" even when the condition I'm looking for is found in the first "if", and slowing down the script. Since the project I'm working on is a background task, I should aim to keep it code optimised. That's the beauty of elseIf, because once it comes true, all the other elseIf's or else are skipped, similar to switch.

So why is the switch statement not working? Could it be that it doesn't like multiple Or'ed test conditions? The only other reason I can think of is a mismatch between the strings, but I can't see it. Hint _civClass looks the same as the strings I'm testing for. Baffed! :confused_o:

Share this post


Link to post
Share on other sites

with (("Worker1") || ("Worker2") || ("Worker3") || ("Worker4")) you're trying to make an or between strings - if you have scriptErrors enabled you'll get an error-message (or look into your arma.rpt)

if you use nested ifs there shouldn't be a difference to elseif except a few more parentheses

if it should be a backround-task and you've got many different states / conditions perhaps a FSM could do the job.

Share this post


Link to post
Share on other sites

Deadfast

That's what I was originally trying to do, but couldn't find the function to do it. How would I extract the parent?

Master85:

I leave state machines until I know how to code them but I understand the theory. That's my next challenge.

I did try to use a switch for numbers to see if OR's work, but again the script halted.

I'm not sure if nested if would work because if the outer "if" is false, then the inner ones will never process. It look like a row of if's is the only way.

Share this post


Link to post
Share on other sites
Deadfast

That's what I was originally trying to do, but couldn't find the function to do it. How would I extract the parent?

You dont need to "extract" it anyway. Just use iskindof/typeof directly as you would use for "Worker1" etc.

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  

×