#Included functions don't see global Vars

Started by Draco9898, November 02, 2005, 10:57:26 PM

Previous topic - Next topic

Draco9898

Functions in source code that come from #includes don't recognize Global varibles. Anyway to fix this? I wanted to code very modularly...Or should I put the globals in the include as well?
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin

kevin

#1
Without an Example, i'm going to have to assume you'd done something like this.

PlayBASIC Code: [Select]
       #include "MyFunctions.pba"
Global SomeVariableUSedInMyFunctions

 

  This will not work.  Since the code is being built in a top down fashion, it'll first include and compile the entire source from the "MyFunctions.pba"  file  at the location of the #include statement, then continue on .  

  So the functions in your library are being compiled before the global variables are created. Thus any usage of the variable name in your functions will create local instances, rather than the global instance.


   eg


PlayBASIC Code: [Select]
Function BumpCounter()
Inc Counter
EndFunction Counter

Global Counter

Function BumpCounter2()
Inc Counter
EndFunction Counter

Do
Cls 0
print BumpCounter()
print BumpCounter2()
SYnc
loop




As you can see there are two functions in this example.  One prior to the global declaration and one after.  When you run this code the BumpCounter() function is using out LOCAL variable called "Counter" since it hasn't previously (to that point in the code)   seen a Global declaration called "Counter"  and the BumpCounter2() function    So Bumpcounter will return 1, and BumpCounter2() will return the global counter value + 1 forever.


   PlayBASIC documentation - #INCLUDE and other Compiler Directives