I am creating a turn-based RPG, and I am using SFML for window and 2d graphics, planning to later use OpenGL for 3d graphics.
I was wondering, is there a way to combine different entities in a getvector format? for example, I have a green rectangle that is grass for now, and was trying to use a vertically scaled rectangle for the player. the thing is, if I have multiple vector2f get(enter vector here(positions, scale etc.)) then it gives me an error. how would I combine the two entities?
I have tried doing operands like &&, &=. +=, etc. but none of them work. I have also tried doing commas in between the two entities, but to no avail
Here is the error
main.cpp:35:18: error: redeclaration of ‘sf::Vector2f position’
35 | sf::Vector2f position = ground.getPosition();
| ^~~~~~~~
main.cpp:32:18: note: ‘sf::Vector2f position’ previously declared here
32 | sf::Vector2f position = player.getPosition();
| ^~~~~~~~
main.cpp:36:18: error: redeclaration of ‘sf::Vector2f scale’
36 | sf::Vector2f scale = ground.getScale();
| ^~~~~
main.cpp:33:18: note: ‘sf::Vector2f scale’ previously declared here
33 | sf::Vector2f scale = player.getScale();
| ^~~~~
Here is the code
#include <SFML/Graphics.hpp>
int main()
{
sf::RectangleShape ground(sf::Vector2f(120.f, 50.f));
sf::RectangleShape player(sf::Vector2f(120.f, 50.0f));
// Manipulate `ground`.
// Manipulate `player`.
sf::Vector2f position = player.getPosition();
sf::Vector2f scale = player.getScale();
sf::Vector2f position = ground.getPosition();
sf::Vector2f scale = ground.getScale();
// Use `player` and `ground`.
}
it might also be important to know that I am on linux. ubuntu if that matters.
Your error looks pretty self-explanatory to me. You have two declarations of position
and two declarations of scale
. This is the same as if your code was
int main()
{
int position = 1;
int position = 2; // ERROR!
int scale = 3;
int scale = 4; // ERROR!
}
This does not work. If you want to "combine" values, then combine them rather than trying to create multiple variables with the same name. (Given your mention of trying to use +=
, I'll assume that "combine" means "add".)
For my non-SFML, non-vector example, one could use
int position = 1 + 2;
//^^^ ^^^^^^^^ ^^^^^
//type identifier expression
or
int position = 1;
position += 2; // NOTE: Not declaring a new variable!
or (an even longer form)
int position = 1;
position = position + 2; // NOTE: Not declaring a new variable!
The lines that consist of a type name followed by an identifier followed by =
and an expression define a new variable that is initialized to the value of the expression. The lines that lack the type name use an existing variable and assign it a new value. Re-using an existing variable is allowed; defining a second variable with the same name (and same scope) is not.
The same syntax rules apply to other types, such as sf::Vector2f
. Since SFML has defined the applicable operator+
and operator +=
(assignment, not initialization), there is nothing special here.
sf::Vector2f position = player.getPosition() + ground.getPosition();
//^^^^^^^^^^^^ ^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//type identifier expression
or
sf::Vector2f position = player.getPosition();
position += ground.getPosition(); // NOTE: Not declaring a new variable!
or (an even longer form)
sf::Vector2f position = player.getPosition();
position = position + ground.getPosition(); // NOTE: Not declaring a new variable!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.