Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Incrementing a Variable

Incrementing a Variable

Scheduled Pinned Locked Moved C / C++ / MFC
performancehelptutorialquestion
15 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Perspx

    Can you give us some more context? Regards, --Perspx

    "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia

    T Offline
    T Offline
    T RATHA KRISHNAN
    wrote on last edited by
    #5

    Why it's been voted down?

    P 1 Reply Last reply
    0
    • T T RATHA KRISHNAN

      Why it's been voted down?

      P Offline
      P Offline
      Perspx
      wrote on last edited by
      #6

      How is that piece of code being run? Is it being run in a loop? Regards, --Perspx

      "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia

      T 1 Reply Last reply
      0
      • P Perspx

        How is that piece of code being run? Is it being run in a loop? Regards, --Perspx

        "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia

        T Offline
        T Offline
        T RATHA KRISHNAN
        wrote on last edited by
        #7

        I've used while loop instead of the top most if statement. The result is same. What to do?

        P 1 Reply Last reply
        0
        • T T RATHA KRISHNAN

          I want to set the speed of the object to 6 if it's travelling between the 1st and 3rd positions. But it's 6 from start to end. How can I set the speed for in between positions(1st to 3rd)? Pls refer to the code above.

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #8

          Wow, that's a hell of a cryptic question. You have to get out of your code for a while and think that we can't see your screen. For a C++ developper (which lot of us are, obviously, except the ones that get lost on this board), an object is a class. And you can't move a class or make it travel. I guess you are doing something on the screen and displaying something that moves but it is very vague. Now, for incrementing a variable, either you increment it or you don't. You don't have another choice. Maybe you are using a float and would like to have a constant increase of your value when the thing you move on the screen is moving, but then you need to handle it yourself and it really depends a lot on how you are moving those things. When you move such a thing, increment a bit your variable at the same time. That's about all the help we can provide giving your cryptic question.

          Cédric Moonen Software developer
          Charting control [v1.4] OpenGL game tutorial in C++

          T 1 Reply Last reply
          0
          • T T RATHA KRISHNAN

            I want to set the speed of the object to 6 if it's travelling between the 1st and 3rd positions. But it's 6 from start to end. How can I set the speed for in between positions(1st to 3rd)? Pls refer to the code above.

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #9

            T.RATHA KRISHNAN wrote:

            But it's 6 from start to end.

            Which means the if(pWaypointNum1 < 6) condition will never evaluate to true. Have you stepped through the code to verify this?

            "Love people and use things, not love things and use people." - Unknown

            "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

            1 Reply Last reply
            0
            • T T RATHA KRISHNAN

              I've used while loop instead of the top most if statement. The result is same. What to do?

              P Offline
              P Offline
              Perspx
              wrote on last edited by
              #10

              Your first line inside the while/if statement is:

              if((pWaypointNum1 > 1) && (pWaypointNum1 <= 3))

              Why not just set pWaypointNum1 to 1 before the while/if statement and then change that line to

              if(pWaypointNum1 < 4)

              as it seems to take integer values? Regards, --Perspx

              "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia

              C 1 Reply Last reply
              0
              • C Cedric Moonen

                Wow, that's a hell of a cryptic question. You have to get out of your code for a while and think that we can't see your screen. For a C++ developper (which lot of us are, obviously, except the ones that get lost on this board), an object is a class. And you can't move a class or make it travel. I guess you are doing something on the screen and displaying something that moves but it is very vague. Now, for incrementing a variable, either you increment it or you don't. You don't have another choice. Maybe you are using a float and would like to have a constant increase of your value when the thing you move on the screen is moving, but then you need to handle it yourself and it really depends a lot on how you are moving those things. When you move such a thing, increment a bit your variable at the same time. That's about all the help we can provide giving your cryptic question.

                Cédric Moonen Software developer
                Charting control [v1.4] OpenGL game tutorial in C++

                T Offline
                T Offline
                T RATHA KRISHNAN
                wrote on last edited by
                #11

                Pardon me! I meant the real world object(like a box, car etc.) not the Object class.

                1 Reply Last reply
                0
                • P Perspx

                  Your first line inside the while/if statement is:

                  if((pWaypointNum1 > 1) && (pWaypointNum1 <= 3))

                  Why not just set pWaypointNum1 to 1 before the while/if statement and then change that line to

                  if(pWaypointNum1 < 4)

                  as it seems to take integer values? Regards, --Perspx

                  "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia

                  C Offline
                  C Offline
                  cgb143
                  wrote on last edited by
                  #12

                  Perspx wrote:

                  Why not just set pWaypointNum1 to 1 before the while/if statement

                  in the original when pWaypointNuml = 1 testl will be set to 3 your solution when pWaypointNuml = 1 testl will be set to 6

                  P 1 Reply Last reply
                  0
                  • C cgb143

                    Perspx wrote:

                    Why not just set pWaypointNum1 to 1 before the while/if statement

                    in the original when pWaypointNuml = 1 testl will be set to 3 your solution when pWaypointNuml = 1 testl will be set to 6

                    P Offline
                    P Offline
                    Perspx
                    wrote on last edited by
                    #13

                    Oh right missed that :doh: Regards, --Perspx

                    "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript

                    1 Reply Last reply
                    0
                    • T T RATHA KRISHNAN

                      I want to set the speed of the object to 6 if it's travelling between the 1st and 3rd positions. But it's 6 from start to end. How can I set the speed for in between positions(1st to 3rd)? Pls refer to the code above.

                      C Offline
                      C Offline
                      cgb143
                      wrote on last edited by
                      #14

                      What does mCharacter1->GoToWaypoint(pWaypointNum1) look like? Does this use 'testl' to increment the screen position? If so, where else does 'testl' get modified? What happens when pWaypointNuml = 6 ?

                      1 Reply Last reply
                      0
                      • T T RATHA KRISHNAN

                        I want to move an Object to a particular point if a condition is met. I use the variable "pWaypointNum1" to refer the position of the object and "mCharacter to refer the Object. Here's the code:

                        if(pWaypointNum1 < 6)
                        {
                        if((pWaypointNum1 > 1) && (pWaypointNum1 <= 3))
                        {
                        test1 = 6; //Change the speed of the Object to 6
                        }
                        else
                        {
                        test1 = 3; //Change the speed of the Object to 3
                        }
                        mCharacter1->GoToWaypoint(pWaypointNum1); //To send the Object to "pWaypointNum1"
                        pWaypointNum1++; //Incrementing the Object's position.
                        }

                        My problem is: I want to increment the variable pWaypointNum1 by 1 as the Object moves from one position to the next position. But it increments in a sudden. How to increment the pWaypointNum1 variable as the Object moves from one position to the next position?

                        N Offline
                        N Offline
                        Nelek
                        wrote on last edited by
                        #15

                        I hope to understand your problem right. Let's suppose your "Object" is a square, if you want to move it from A to B as "animation" with different speeds... One way to do could be: 1) You calculate the differences in coordinates between A and B. Let's suppose an horizontal line where A is (0, 150) and B (500, 150). So for that example the distance is going to be 500 "pixels" or units of your screen coordinates. 2) You set your different speeds. Let's suppose 10 pixels / sec, 50 pixels / sec and 100 pixels / sec 3) Speed = distance / time == distance = speed * time == time = distance / speed Let's choose this last one, time depending on the other 2. 4) You make a function to increment in one pixel the coordinate of your square

                        void MoveSquare () { square.x = square.x + 1; }

                        for such a line you don't need a function, but if you want to do more things or move it in more directions you can ride better with it. 5) When you have to move the square you just need to calculate the time needed with the end_pos and the speed (500 / 10 = 50 sec; 500 / 50 = 10 secs; 500 / 100 = 5 sec) 6) You have to call the MoveSquare 500 times to move it till end_pos, then 1st case -> 50 sec / 500 calls = 0.1 sec / call 2nd case -> 10 sec / 500 calls = 0.05 sec / call 3rd case -> 5 sec / 500 calls = 0.01 sec / call 7) Create a timer with that refresh rates 100, 50, 10 ms and call MoveSquare into it 8) when end_pos is reached, kill the timer Is something like that what you were searching?

                        Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson Rating helpfull answers is nice, but saying thanks can be even nicer.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups