orxObject definitions in other code units

edited October 2011 in Help request
I'm doing some code refactoring at the moment. I have a lot of orxObjects in the main.cpp file which I would very much like to separate out into another file.

I have some success but there is a problem. Say I start with this cut down code:

main.cpp
#include "orx.h"

orxOBJECT *droid;
orxOBJECT *droid2;

orxSTATUS orxFASTCALL Init()
{
	droid = orxObject_CreateFromConfig("DroidObject");
	droid2 = orxObject_CreateFromConfig("DroidObject2");
}


I would get two objects on screen. Next, I'd like to separate out some objects into another code unit. So, I'd like *droid2 to be placed in another file, declared there, and usable from main.cpp.

So I alter things to be:

main.cpp
#include "orx.h"
#include "separate.h"

orxOBJECT *droid;


orxSTATUS orxFASTCALL Init()
{
	droid = orxObject_CreateFromConfig("DroidObject");
	InitOtherObjects();
	orxObject_SetCurrentAnim(droid2, orxNULL ); //crash
}


separate.h
#ifndef SEPARATE_H
#define SEPARATE_H

#include "orx.h"

static orxOBJECT *droid2;

void InitOtherObjects();

#endif


separate.cpp
#include "separate.h"

void InitOtherObjects(){
	droid2 = orxObject_CreateFromConfig("DroidObject2");
}


Everything is good, everything compiles and runs, but when we get to the point of setting or using the droid2 orxObject, it is only an empty pointer, so using it crashes the program.

I realise this is really a c++ behavioral question. Anyone able to lend a hand here where I might be missing something?

(I have no issues if the separate file is a class and contains objects but this is not using a class)

Comments

  • edited October 2011
    Did you try including orx.h in your separate.h file?

    If you're running in debug mode, do you have the __orxDEBUG__ preprocessor definition set? Not having that flag caused my program to crash when referencing pointers.

    Also, why are you making droid2 static?
  • edited October 2011
    To declare a global variable, use the keyword 'extern'
    To define it, do not use the keyword 'static' as it means 'this is not a global definiton'

    Thus in separate.h :

    extern orxOBJECT *droid; //Declare global variable droid, ie: tell the linker to look for it's definiton in another file

    in separate.cpp:

    orxOBJECT *droid; //Define the global variable droid, this is the 'actual variable' per se.

    and in main.cpp, just include separate.h


    Now, if you had another file, something_else.cpp
    and you put:

    static orxObject *droid; //This variable is different than the one defined in separate.cpp

    Now you have two droid variables, one in something_else.cpp and one in separate.cpp. The static keyword tells the linker that the one in something_else.cpp is valid only within the file something_else.cpp, and thus, the 'extern orxObject *droid' declarations do not refer to it, but rather to the one in separate.h
  • edited October 2011
    Thanks for clearing up that difference between static & extern. I read some other things online but nothing that was as simple as that.

    I have made those changes as suggested, and I get a good compile and link but I still get an empty object and a crash when it is used. The code is now:

    main.cpp
    #include "orx.h"
    #include "separate.h"
    
    orxOBJECT *droid;
    
    
    orxSTATUS orxFASTCALL Init()
    {
    	droid = orxObject_CreateFromConfig("DroidObject");
    	InitOtherObjects();
    	orxObject_SetCurrentAnim(droid2, orxNULL ); //crash
    }
    


    separate.h
    #ifndef SEPARATE_H
    #define SEPARATE_H
    
    #include "orx.h"
    
    extern orxOBJECT *droid2;
    
    void InitOtherObjects();
    
    #endif
    


    separate.cpp
    #include "separate.h"
    
    orxOBJECT *droid2;
    
    void InitOtherObjects(){
    	droid2 = orxObject_CreateFromConfig("DroidObject2");
    }
    
  • edited October 2011
    Wind that all back! You are totally right, 4babce, it works as you suggested. I recreated the code again, but forgot to execute the InitOtherObjects() method, hence the object was null.

    In fact, if I move the declaration out, I don't need need the function as all.

    It short, thanks, it works.
  • edited October 2011
    Ah, glad to hear it!
Sign In or Register to comment.