Variables don't change unless there's a breakpoint

I'm on Xcode and I have a project, but when I run it without a breakpoint in the section which uses the variables which aren't changing, they don't change.

However, if I put a breakpoint there they do get updated.

This references the variables:

 
return vector.x >= this->topLeft.x && vector.x <= this->topRight.x && vector.y >= this->topLeft.y && vector.y <= this->bottomLeft.y;


and they are updated here:

1
2
3
4
5
6
this->xPos -= this->speed * elapsedTime;
this->topLeft = this->topLeft - Vector2(this->speed * elapsedTime, 0);
this->topRight = this->topRight - Vector2(this->speed * elapsedTime, 0);
this->bottomLeft = this->bottomLeft - Vector2(this->speed * elapsedTime, 0);
this->bottomRight = this->bottomRight - Vector2(this->speed * elapsedTime, 0);
this->moving = false;
So do you have threads anywhere in this code?
http://www.eelis.net/iso-c++/testcase.xhtml
A testcase consisting of randomly copy&paste'd bits of code that you thought were relevant can obviously not reproduce the problem.
I don't have any Threads that I declare, but the SDL2 toolkit might declare some.

Here's some more code:
1
2
3
4
5
6
7
8
9
//This is in main.cpp
W_TestObject testObject = W_TestObject(se.getGraphics(), 0, 0);
W_TestObject testHit = W_TestObject(se.getGraphics(), 18, 0);
//...
if (se.keyboard.isKeyHeld(SDL_SCANCODE_LEFT))
{
	testHit.moveLeft();
}
testObject.runIfHit(&testHit);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//This is the W_TestObject class
class W_TestObject : public W_Object
{
public:
	W_TestObject(SE_Graphics *graphics, int xPos, int yPos);
	
	void moveLeft();
private:
	float speed;	// How long (in milliseconds) it takes to move 1 pixel
	bool moving;
	
	virtual void hit(W_Object *other);
	virtual void updateObject(int elapsedTime);
	virtual void animationDone(std::string currentAnimation);
	virtual void setupAnimations();
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Here's the W_Object class:
class W_Object : public AnimSprite
{
public:
	W_Object(SE_Graphics *graphics, const std::string image, float xPos, float yPos, float width, float height, bool hasHitbox, float worldX, float worldY, float timeToUpdate);
	
	void update(int elapsedTime);
	
	float xPos, yPos, width, height;
	bool hasCollided(W_Object *other);
	void runIfHit(W_Object *other);
	
	Vector2 topLeft;
	Vector2 topRight;
	Vector2 bottomRight;
	Vector2 bottomLeft;
	
	int objectType;
	
protected:
	bool vector2InObject(Vector2 vector);
	
	virtual void hit(W_Object *other) = 0;
	virtual void updateObject(int elapsedTime) = 0;
	virtual void animationDone(std::string currentAnimation) = 0;
	virtual void setupAnimations() = 0;
	
private:
	bool hasHitbox;
};

1
2
3
4
5
6
7
8
9
10
// This code checks for the collision and is being problematic.
bool W_Object::vector2InObject(Vector2 vector)
{
	std::cout << vector.x << "N" << std::endl;
	if (vector.x <= 64)
	{
		std::cout << vector.x << std::endl << vector.y << std::endl;
	}
	return vector.x >= this->topLeft.x && vector.x <= this->topRight.x && vector.y >= this->topLeft.y && vector.y <= this->bottomLeft.y;
}
Putting breakpoints in may significantly affect your idea of 'elapsedTime'.

Without breakpoints, elapsedTime might be 0, or so close to 0 that all your calculations show no movement.

You also seem to be duplicating data (which is bad).
1
2
3
4
5
	float xPos, yPos, width, height;
	Vector2 topLeft;
	Vector2 topRight;
	Vector2 bottomRight;
	Vector2 bottomLeft;

Create a computeHitbox function (what's bool hasHitbox; for?), and call it on demand, rather than trying to store it and keep it up to date whenever you move the object.

Use const references, not pointers or value copies.
> bool hasCollided(W_Object *other);
> void runIfHit(W_Object *other);
> bool vector2InObject(Vector2 vector);


I was able to fix it by making my movement have a delay of 20 milliseconds and reprogramming the motion system. Thanks for answering everyone!
Registered users can post here. Sign in or register to post.