0

hi below is a switch case that is suppose to go to the next
"hep" # and show a message... so if hep is 0 it shows one message, if hep is 1 it shows another, etc.
I have it showing a message for 0, and 1, but it ALSO goes back to 0! I thought the case switched with ++hep so its not 0 anymore...pplease advise?

if(Item *item=CAST(Item, obj[i]))

             if(item.type==ITEM_MISC && item.mesh && Dist(test_pos, item.mesh->box*item.matrixScaled())<=ctrl.radius()+0.1f) 
  {



       int hep;

    hep=0;

   switch(hep)
    {

    case 0:
    if ( item.name=="barrel" && hep==0)

 //  }

    ++hep;
     AddMessage(S+"000er"+ hep);//1
     // (itemPickUp(*item));


  break;
 case 1:
      if ( item.name=="biggun" && hep==1)

    //void itemRemoved();

    ++hep;

     AddMessage(S+"111111er"+ hep);//1
   //   (itemPickUp(*item));


  break;


     case 2:
      if ( item.name=="Spirit" && hep==2)

      ++hep;
     AddMessage(S+"333333er"+hep);
      itemPickUp(*item);



     break;

    } 



  }
4
Contributors
7
Replies
43
Views
2 Years
Discussion Span
Last Post by verse.ability
0

hi no errors... got 2 warnings but i dont think its related (hopefully). i put the heps inside { } in the ifs now but only one works...so i left the {} off..is this a game engine issue more than c++? Esenthial is a c++ gameengine but hmay be unique.. ok thanks

.\Windows Manifest.xml : manifest authoring warning 81010002: Unrecognized Element "compatibility" in namespace "urn:schemas-microsoft-com:compatibility.v1".
.\Windows Manifest.xml : manifest authoring warning 81010002: Unrecognized Element "application" in namespace "urn:schemas-microsoft-com:asm.v3".

0

After cleaning up your code by removing commented out code, extra whitespace and the redundant checks against hep in the if statements inside your switch statement, we are left with this:
(NOTE: This is just what the code you posted above boils down to logically - I haven't fixed anything. This is just your code!):

if(item.type==ITEM_MISC && item.mesh && Dist(test_pos, item.mesh->box*item.matrixScaled())<=ctrl.radius()+0.1f)
{
        int hep=0;

        switch(hep)
        {
        case 0:
                if ( item.name=="barrel" ) // no need for && hep == 0, we already know it is 0. We wouldn't be here otherwise!
                        ++hep;
                AddMessage(S+"000er"+ hep);
                break;
        case 1:
                if ( item.name=="biggun" )
                        ++hep;
                AddMessage(S+"111111er"+ hep);
                break;
        case 2:
                if ( item.name=="Spirit" )
                        ++hep;
                AddMessage(S+"333333er"+hep);
                itemPickUp(*item);
                break;
        }
}

The only thing that is ever going to be executed in that block is the first bit under case 0 of the switch. Take a look at the logic of what is going on in the code:
1. Assuming that we pass the conditions of the initial if statement, we enter the block of code under the if statement and create an int variable called hep and initialise it to 0.
2. We enter the switch statement. hep is 0, therefore the code under case 0 is executed.
3. If item.name is "barrel", hep is incremented
4. Message is written out regardless of the item name
5. We hit the break statement, so we are taken to the closing brace of the switch statement;
6. We then hit the closing brace for the if statement at the first line and we are now outside the scope of the initial if statement.

The next time we end up going through that block of code, hep will be created again and initialised to 0 and you will only get the first part of the switch executed. The rest of it will never be used unless you change the value of hep outside of the switch statement.

So the real question is, what are you actually trying to do in this block of code? Without knowing your exact intention, or how this block of code fits in with the rest of your program; We can't really suggest a way to fix it.

Edited by JasonHippy

0

answer your question as to what the goal is :
there are a few 3d objects with different names such as barrel, biggun,etc. When the player gets close to one of them AND the hep is a certain value the point is awarded (or in this case a message appears). The
concept is like different questions, question 0 is
"what stores water".. so player needs to find barrel.
Then the question should be switched to "1" when found to be something like "what hurts"...player needs to find a "biggun" for the correct answer...
I tried doing switch cases with strings but it seemed hard so i thought numbers of questions (hep) would be a way. Please let me know how this is easily tweaked to work? thanks!!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.