-
Static variables
I have a few global variables that must be declared static or linker reports an error when I try to use them from the action...
Code:
static float gl_variable;
I want to doing this in runtime.cpp:
Code:
short WINAPI DLLExport HandleRunObject(LPRDATA rdPtr)
{
gl_variable += 0.01; // this is a global variable
return LOOP_DONE; // equals 0, function runs each loop
}
This compiles ok, but when I try to get the variable with an expression, it is always the same, no matter that it should be 0.01 greater each loop.
Why does HandleRunObject() ignore this static variable?
Can static variables be changed from nonstatic function at all?
How can I change the global variable?
-
Re: Static variables
static variables can be changed from nonstatic methods, im quite sure.
You should debug your extension, put a breakpoint on that line.
-
Re: Static variables
"static" means that the variable is only in that file. In other words, you have a different variable with the same name in runtime.cpp vs main.cpp.
*** You're not seeing the change in value because you're reading from a different variable. ***
What are the messages you get if you remove "static"?
EDIT: One is probably that it's uninitialised. You should add "= 0" to it.
-
Re: Static variables
i get lots of
Code:
error LNK2005: "int Operation" (?Operation@@3HA) already defined in Edittime.obj
I don't know exactly how linker works, so I don't know how to avoid this error...
maybe I am declaring variable in the wrong place?
Where should I put them to be in global scope?
Currently I have tham above the next code in common.h
Code:
// Globals and Prototypes
#include "myGlobals.h"
extern HINSTANCE hInstLib;
extern short conditionsInfos[];
extern short actionsInfos[];
extern short expressionsInfos[];
i've tried to exclude myGlobals.h from build but the result is the same...
-
Re: Static variables
I even tried to put these global variables into its own namespace:
Code:
namespace gl {
int operation;
}
and then use gl::operation, but that too is not working...
If you have any advice, please say, because I really need global object and I can't continue the development until I solve this issue...
Thank you.
-
Re: Static variables
Why can't you stick it in rdPtr? To avoid the linker error, in common.h
extern int operation;
and then in any single CPP file
int operation;
-
Re: Static variables
I can't stick it in rdPtr because the data from rdPtr is deleted when frame ends... and I need it to be global throughout the entire application.
@Jam: if I follow your example, should I put "int operation;" outside any scope in cpp file or in function where I wanna use the variable?
-
Re: Static variables
-
Re: Static variables
Yeah, put "int operation=0;" in one file and "extern int operation;" in the others where you want to use it.
I don't remember offhand, but putting extern on the other one might work too.
You might notice that the globals in common.h all have "extern".
-
Re: Static variables
Thank you, I got it... I've done it this way
file globals.cpp
Code:
int Mode = 1;
int Operation = 0;
file globals.h
Code:
extern int Mode;
extern int Operation;
file common.h
Code:
...
#include "globals.h"
...
This appears to be working... I've posted this solution here if anyone can find this useful...
Thank you, guys.
-
Re: Static variables
You know there is a SDK runtime function for global data?
-
Re: Static variables
no, what is it? (in standard MMF SDK)... i was using rSDK but I think I'll be able to find it there...
-
Re: Static variables
-
Re: Static variables
Yes, if you have a variable that is global to the application, you should store it with mvSetExtUserData and retrieve it with mvGetExtUserData.
Because if you use a global variable in the global heap of the extension, you have to know that :
- it will be shared by all the sub-applications too.
- if your extension is played in a Vitalize! 4 application, then the variable will be shared by all the CCN applications played in the same instance of the browser (as V4 is executed in multi-thread mode).
-
Re: Static variables
I want it to be shared by all subapplications...
Another question - If I use the global variable (and NOT using the mvGetExtUserData), will it be shared with the same extension in another instance of the same application?
For my game I will not allow another instance, but I just wanted to know if it is shared even across applications.
-
Re: Static variables
No, the global variable won't be shared with other stand-alone applications.