[Scripting] global variables?

+
[Scripting] global variables?

Anyone knows how to create a variable in REDkit script which I can access from different classes?

I tried
public class _global
{
public var tmpl4: CEntityTemplate;
}
but when using _global.tmpl4 I get the error: member of void.

I tried it with
struct _glob
{
editable var tmpl3: CEntityTemplate;
}

but to get access to it I have to put a
var globTest: _glob ;
in the scope where I'm calling from, for example a function not allowing global access.

The background: I'm spawning a Nekker in a state of a CStateMachine derived class. This is working fine:

tmpl1 = (CEntityTemplate)LoadResource("nekker_strong");
theGame.CreateEntity(tmpl1, pos, rot);

with var tmpl1 : CEntityTemplate;

But I need access to tmpl1 from outside the class to alter the behavior of the Nekker after spawning.

Any ideas?

edit: Looks like the vars in a state are being preserved.

So calling an additional entry function in the state should do the trick.
 
Last edited:
Stupid question - did you created object of class _global? :p Or did you made this class static (i'm not sure if witcher script have static classes though)
 
Stupid question - did you created object of class _global? :p
There's no "stupid question".:)
No I did not.

Or did you made this class static (i'm not sure if witcher script have static classes though)
The term "static class" isn't used in neither of the existing scripts. So is the term "public class".

But many classes are being imported. So are some public variables.

Since I might use vars in a state my "public var problem" seems to be solved.
But there are many others.
 
Last edited:
well... if there isn't static class and you haven't created object then you shouldn't be surprised that you can't get to the attribute :)
 
Yep. But I didn't want to instantiate an object. I simply needed a place to store a "global" pointer. And the only way to avoid an error message was to put the var in a class.

The script studio doesn't recognize/colorize the keyword 'static'.

But as I stated above the problem should be solved because the vars in a state seem to be preserved.
 
Last edited:
WitcherScript as far as i know is fully object oriented language, so global variables/procedures/functions cannot be declared. Only methods and attributes. Getting something like C global variable can be emulated with static class or having struct/class and then creating one instance of it.

And by the way... Why would you need global variable? Effect you want surely can be achieved without it. And the other thing is that using global variables in object oriented programming is bad habbit as far as i know. They should be used only when low level optimisation is required, and since you're not modifying C++ Engine code, it's not what you need to do.

And by the way - witcherscript language doesn't have pointers. Almost everything is working on references like in Java, C# and especially ActionScript which, by the look of witcherscript syntax, was inspiration for CDProjekt engine designers.

EDIT:
Ok... it seems that i was at least partially wrong - there is possibility of definning functions outside classes.
 
Last edited:
Top Bottom