If I define a static var high up in the type-tree (such as at atom or atom/movable) and then try to set a different variable for something lower in the type tree (such as /mob or /obj) the compiler gives me the error 're-initialization of global var'.
Shouldn't there be a different instance of the static var for each individual type, not just one static var for every single type?
If not... How could I define a variable that would be present across any subtypes of the type i define it on, defaults to the setting I placed on the primary type, and can be changed on any subtypes? Oh.. and can be accessed like a static var.
Example: /obj:MyVar, instead of creating a new obj and then checking the var.
I CAN work around the issue in the manner I just said, which is simply loop through all of the typesof() and create one new object for each and check the var (using a non static var)... But I'd really like to not if at all possible.
ID:132893
![]() Aug 17 2009, 7:29 pm
|
|
![]() Aug 17 2009, 7:33 pm
|
|
Wait, when did we get static variables? Or is this just a hypothetical question?
|
DarkCampainger wrote:
Wait, when did we get static variables? Or is this just a hypothetical question? LoL! Yes we have static variables. Stealing a snippit from a previous discussion... obj/var/static/Test=1 If you unquote that last line it will produce errors because you're trying to change a const. The rest operates appropriately. (Someone had said that static was another word for const) But I have to say they aren't "fully" functional, mostly because people don't seem to use them much... I like them though. :( |
Aha, it confused me because it used the keyword static, but it isn't really static in the usual sense.
Apparently the static keyword is equivalent to the global keyword, which is why you're having this issue -- it isn't tied to the atom type at all. That is, if I'm understanding this correctly. I don't use variable modifiers other than tmp very often.. |
I'm not sure on that. I can't test right now, could you?
If you define: atom/var/static/PWN=1 And try to access it as world<<PWN does it work? Anyway~ Meh. Sucks. I'm looking for static in the sense that it is a variable tied to the 'type' not to any one instance of said type. |
Yep, outputs 1. But you defined it as an atom, which is a parent of /mob.
This shows that /obj/var/static/PWN and /mob/var/static/PWN are accessing the same variable: mob/var/static/PWN=23 Outputs: 23 23 23 |
Thats odd..
The compiler gives errors if you try to define the same var on a subtype, but apparently even if it isn't a subtype or parent it uses the same var if it is the same name. I don't believe that is intended. |
Static variables in DM are somewhat strange (note, however, that as Kaioken has drilled into everyone var/static is equivalent to var/global ;)).
var/a=10 Outputs: a: 10, 10, 11 b: 9000, 9000, 9000 The fact that global:b worked shows that a global variable is created for every static variable. So if you try to declare two static variables with the same name, you'll get an error (or at least unexpected results) because BYOND is also trying to make two global variables with the same name. |
DarkCampainger wrote:
Outputs: It's still not the proper result you'd expect, but interestingly, I actually get 23,1,1 instead when running that code. This shows that /obj/var/static/PWN and /mob/var/static/PWN are accessing the same variable: Well, with further testing, they don't - however, it does show that method of accessing the 'special' global var can't differentiate between identically-named vars. It works fine if you use a more standard/conventional method. There are 3 different methods to access such a 'special' global var: obj/var/global/NAM = "obj nam" (Only method 2 can be used with proc/verb global vars.) If you run that code, you'll see only method 1 can properly differentiate between the 2 global vars and output them both. Anyway, what you used here:
...is really method 2 above. As mentioned, the value those variables hold don't actually matter - they're not actually being read and used, contrary to what we believed previously. You can set them to any value you'd like and the result won't change; so this is why you can't use it to differentiate between identically-named vars - DM has no way of knowing which one to pick here, so it probably just grabs the first it found. This might even be done at compile-time like method 1 is. Also, about this in your code:
Someone might've missed that, so just making sure it's known the last line is of course just interpreted as src.PWN, and this corresponds to 'method 1' (yes, 1, not 3). Lastly, for completeness, the global:VAR syntax is essentially a different way of using method 2. Anyway, in light of the above, with non-uniquely-named 'special global vars', looks like you should stick to using method 1, or you might accidentally catch a different global var with the same name, eh. |