Global pointers? Need help with cross class varibles or pointers

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


Game.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "Player.h"
#include "Bullet.h"
#include <QDebug>

#include <QKeyEvent>
#include <QGraphicsScene>

Player::Player(QGraphicsItem *parent)
{
    bulletSound = new QMediaPlayer;
    bulletSound->setMedia(QUrl("qrc:/Sound/bullet.wav"));

    //setPixmap(QPixmap(":/Art/player.png"));
}

void Player::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Left){
        if(pos().x() > 10){
            setPos(x()-10, y());

    }
        }
    else if(event->key() == Qt::Key_Right){
        if(pos().x() < 590){
            setPos(x()+10, y());

    }
        }
    else if(event->key() == Qt::Key_Space){
        Bullet *bullet = new Bullet();
        bullet->setPos(x() + 45,y() + 5);
        scene()->addItem(bullet);

        //play sound
        if(bulletSound->state() == QMediaPlayer::PlayingState){ //if already playing
            bulletSound->setPosition(0); //set to start
            }
        else if(bulletSound->state() == QMediaPlayer::StoppedState){ //if not playing
            bulletSound->play(); //play
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
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).
Could you explain that a bit?
Anything in particular that's confusing you?

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.
Last edited on
Haphazardly introducing globals doesn't keep the code tidier than implementing a proper class structure.
Ok, will take your advice on that. I might just merge both classes to make it easier and cleaner.

Thank you.

Topic archived. No new replies allowed.