Communicating between classes

Yeah, I’m an oF/C+±newbie. I guess this question is pretty stupid, but I can’t seem to find a way to solve my problem.
I’m coming from Processing, so I’m used to being able to access a classes variables no matter what parent class instantiated it. I already know how to access the variables of a testApp child class from testApp itself (eg childClass.pos or childClass.getPos()), but what about communication between two classes both instantiated by testApp?
Say in testApp I have player and enemy objects. The enemies are supposed to follow the player around the screen, but even after extensive googling I couldn’t figure out how they can access the player variables.
What is best practice in this case? Is there a C+±tutorial that explains what to do (I already looked through the oF tutorials, especially the one for Processing users)?

Not sure if this helps but you could create a function in your enemy class which tells the class, the position of the player.
//Enemy.h class:
Create something like

void playerPos(ofPoint playerPos);

then in your testApp.cpp

void testApp::update(){



Or Not sure if this is the proper way but you could use the ofGetAppPtr() function

So in terms of your example you would have
class player: public ofBaseApp {

//something here

//the variable you want
ofPoint playerPos;


Then in:


#include “player.h”

class enemy {


player* playerCrossOverValues;


//enemy.cpp file

void enemy::setup(){

playerCrossOverValues = (player*)ofGetAppPtr();


void enemy::update(){

ofPoint enemySeekPos = playerCrossOverValues->playerPos;


hope it helps

You always need a reference to an object to access its variable, in our case, testApp will work as a bridge between the two. In Processing probably you wrote this part inside the draw() function, using global variables as well.

You can accomplish what you are trying to do, from the Update() function of testApp. You should loop through each enemy object and pass them a reference to the player object, or even simpler, pass the player XY position. Does this solve your problem?