I have been working on a small C++ game with Qt, but I wanted to make a simple AI for the game to be a bit more interesting. But I need the Enemy class to know where I am so it can react, but I can not find a way to export my position which is defined by pos().x() in the Player class. Anyone have any ideas as to how to export it, or any different ideas? I have started with C++ fairly recently so If you could give a nice explanation it would be greatly appreciated!
Note* : I realize it is part Qt, which is off topic in the forum, but exporting pointers or variables should be a C++ topic right? Please correct me if I'm wrong.
Player.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#ifndef PLAYER
#define PLAYER
#include <QGraphicsRectItem>
#include <QMediaPlayer>
#include <QGraphicsItem>
class Player : public QGraphicsRectItem{
public:
Player(QGraphicsItem *parent=0);
void keyPressEvent(QKeyEvent *event); //QKeyEvent takes a pointer as argument
//double currPos;
private:
QMediaPlayer *bulletSound;
};
#endif // PLAYER
class Actor{
public:
virtual QPoint get_position() = 0;
};
class Player : public QGraphicsRectItem, public Actor{
//...
};
class Enemy : public Actor{
public:
AiMovement compute_movement(ActorIterator actors);
};
Could you explain that a bit? Also, when I try and use your method I get ambiguous cases on every try. Also, what is the Actor class? Thank you for the answer by the way. Is it not possible to just export a variable/pointer with the values for position from Player class to Enemy class (they are in different files, but I don't think that matters).
when I try and use your method I get ambiguous cases
What do you mean? I only gave a vague idea of what your class structure should look like. There's nothing to try.
what is the Actor class?
You could say an actor is anything that is dynamic and needs to be considered by the game rules. So doors (not doorways) would be actors, but walls wouldn't be. Anything that (in the context of the virtual world) is conscious would be an actor. Projectiles may be actors if they're distinct entities. For example, when fired, the guns in nearly all FPSs don't generate actors. They immediately call the function shot() on whatever the shooter is looking at. On the other hand, rockets fired from rocket launchers are actors, because they may be dodged, blocked, or sometimes shot down. They exist as entities distinct from both the shooter and the target.
Is it not possible to just export a variable/pointer with the values for position from Player class to Enemy class
Could you define in precise terms what it would mean to "export" something? In programming you can do anything and only the things that can be defined in mathematical terms.
But to properly answer your question, no. There doesn't exist anything that will handle the intricacies of moving data around. You could make something, but if you do, you will not say "just export" when you use it. It's more likely that you will say "what a pain! The code looks butt-ugly and the program keeps crashing because of illegal memory accesses!"
Oh sorry, did not entirely understand on first reading, feeling better about it now.
What I meant by export is, for example, to declare a variable/pointer that carries the value, and somehow point to that value from a separate class, like using a global variable, basically letting other classes and their functions outside the class containing the position have access to the value of the position. I could just code this part into the class containing the variable, but was just wondering about other ways to do it to keep the code a bit tidier.
Also thank you for the idea in your first comment, will try to implement in some way if I decide to go along with using the variable across classes and there are no other propositions.