Main forum > Bug Reports

Global variable assign


This bug is back again, I can't assign a value to a variable in a global declaration

In the code below the two debug lines are supposed to return the same values.

--- Code: (glbasic) ---// IDE Version: 5.047

GLOBAL sx,sy,width
width = 40

// This gets the wrong result
GLOBAL y1 = sx / width
DEBUG " y1=" + y1 + " sx=" + sx + " width=" + width + "\n"

// If I do the same but split the line I get the correct result
y2 = sx / width
DEBUG " y2=" + y2 + " sx=" + sx + " width=" + width + "\n"
--- End code ---
but I get this output:
 y1=-1.#IND sx=640 width=40
 y2=16 sx=640 width=40

Kitty Hello:
Yikes!!! The GLOBAL a=1 will set the global variable at the first time you use it to '1' -> before the program starts.
What you want is:

--- Code: (glbasic) ---GLOBAL y1 // Make a global variable "y1", and assign '0' at program start
GLOBAL y1 = sx/width  // assign the global variable 'y1' with sx/w
--- End code ---
If you don't have a local variable 'y1', better write:

--- Code: (glbasic) ---GLOBAL y1 // I know this one exists - don't warn me, OK!
y1 = sx/width
--- End code ---
Even better: Name all global Variables gXXX or g_XXX, to make it obvious when you touch old code in a year again.

Aha, then it works like I was expecting it to work. I just misunderstood one of the examples somewhere. Funny thing it works every other version you put out :-)

Now I removed it again from my game and split it on separate lines so won't see that problem again. Thanks :-)


[0] Message Index

Go to full version