View Full Version : How Global Variables Work
PointOfLight
10-16-2006, 05:13 PM
If I do the following:
inctest.ppl
global(GlobalVar$);
globaltest.ppl
#include "inctest.ppl"
func SetGlobalVar()
GlobalVar$ = "bob";
end;
func WinMain()
SetGlobalVar;
ShowMessage(GlobalVar$);
end;
what should I expect from GlobalVar$? I would assume that ShowMessage would display "bob", but instead it displays 0. In order to get this code to work, I have to change all instances of GlobalVar$ to GlobalVar%.
kornalius
10-16-2006, 05:19 PM
The Global() is a function. It needs to be executed in order to make the variable global. You will need to do the following in inctest.ppl
proc SetupGlobals
Global(GlobalVar$);
end;
Now in your WinMain() do the following:
SetupGlobals;
Btw, careful with FUNC's not returning values. PPL is intelligent enough to know and not crash but it is not good programming practice. ;)
PointOfLight
10-16-2006, 07:43 PM
So why does it work when I change the variable to GlobalVar%? Is it just because PPL sees the % the first time the variable is called and automatically makes it global?
What would be the chance of having PPL actually be able to execute "standalone code" in the future? NSBasic allows you to place code outside of any functions, and that code gets called before the first function does. It's actually a pretty nice little feature.
kornalius
10-16-2006, 11:42 PM
The % makes the variable global by default but throughout all PPL's running applications. If you have 5 apps running at the same time, they can share information with the variable% syntax.
You want to make code run outside the main function? It can easily be done with a proc, so I don't see the point really. I understand it could be a nice feature but having code all over the place is not really easy for the compiler to understand and can lead to badly programmed code.
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.