Global variables, using Particle and soundStream—very simple question

Hi there,
I am new to OF, I hope you won’t mind my ultrasimple question.

I am working at drawing some Particles, so far I have kept everything divided into:
main.cpp
testApp.cpp //here in the setup I add the particles to a vector, also I am using SoundStream and here I have all the code
testApp.h

//This is where I have all the elements re the particles
Particle.cpp
Particle.h

Now, provided I have scaledVol as a float in test.cpp, how can I let it control the way I draw particles?
If I use scaledVol in the Particle.cpp OF won’t recognise it (Use of undeclared identifier)

I guess this has to do with either creating a var that can be accessed from everywhere or…

Many thanks in advance for your help, I can post the code if needed, although it is a bit too long…

Best

Faeviuz

If you really want to access scaledVol from everywhere, the simple approach would be to make it globally accessible (do a search on “global” or “static” variables). For a simple program that might be feasible.

When things get more complicated though, it’s easy to lose track of where and how global variables are modified. That’s why I would usually prefer passing the value as parameter to a function call on Particle or have Particle use a reference to an object that is updated with the current value.

Thanks for suggesting Sinasonic,
yes, after a bit of search, it looks like the best way is that of using a pointer/function.
Truth is that I come from Processing and I am still finding my way around, there in P you can pretty much always access variables from wherever you are, depending on how you declared them.
I will give it a go and see how it goes!

I did it in the end, I managed to pass the parameter to the function on Particle and then access that from the testApp.cpp.

Thanks!

Here is a delegate style example that may take more setup time but avoids globals

Create a class for testApp to inherit
ParticleDelegate.h

  
  
#pragma once  
  
#include "ofMain.h"  
  
class ParticleDelegate  
{  
public:  
	virtual void onParticleMessage( ofVec2f particleCoordinate ) = 0;  
	  
};  
  

add it to testApp.h as well as any functions it inherits

  
  
#pragma once  
  
#include "ofMain.h"  
#include "Particle.h"  
#include "ParticleDelegate.h"  
  
class testApp : public ofBaseApp, public ParticleDelegate //additional functions  
{  
  
	public:  
		void setup();  
		void update();  
		//removed rest of stuff for example  
	Particle particle;  
	void onParticleMessage( ofVec2f particleCoordinate );  
		  
};  
  

Sample Particle Class (Particle.h)

  
  
#pragma once  
  
#include "ofMain.h"  
#include "ParticleDelegate.h"  
  
class Particle  
{  
public:  
	Particle();  
	void setDelegate(ParticleDelegate *aDelegate);  
	void doSomething();  
	ParticleDelegate *delegate;  
};  
  

Sample Particle Class implementation (Particle.cpp)

  
  
#include "Particle.h"  
  
Particle::Particle()  
{  
	delegate = NULL;  
}  
  
void Particle::setDelegate(ParticleDelegate *aDelegate)  
{  
	delegate = aDelegate;  
}  
  
void Particle::doSomething()  
{  
	if (delegate != NULL)   
	{  
		ofVec2f particleCoord(ofRandom(ofGetWidth()), ofRandom(ofGetHeight()));  
		delegate->onParticleMessage(particleCoord);  
	}  
}  
  

back in testApp.cpp

  
  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
	particle.setDelegate(this);  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
	particle.doSomething();  
}  
  
//--------------------------------------------------------------  
void testApp::onParticleMessage( ofVec2f particleCoordinate ){  
	cout << "onParticleMessage: " << particleCoordinate.x << " " << particleCoordinate.y << endl;  
}  
  

Not a very exciting particle system but hopefully opens up some options :slight_smile:

1 Like