# initialising constructor

I am using the SFML library to create a 2D grid. i use grid cords so I can place my players on the grid:

I use struct to hold information of the nodes

struct Node

{

std::pair <int, int> node;

bool iswall = false;

};


I created the Grid class function where I set the position.

void Gridspot::setEnemyPosition(int i, int j)

{

Enemytank.node = std::make_pair(i,j);

}


I created the enemy in a different class

class AIEnemy

{

public:

AIEnemy()

void Update();

sf::RectangleShape getRect();

void setNodePosition(sf::RectangleShape \* pos);

void AutoMotion(int col, int row);

};


i entered the properties of the enemy(size, colour etc.) in its constructor.

AIEnemy::AIEnemy()

{

ect.setSize(sf::Vector2f(25.f, 25.f));

sf::Vector2f origin = rect.getSize();

rect.setOrigin(sf::Vector2f(origin.x /2 , origin.y / 2));

rect.setFillColor(sf::Color::Blue);

}


and I set the sfml shape in the grid by getting the node cord in the parameter and getting its position in the window

void AIEnemy::setNodePosition(sf::RectangleShape * pos)

{

sf::Vector2f origin = pos->getSize();

rect.setPosition(pos->getPosition().x+ origin.x /2, pos->getPosition().y + origin.y / 2);

}


in the main :

if (i == pAInode.first && j == pAInode.second)

{

AItank.setNodePosition(&box[i][j]);

}


now, I did not set the position in the constructor because I needed to set it in the game loop where im creating the visual display of the grid.

problem is that when I now call the update function:

void AIEnemy::Update()

{

bottom = rect.getPosition().y + rect.getSize().y;

left = rect.getPosition().x;

right = rect.getPosition().x + rect.getSize().x;

top = rect.getPosition().y;

}


it returns 0,0 even when I set the node position elsewhere. I know this is due to me not setting it in the constructor so my question is: is there a way i can set the position after creating the object?

• Reading your question, I'm confused, there are multiple pieces that do not seem to fit together. Where is rect declared, what is it? Could you provide a minimum complete verifiable example, or at least some more complete code? – Vaillancourt Feb 17 at 3:10