Switch case question

Non-platform specific questions.

Switch case question

Postby foleyjo » Thu Feb 09, 2012 2:25 pm

Hi just need a bit of help but I'm not too sure how to explain it.

I'll start with what I have

In create actor is this call (ObjectText is a local actor string)
Code: Select all
strcpy(ObjectText, getObj(animindex));


getObj gives the Object's text based on the animindex
Code: Select all
Char * getObj(int ThisObject)  //Return the name of the object based on the anim index image
{
  switch (ThisObject)
  {
    case 0: return "Object text 1";
    case 1: return "Object text 2";
    case 2: return "Object text 3";
    case default: return "No Object";
}
  return NULL;
}


Now my problem is I may want to change the animnames in the anim index so how can I make it so it actually searches for the animation name in the switch statement. So my statement would look like the following

Code: Select all
 switch (ThisObject)
  {
    case Anim1: return "Object text 1";
    case Anim2: return "Object text 2";
    case Anim3: return "Object text 3";
    case default: return "No Object";
}


I know I could use a #define method for a similar effect but that's not what I'm after
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: Switch case question

Postby skydereign » Thu Feb 09, 2012 6:39 pm

Well you need to use strcmp and ifs instead (and you'll still need to bypass the getAnimName actor specific problem). You can use getAnimName but it only gets the animation name of the event actor. Still it kind of does what you want. Not sure though if this is exactly what you want since it isn't quite dynamic enough to be particularly useful.
Code: Select all
if(getAnimName(Anim1, "whatever you are searching for")==0)
{
    //
}
//else if and so on
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Switch case question

Postby foleyjo » Thu Feb 09, 2012 7:36 pm

Thanks for that sky.
I forgot about strcmp which is stupid of me as I used it lots in the Quizo games.

The getAnimName shouldn't be too much of a problem.
I have 1 object actor with many different Anims. Each anim is indicates what the object is.
The Actor variables for the object are specific to what the animations is (for example the object's name) and these are assigned when the actor is created.

So if I pass the actors animname in the function call I should have the first string to compare.
Then in the if's I can do the string compares.


Would it be extremely messy and resource wasteful to have a string compare function that returns a value and then do a switch case statement to assign the variables?

eg
Code: Select all
//In stringcompare() function
if(stringcompare code ==1) return 1;
if(stringcompare code ==1) return 2;
if(stringcompare code ==1) return 3;
return 0


Code: Select all
//in other function
switch (Stringcompare(StringVar 1,))
{
 case 1:assign all variables n actions; break;
 case 2:assign all variables n actions; break;
}



I know the code I put there won't do anything but I'm sure you will understand what I'm getting at.
It may not seem useful with the snippets I've given but I think it might help later on in my game if my planning is correct.
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: Switch case question

Postby skydereign » Thu Feb 09, 2012 8:01 pm

Well it wouldn't be that wasteful (besides the potential mass amount of strcmp calls). But it does seem weird that you have a lot of strcmp calls to get a variable, that you later use to determine what the values should be set to. I guess it'd make sense if the strcmp calls and the values to be set weren't one to one. Also another major thing is make sure when you actually use strcmp to check if it is equal to zero. strcmp returns 0 when the strings are the same.

If I were to do something like this, I would use integer comparisons instead of string comparisons as it is a lot faster. So while you prefer to use the actual animation names, it'd probably be better if you are worried about efficiency to create some animindex system instead of animname. Also I hear makslane says the getAnimName function is pretty inefficient (though I've never run into any noticeable problems from it).
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Switch case question

Postby foleyjo » Fri Feb 10, 2012 10:13 am

To be honest I would prefer to use an integer system. My first plan was to just set up a load of #define statements to keep track of which object is which.

My main concern is that when I was working on the screenshots in the first quizzo game I decided to remove 1 of the animations. This messed up the animation index and I had to remove all the images and then put them back in making sure they were in the correct order.

So basically what I'm looking for is a way to avoid doing the same issue if I decide to change one of the objects which is why I think doing something with the anim name might work.
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest