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;

    } 



  }

Recommended Answers

All 7 Replies

It is not always clear if hep is inside or outside an if-statement.
Do you have a debugger?

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".

If you tidy up your code that would look better.

thanks. but still didnt work.

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.

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!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.