Getter Function Problem

While debugging my code, I noticed that my getter functions are not working. And by not working, I mean they are not updating the new value of the member variable, but instead return the initialized value from constructor.


Agent.cpp
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
31
32
33
34
35
36
37
38
39

Agent::Agent() : m_aRadius(10.0f), MovingEntity(0.10f, 0.20f, 100.0f, sf::Vector2f(10.0f, 400.0f), sf::Vector2f(0.0f, 0.0f), sf::Vector2f(0.0f, 0.0f))
{
	m_Agent.setRadius(m_aRadius);
	m_Agent.setFillColor(sf::Color::Red);
	m_Agent.setPosition(m_Position.x, m_Position.y);
	
}

 void Agent::Update(sf::Time dt)
{
	SteeringBehaviors behavior;
	sf::Vector2f steeringForce = behavior.FollowPath(m_Agent);

	if(steeringForce.x >= m_MaxForce || steeringForce.y >= m_MaxForce)
		steeringForce = sf::Vector2f(m_MaxForce, m_MaxForce);
	
	m_Acceleration = steeringForce / m_Mass;
	
	m_Velocity += m_Acceleration * dt.asSeconds();

	if(m_Velocity.x >= m_MaxSpeed || m_Velocity.y >= m_MaxSpeed)
		m_Velocity = sf::Vector2f(m_MaxSpeed, m_MaxSpeed);

	m_Agent.move(m_Velocity.x * dt.asSeconds(), m_Velocity.y *dt.asSeconds());
	
	m_Position = m_Agent.getPosition();
}

sf::Vector2f Agent::getVelocity()
{	
	return m_Velocity;
}

sf::Vector2f Agent::getPosition()
{
	return  m_Position;
}


SteeringBehaviors.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
sf::Vector2f SteeringBehaviors::Seek(const sf::Vector2f targetPos)
{
	
	sf::Vector2f agentPos = agent.getPosition(); // <-- Same problem here with agent.getPosition().

	sf::Vector2f GetNormalized;
	GetNormalized.x = targetPos.x - agent.getPosition().x; 
	GetNormalized.y = targetPos.y - agent.getPosition().y;

	sf::Vector2f DesiredVelocity = NormalizeVector(GetNormalized) * agent.getMaxSpeed();

	//cout << agent.getVelocity().x << "  " << agent.getVelocity().y << endl; <-- Returns Zero
	
	return (DesiredVelocity - agent.getVelocity());  //<--- agent.getVelocity() is the problem. It keeps returning zero, despite the velocity changing in the Agent update function
}


Note:
m_Velocity is a member variable from the base class MovingEntity (protected), but is being initialized in its derived class Agent (Fifth argument in the Agent ctor).


Can somebody give me a hint in what I'm doing wrong. Feel free to ask for more code if the above code is not sufficient.
Last edited on
How is update() called?

Try putting a breakpoint in your update method and running your code.
Does it actually go in there?
I am calling the function in main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

     Agent agent;

	while(Window.isOpen())
	{
		Window.clear();

		sf::Time dt = clock.getElapsedTime();
		agent.Update(dt);
              
            // Irrelevant code

	}



Yes, the program actually enters the Update function. I even placed an output statement and it works. The problem is the getter functions not updating their new values.

The Seek function is being called in the FollowPath function which is called in the Agent Update function.
Last edited on
The agent.getPosition() and agent.getVelocity() both work in the main function and Agent update function, but whenever I call

 
cout << agent.getVelocity().x << "  " << agent.getVelocity().y << endl; 


in say, the Seek function, the values do not change. Which is odd, as I said before, the Seek function is called by the FollowPath function and FollowPath is being called by the Agent Update function and Agent Update is called in the Main function.
Last edited on
Topic archived. No new replies allowed.