SFML- Missing ';' before identifier - c++

I want Player to create an object of the class Bullet.
Result:
syntax error : missing ';' before identifier 'bullet'
From what i've can find about the issue is that the Bullet class isn't known to the compiler at that point,
how do i make it known?
Player class
class Player :public Entity{
private:
float velocity;
Sprite titleSprite;
Texture titleTexture;
Sprite playerSprite;
Texture playerTexture;
Bullet bullet; // <-----------
public:
virtual void draw(RenderTarget& target, RenderStates states)const;
virtual void update(float dt);
void movePlayer(float offset);
Sprite getPlayerSprite()const;
Sprite getTitleSprite()const;
Bullet getBullet();
Player();
virtual ~Player();
};
Bullet class
#include "Player.h"
class Bullet : public Entity{
private:
Sprite bulletSprite;
Texture bulletTextucre;
public:
void shootBullet(float offset);
Sprite getBulletSprite()const;
Sprite getBulletTexture();
void setBulletSprite(Sprite bulletSprite);
virtual void draw(RenderTarget& target, RenderStates states)const;
virtual void update(float dt);
Bullet();
virtual ~Bullet();
};

Looks like Bullet class doesn't include Entity.h

#include Bullet.h header inside the Player header file.
And remove #include "Player.h" from the Bullet header if you're not using anything from that class.

Related

How to rotate bones in UE4 with C++?

I just want to create a node that simply specifies the bone name of the character and rotates it.
I want to move multiple bones, not just one bone, using the naming method.
I wrote the following codes.
MyCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MyCharacter.generated.h"
class USkeletalMeshComponent;
class UPoseableMeshComponent;
UCLASS()
class TESTPLUGIN_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AMyCharacter();
FRotator RotationValue;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
private:
UPoseableMeshComponent* PoseableMesh;
};
MyCharacter.cpp
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MyCharacter.generated.h"
class USkeletalMeshComponent;
class UPoseableMeshComponent;
UCLASS()
class TESTPLUGIN_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AMyCharacter();
FRotator RotationValue;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
private:
UPoseableMeshComponent* PoseableMesh;
};
When I compile the above code and open it with a level blueprint, I see a node like the one in the figure below.
This node is not what I wanted.
I want to create a node that can connect to FinalAnimationPose as shown in the image below.
How can I create a node like this?
All I want to do is simply rotate the bones every second. I couldn't find this information.
I am a beginner in both UE4 and C ++. Any answer will help.
I am new to UE but I think it was the same question which was asked in UE community to rotate but it was for a single bone:
Here's the link to that: https://answers.unrealengine.com/questions/47930/how-to-rotate-bone-in-c.html

C++ how to prevent import loops

I am a bit in the dark for the moment so I am trying stack overflow. My problem is that I have a classed named ‘Scene’ which will have a map of ‘GameObject’. The problem is that these GameObject need to use some function from there scene so I create a Scene* that is assigned to the main scene. The problem with this case is that I a in a loop where I import Scene which include GameObject which include Scene (and it goes on). If you think 5isnis not clear feel free to comment I will give more detail or edit this post thx.
Scene.h
class Scene
{
public:
sf::RenderWindow* window;
_Manager manager;
sf::Color backgroundColor;
std::string name;
std::map<std::string, GameObject*> gameObjects;
Scene();
Scene(std::string name);
virtual void Start() = 0;
virtual void Update() = 0;
virtual void Draw() = 0;
void _Update();
};
GameObject.h
class GameObject : public Object
{
private:
Scene* scene;
std::vector<Component*> components;
public:
Transform* transform = new Transform;
void AddComponent(Component* component);
Component* GetComponent(std::string type);
GameObject* Find();
void Instantiate(GameObject* gm, Transform* transform);
void Start();
void Update();
};
For those who got the same problem i just forgot you can do forward declaration and when you need to use the full class just import the files you need in the .cpp file of your class.

C++ - Circular dependency between 3 Classes

I have three classes, 2 of which rely on the functionality of each other, and one which contains pointers to and is included in both:
Player:
#pragma once
#include <SFML\Graphics.hpp>
#include "RenderableObject.h"
//#include "Renderer.h"
class Renderer;
class Player : public RenderableObject
{
public:
Player(int size, Renderer* renderer);
~Player();
void render();
void setX(int x);
void setY(int y);
int getX();
int getY();
void setVelX(float x);
void setVelY(float y);
float getVelx();
float getVely();
int getSize();
private:
int size;
int x;
int y;
float velx;
float vely;
Renderer* ren;
};
GameManager:
#pragma once
#include "Player.h"
class Renderer;
class GameManager
{
public:
GameManager(Renderer* r);
~GameManager();
Player* getBall();
private:
Player* ball;
};
Renderer:
#pragma once
#include <SFML\Graphics.hpp>
#include "GameManager.h"
#include "Player.h"
class Renderer
{
public:
Renderer(GameManager* game);
~Renderer();
sf::RenderWindow* getWindow();
void draw();
void renderCircle(int size, int x, int y);
void renderSquare(int size, int x, int y);
private:
sf::RenderWindow* win;
GameManager* game;
Player* ball;
};
I suspect there is some sort of circular dependency going on here. I managed to reduce the 60 odd errors down to 3, by adding "class Renderer;" to the Player header file. However, I'm now receiving "Use of undefined type" errors.
It might help to give a higher level view of what I want to achieve:
The GameManager object should hold an instance (maybe more) of player. This GameManager object is shared between subsystems such as Renderer so they all have access to the same resources.
The Renderer object should be able to call the Player object's (retrieved from the GameManager object) render() function, which in turn should call back to the Renderer object's renderCircle() object.
I'm hoping this is a simple case of re-arranging the includes, without having to re-design what I've already done.
In a header file, where you only declare variables that are pointers or references, all the compiler needs to know is that a class exists, nothing more. It doesn't need to know how big it it, or what members the class have. Therefore it's enough with a forward declaration.
However, in the source files where the variable is used, objects are created, and member functions are called, then the compiler need the full definition which means you have to include your whole header file.
Taking your code, simplified:
Player.h
#pragma once
class Renderer;
class Player
{
public:
Player(Renderer*);
private:
Renderer* ren;
};
GameManager.h
#pragma once
class Renderer;
class GameManager
{
public:
GameManager(Renderer*);
private:
Renderer* ren;
};
Renderer.h:
#pragma once
class Player;
class GameManager;
class Renderer
{
public:
Renderer(GameManager*);
private:
GameManager* game;
Player* ball;
};
The above header files are about the same that you already have, with the exception that I used forward declarations in Renderer.h instead.
Now the Renderer.cpp source file, where the Player and GameManager objects are actually created and used, we need the full definition from the header files:
#include "Renderer.h"
#include "Player.h"
#include "GameManager.h"
Renderer::Renderer(GameManager* game)
: game(game), ball(new Player)
{
}
// Other functions that calls member function in the `man` and `ball` objects
Of course, you need to #include "Renderer.h" in the Player.cpp and GameManager.cpp source files.
If you have other errors, they are because of other things you done, not because of the circular dependencies, as those have been solved with the forward declarations.

Inherited sprite not draw-able

In SFML I wanted to have a sprite but with other functions and variables so i decided to create a class that inherits the sprite class like this:
1. Player.hpp
#pragma once
#include <stdio.h>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
class Player : sf::Sprite{
public:
void setup();
void input();
private:
int direction;
void moveRight();
void moveLeft();
sf::Texture textures[8];
};
2(WIP). Player.cpp
void Player::setup(){
direction = 0;
textures[0].loadFromFile("images/player_right_still.png");
textures[1].loadFromFile("images/player_right_jump.png");
textures[2].loadFromFile("images/player_right_walk1.png");
textures[3].loadFromFile("images/player_right_walk2.png");
textures[4].loadFromFile("images/player_left_still.png");
textures[5].loadFromFile("images/player_left_jump.png");
textures[6].loadFromFile("images/player_left_walk1.png");
setTexture(textures[0]);
}
void Player::input(){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
moveRight();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
moveLeft();
}
}
void Player::moveRight(){
}
void Player::moveLeft(){
}
game.cpp (picture because SO is being weird on me)
But when I compile it I get this error:
game.cpp: In function ‘int main()’:
game.cpp:23:27: error: ‘sf::Drawable’ is an inaccessible base of ‘Player’
window.draw(player);
class Player : sf::Sprite means that inheritance is private, that is code using instances of Player class won't be able to cast it to Sprite or access methods inherited from Sprite class. You should change inheritance to public:
class Player: public sf::Sprite

'GameState': base class undefined

I have a general GameState class:
#pragma once
#include "GameContext.h"
class GameContext;
class GameState
{
private:
protected:
public:
GameState(GameContext* context);
GameContext* gameContext = nullptr;
virtual void update(float deltaTime) = 0;
virtual void draw(float deltaTime) = 0;
};
which is suppose to be the base for other GameState's. I have attempted to implement a derived class of GameState:
#pragma once
#include <iostream>
#include "GameState.h"
#include "GameContext.h"
class GameStateMenu : public GameState
{
private:
protected:
public:
GameStateMenu(GameContext* gameContext);
void update(float deltaTime);
void draw(float deltaTime);
};
I am getting a error in the implementation of GameStateMenu at
class GameStateMenu : public GameState
where it says
'GameState': base class undefined
I have googled and gone through my code for a good few hours now and can still not figure it out. I feel like it should know what GameState is but I can't figure out why it doesn't.
(If the current code is not enough to figure it out the rest is available HERE)
As I suspected the problem was a circular dependency that I had also forward declared which meant that I did not see an error about it when compiling. Removing the #include "GameContext.h" in GameState.h removed the one circular dependecy that was causing the problem.