BASIC

Author Topic: Global variable assign  (Read 2602 times)

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1774
    • View Profile
    • Homepage
Global variable assign
« on: 2007-Oct-21 »
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 [Select]
// IDE Version: 5.047

GLOBAL sx,sy,width
GETSCREENSIZE sx, sy
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
GLOBAL y2
y2 = sx / width
DEBUG " y2=" + y2 + " sx=" + sx + " width=" + width + "\n"
but I get this output:
 y1=-1.#IND sx=640 width=40
 y2=16 sx=640 width=40

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10695
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Global variable assign
« Reply #1 on: 2007-Oct-22 »
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 [Select]
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
If you don't have a local variable 'y1', better write:
Code: GLBasic [Select]
GLOBAL y1 // I know this one exists - don't warn me, OK!
y1 = sx/width
Even better: Name all global Variables gXXX or g_XXX, to make it obvious when you touch old code in a year again.

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1774
    • View Profile
    • Homepage
Global variable assign
« Reply #2 on: 2007-Oct-23 »
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 :-)