General query regarding functions

Hi

I need some advice about implementing functions.

Say for example I want to write a simple function that loads a movie from a directory and then plays it.
Also I would like to know how to access a specific instance and variables used inside of the function outside of the function (a question of scope?). In this pseudo code I have instance ‘movPlayer’ and the variable ‘truth’.

So for example

  
  
void playMovieInDir(ofDirectory dir, truth bool)  
	{  
	movPlayer.loadMovie(dir.getPath(ofRandom(0,dir.size())),mode);  
	movPlayer.play();   
	truth = false;  
	}  
  
void movieApp::update(){  
	  
	if (something)  
	{ playMovieInDir(folderA)}  
  
	if (something_else)  
	{ movPlayer.stop}  
  
	if (truth == false && someothercondition)  
	{ ................  
	movPlayer..loadMovie("blah blah blah.mp4")  
	.........}  
	  
	etc  
  
}  

Thanks in advance for any help

Daniel

You can’t access a parameter of a function outside of the function definition. From within the update function, the parameters of any other function would be out of scope.

So there are a few ways to design your code to allow for the truth variable to be in scope in both the update and playMovieInDir functions.

  1. declare truth as a global variable and remove it as a parameter to playMovieInDir().
    This is the simplest and arguably the ‘messiest’ or least elegant solution. if you were to do this for every function write, you’d have a huge list of global variables to keep track of.

  2. declare playMovieInDir() and truth as members of the movieApp class, and remove truth as a parameter to playMovieInDir().
    This is a little better as truth is no longer a global variable, but it might as well be because it is available to all of the methods of testApp, which is where you’d be writing the bulk of your code. May be the best solution if multiple methods need to access this variable.

  3. declare the parameter truth as pass by reference variable (bool &truth), and pass a variable in the update function’s scope to the function playMovieInDir()

  
  
void playMovieInDir(ofDirectory dir, truth &bool)    
    {    
    ....  
    }    
    
void movieApp::update(){    
    bool truth;  
    playMovieInDir(folderA, truth);  
    
    if (truth)    
    { ................  }  
}    
  

this seems to be in the spirit of the original code you had, and maybe what you wanted to do in the first place, the difference is you need to declare a boolean variable in the update function in order to pass it to the playMovieInDir() function. However, passing by reference in this case seems a bit overboard and is generally used in a case where multiple values need to be returned, you want to reduce the overhead of copying a large data structure, or you want to modify an external data structure in some other way.

  1. declare playMovieInDir() as a bool, and return the truth variable
  
  
bool playMovieInDir(ofDirectory dir)    
    {    
    movPlayer.loadMovie(dir.getPath(ofRandom(0,dir.size())),mode);    
    movPlayer.play();     
    return false;    
    }   
  

In this specific case, if you want to check a single value that a function will set, its probably the cleanest and most elegant solution to simply return that value, then store the return value in a boolean within the update function, or just check it directly.

  
  
void movieApp::update(){    
      bool truth;  
    if (something)    
    { truth = playMovieInDir(folderA)}    
    
    if (something_else)    
    { movPlayer.stop}    
    
    if (truth == false && someothercondition)    
    { ................    
    movPlayer..loadMovie("blah blah blah.mp4")    
    .........}    
        
    etc    
    
}    
  

Hope this helps and isnt too confusing. I also think that in general you should be declaring additional functions as methods of your OF App’s class, as well as any variables or objects you want to use. so movPlayer should be a member variable of movieApp, and playMovieInDir() should be a member function (method) of moveiApp as well.

I would just extend the ofVideoPlayer Class add a returning bool like Tim very beautifully illustrated.
or have it as a void function and have an additional returning boolean getisloadedandPlaying()
That way you could have a state for all your videos and not just one.

Also you could just access the bool ofVideoPlayer::getIsPlaying() instead of creating a new one.

in a sense… it’s the same because in order to play a video you have to load it first.

you could also access the getisLoad() bool of the ofVideoPlayer object

  
  
///.h file:  
  
class myvid : public ofVideoPlayer{  
    
    public:  
    void loadAndPlay(string adr){  
          
        ofVideoPlayer::loadMovie(adr);  
        ofVideoPlayer::play();  
    }  
};  
  
//in testapp.h class declaration  
  
  
class testApp : public ofBaseApp{  
  
	public:  
		void setup();  
		void update();  
		void draw();  
      
      
    myvid movie; /////declare your instance  
      
};  
  
  
      
//.cpp file:  
  
if(!movie.isLoaded())movie.loadAndPlay("movie.mov");  
  
  
  
//in draw():  
  
if(movie.isPlaying()){movie.idle();movie.draw(0,0);}  
  
  
};