jo-thijs
Member
That doesn't work though, you cannot use periods in macro names.I'm using GM2, and with the nice macro features, one can "replace" any bit of information by any bit of code.
I agree it's just eye-candy, but it makes my eyes happy when I read my code.Code:#macro Enemy.count global.EnemyCount // I can now use Enemy.count in the game, and at compile time it will be replaced by global.EnemyCount.
GM will interprete it as though you defined "Enemy" as ".count global.EnemyCount".
Yes, that's what he's trying to do.This sounds like you're misunderstanding instantiation in GMS as a whole. Code placed in the object form are not constants as such, and none of the values will exist if there are no instances of the objects.
I read this whole thread twice and still seem to be misunderstanding the issue. It seems a bit like you're trying to apply something from another language that isn't really applicable at all.
Are you trying to access variables set in a code form in an event of an object that is never created? None of those events are performed if the instance never exists.
Why wouldn't you use global values or just create a controller object that handles these "static fields"?
He wants to associate a value "gold_buy" to some objects, so that the value only depends on the object index.
For example, buying item 1 will cost 100 gold, buying item 2 will cost 250 gold.
In java, this would conceptually correspond to static variables.
He then obtains an object index in some variable "obj" and wants to retrieve the associated "gold_buy" value.
This is where the analogy with java fails.
This is also why just using global variables won't suffice.
He also wants to organize his code in a specific way though.
He doesn't like putting the values of "gold_buy" in 1 spot, but wants to have the values be declared in proximity to the objects they relate to (in their create events for example).
Hence, he doesn't like the controller solution, as the values would be defined somewhere outside the objects they relate to.
I believe it would be best to just have a global variable "global.gold_buy_map" that contains a ds_map that maps object indices to their respective "gold_buy" values,
to have a script "scr_associate_gold_buy(object, value)" that checks if the global variable has been initialized and initializes it with a newly created ds_map if not and then adds the new association to it.
Finally, using gml_pragme with "global" as first argument, he can call this script in the create event of all the relevant objects to make the associations in exactly te way he wants it to be organized.
The only disdvantge is that he'd be working with something of the form "global.var_name[?'obj']" or by using a script for it instead of using "obj.var_name".
This doesn't seem like a bad trade off though.
There are multiple tricks to know if the global variable has been initialized in GM:S 2.
First of all, there is just a function whoms purpose is just that, so that can be used.
Alternatively (I don't know if it's better), there's some trick with arrays that can be used.
Code:
global.inited[1] = 0;
if global.inited[0] == 0 {
// initialize your other global variables
global.inited[0] = 1;
}