Sign in to follow this  
Crusable77

I am not sure why this is not working.

Recommended Posts

Crusable77    594

Hello, I have been pondering an error I have had for a few days now and I just cannot seem to figure out the problem. I have an abstract base class called Entity which my PlayerEntity class is inheriting from. Entity has a function called setPosition and takes no parameters I am trying to use the setPosition function in my PlayerEntity's constructor however when I do I get a window that says Debug Assertion Failed! and then says vector subscript is out of range. I have narrowed down the problem to my setPosition function. Thank you for any help.

 

Entity::setPosition:

void Entity::setPositon(){
	
	//set the choords of the vertecies on the vertex array
	m_sfEntityBox[0].position = sf::Vector2f(m_sfPosition.x, m_sfPosition.y);
	m_sfEntityBox[1].position = sf::Vector2f(m_sfPosition.x + m_sfSize.x, m_sfPosition.y);
	m_sfEntityBox[2].position = sf::Vector2f(m_sfPosition.x + m_sfSize.x, m_sfPosition.y + m_sfSize.y);
	m_sfEntityBox[3].position = sf::Vector2f(m_sfPosition.x, m_sfPosition.y + m_sfSize.y);
}

Share this post


Link to post
Share on other sites
SiCrane    11839

Internally, SFML's VertexArray uses a vector for storage. (Well at least some versions of SFML anyways.) And like a vector, it needs to be sized properly before you use operator[] on it. 

Share this post


Link to post
Share on other sites
BitMaster    8651
Well, in this case have you ensured that m_sfEntityBox.getVertexCount() returns at least four and called resize if that is not the case? On a side note, according to code available online, sf::VertexArray is just an extremely thin wrapper around an std::vector.

Share this post


Link to post
Share on other sites
Crusable77    594

Well, in this case have you ensured that m_sfEntityBox.getVertexCount() returns at least four and called resize if that is not the case? 

Yes that was the problem thanks. I set the sf::VectorArray in the constructor of the Entity, and the way I was doing it before worked, which was sloppy and looked bad so I changed it. Thanks.

 

Edit: For the function void Entity::setPosition() do I need to use "this" for all the variables? I would think so because I am not using the function for the Entity but for a child class. It works as is, I am just wondering.

Edited by Crusable

Share this post


Link to post
Share on other sites
BitMaster    8651

I would think so because I am not using the function for the Entity but for a child class. It works as is, I am just wondering.


You do not need to, neither is it common to write it. I would strongly suggest getting on a more firm footing with C++ before adding the additional complication of external libraries like SFML.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this