Author Topic: Globals -- Bug or working as intended?  (Read 1637 times)

Hatonastick

  • Guest
I had (what seemed to me at least) some odd behaviour with some global arrays in my current project.

For example (not compilable as is, just illustrating my point) I have three files in my project: GLBMUD.gbas (main file), Events.gbas, Globals.gbas

I get an error in this scenario:
GLBMUD.gbas:
Code in here, but nothing related to this scenario.

Events.gbas
Code: (glbasic) [Select]
FUNCTION EventAdd: timer%, call%
LOCAL event AS EventData

event.Timer% = timer%
event.Call% = call%
DIMPUSH EventQueue[], event    <- Error is declared to be here: wrong argument type :
SORTARRAY EventQueue[], 0
ENDFUNCTION

Globals.gbas:
Code: (glbasic) [Select]
TYPE EventData
Timer%
Call%
ENDTYPE

GLOBAL EventQueue[] AS EventData

However if I change the above to this:

GLBMUD.gbas:
Code: (glbasic) [Select]
GLOBAL EventQueue[] AS EventData

Events.gbas
Code: (glbasic) [Select]
FUNCTION EventAdd: timer%, call%
LOCAL event AS EventData

event.Timer% = timer%
event.Call% = call%
DIMPUSH EventQueue[], event
SORTARRAY EventQueue[], 0
ENDFUNCTION

Globals.gbas:
Code: (glbasic) [Select]
TYPE EventData
Timer%
Call%
ENDTYPE
Everything then compiles fine and the error disappears.

Now I'm not even sure this is a bug, I suspect it might be related to the order in which GLB compiles things and is working as intended.  There isn't a way to force GLB to compile things in a particular order, correct? ie. like you can with C.  Not a big deal, just had me scratching my head. :)

Edit: It just occurred to me, should there be a DIM EventQueue[] in there somewhere?
« Last Edit: 2009-Mar-06 by Hatonastick »

MrTAToad

  • Guest
Re: Globals -- Bug or working as intended?
« Reply #1 on: 2009-Mar-06 »
You have to be somewhat careful about where arrays are defined, especially when they are scattered around separate source files, as the main program may not be able the correct type.