ID:260807
 
I am using the variable!

To make things a little easier, I use global variables within objects. I have an item, let's say /obj/item/apple, and this has a global variable of fruit which is set to 1.

Pretty lame, but it suffices as an example. Performing the following action:

obj/item/apple
var
global
fruit = 1
obj/item/transmutator
//turns apples into oranges!
New()
. = ..()
spawn
var
obj/item/apple/I = new
if(I.fruit) //despite usage, still a warning
...
else
...
I = null


Now I'm using this method to obtain the value of the fruit variable. I now postulate the question if this is a glitch, or if I'm doing it wrong and I need to access this global variable some other way. Does anyone know?
I'm going to wager "because the global variable does not belong to that instance of apple". I'm going to assume in your "not apple but actual game" scenario, the object is not actually used as an instance object at all, more a container for static variables you intend to scope through object instantiation.

Where that warning is a bug is something of a matter for debating semantics and whether you should need an isntance variable to access something inherently static.
In response to Stephen001
Stephen001 wrote:
I'm going to wager "because the global variable does not belong to that instance of apple". I'm going to assume in your "not apple but actual game" scenario, the object is not actually used as an instance object at all, more a container for static variables you intend to scope through object instantiation.

I realize that the variable is a global variable, but I know of no other way to get to it since I defined it in that object. I was wondering if the error meant that I was doing it wrong and had to access it some other way.
In response to Android Data
Well, the warning suggests a gap in the language set. I wouldn't say it's a bug, so much as a lack of feature.
Technically, you do not have to create a new instance of the apple to access the global variables, only have a variable of the correct type. However: you'd still get the warning if you did that.
I don't even understand why you'd want to do something like that instead of using a non-global variable and normal typecasting.
In response to Android Data
Well you can always access variables from different types using the : operator

But since I am not completely sure about correct use of the : operator... I personally tend to avoid it...

obj
lemon
var/sour

mob/verb/lalala(var/obj/O in contents)
if(O:sour)
src << "OMG!"
In response to Nielz
Nielz wrote:
Well you can always access variables from different types using the : operator

That has absolutely nothing to do with whats being discussed here, and that example you posted would cause a runtime error if the object was anything except a lemon...
In response to Falacy
Falacy wrote:
Nielz wrote:
Well you can always access variables from different types using the : operator

That has absolutely nothing to do with whats being discussed here, and that example you posted would cause a runtime error if the object was anything except a lemon...



Nielz wrote:
But since I am not completely sure about correct use of the : operator... I personally tend to avoid it...
In response to Nielz
Nielz wrote:
But since I am not completely sure about correct use of the : operator... I personally tend to avoid it...

  • "It is used to access a property of a var that is not explicitly prototyped." Technically, its supposed to ignore all type-casting, but currently it only checks to the highest defined non-atom level (ie: your example would check all obj variables, but not areas, mobs, or turfs).
  • Then you shouldn't be telling other people to use it?
  • As Nadrew said, unless AD is doing something incredibly unusual here then there's probably not a reason to even have declared it as a global variable. Though it does seem to most likely be a compiler error either way.
In response to Nadrew
Nadrew wrote:
I don't even understand why you'd want to do something like that instead of using a non-global variable and normal typecasting.

I have an object which has global properties. If I define the variable as global, they're the same on all other instances of that object. So if I create two apples, and change fruit=0 on one of them, the other one will also have its fruit variable set to 0.

This can be quite handy in some cases. In my case, I have a "super secret teleporter" object that stores a randomized "base code" needed to activate such a device as well as a random center point on the map to derive coordinates from. By using a global variable I can ensure that these variables remain the same for every instance of that object.

The only issue is with accessing this variable: creating a new instance of the object and referencing to this variable isn't seen by the compiler as valid usage, so it gives me a warning "the variable was defined but not used". I know of no other way to access the variables within an object.

It would be nice if I could do something like /obj/apple.fruit or something. I don't know how feasible that would be at 7 AM in the morning, but eh.
In response to Android Data
I think instead of calling it 'global' using the 'static' keyword may resolve your issue, they're essentially the same thing except the compiler looks at them differently. I haven't tested it myself, though.
In response to Android Data
Android Data wrote:
I have an object which has global properties. If I define the variable as global, they're the same on all other instances of that object. So if I create two apples, and change fruit=0 on one of them, the other one will also have its fruit variable set to 0.

Between your previous example, and what you explained in this replied to post; I see no reason that either normal type casting, or normal (non type cast) global variables wouldn't work.

And I tested what Nadrew suggested (using a static variable) it doesn't resolve the issue, leaves you with the same compile error.
Weird no errors with this

obj/item/apple/var/global/var/fruit = 1
obj/item/transmutator
//turns apples into oranges!
New()
. = ..()
spawn
var
obj/item/apple/I = new
if(I.fruit) //despite usage, still a warning
...
else
...
I = null
In response to Android Data
Note that as a workaround, to suppress the error, you could always just use a regular global variable, or include a dummy proc somewhere that explicitly uses the var, so a not-used warning isn't generated.

Android Data wrote:
The only issue is with accessing this variable: creating a new instance of the object and referencing to this variable isn't seen by the compiler as valid usage, so it gives me a warning "the variable was defined but not used".

What you're doing is seen as valid usage, or of course it would've generated an error instead of being successfully compiled. What you're experiencing would simply be a bug or oversight causing the compiler not to realize the var was "used" and so it displays said warning.

I know of no other way to access the variables within an object.

As Garthor had already mentioned in this thread, you can always access such a variable from everywhere by freely using the : or . operators, without actually needing an object reference to an instance of the object - since that var is really just a global var.
Note using the . operator is more recommended, since the other one is more error-prone; if you have multiple global vars with the same name, you'll only be able to grab one of them using it, which may not necessarily be the one you want.

It would be nice if I could do something like /obj/apple.fruit or something.

As mentioned, you can do such a thing already:
obj/something
var/global/myglob = 234
mob/verb/glob()
var/obj/something/O
src << O.myglob //outputs 234

In fact, this is what you'll always be doing (you're doing it as well); whenever you access such a global var (object_var.global_var, same with : operator), DM never actually reads the value of the object var you're using - so in your given example, the value of I doesn't actually matter, it can be anything and it will work the same. That would be why the compiler claims the variable isn't used, because the compiler isn't actually using the var itself, but simply reading its compile-time-only defined-type value, and compiling a statement to access the desired global var. Of course, even so, this warning shouldn't be generated.
(The above also means you don't have to use an object var at all since you can do something like null:global_var to access it, but as said using : is less recommended.)