Posted: 1st Sep 2011 16:39
Hello,

Please could you explain how to set a global variable ?

I want to use a variable in several modules but it seems that variables are 'local' to their own modules ?...

Thanks for your help,
Alain
Posted: 1st Sep 2011 16:43
Add any globals into your main.agc file and they can be used in any included files.
Posted: 1st Sep 2011 17:11
Thanks for your answer, but it doesn't seems to work (with me )

my example :


main code :
+ Code Snippet
rem
rem AGK Application
rem
dim myValues [3]

#include "test.agc"

do
 print (myValues[1])
 print (myValues[2])
 print (myValues[3])

 Sync()
loop


the "test.agc"
+ Code Snippet
 myValues[1] = 10
 myValues[2] = 20
 myValues[3] = 42



What am I missing please ?

regards,
Alain
Posted: 1st Sep 2011 22:40
Just a guess, but the problem might be in your PRINT statements.

Try changing them to:

print (str(myValues[1]))
print (str(myValues[2]))
print (str(myValues[3]))

The Print statement is looking for string values, which your array is not, so you need to convert them to a string using the str() command.

Hope that was it.

Thanks
JHA
Posted: 1st Sep 2011 23:09
The type casting to string is not the problem here. The problem is that, when you include a new file, the code that it is in there, outside any function or subroutine, won't be executed like if they were in your main file.

If you add them to a function (or subroutine) in your test.agc file, and call this function at some point in your main.agc, then it will work.

Better show you than explain:

test.agc:

+ Code Snippet
Function SetMyValues()
     myValues[1] = 10
     myValues[2] = 20
     myValues[3] = 42
EndFunction


main.agc:

+ Code Snippet
dim myValues [3]

#include "test.agc"

SetMyValues()

do
 print (myValues[1])
 print (myValues[2])
 print (myValues[3])
 Sync()
Loop



You'll notice that in the main.agc, before the main loop starts, I'm calling the function, so the values are correctly set.

This is based on my observations, so maybe some TGC experts can confirm it


Cheers
Posted: 1st Sep 2011 23:52
Now that I look at it again, I do see the problem.

The values are never being set, because he is never calling a sub or function to actually set them.

DOH!! I should have seen that the first time.....

Good catch MobileCreator!!
Posted: 2nd Sep 2011 2:01
The Print statement is looking for string values, which your array is not, so you need to convert them to a string using the str() command.


That was my first thought too JHA
Posted: 2nd Sep 2011 10:18
There's no need to change them into strings. I use Print(array[0].thing) all the time and it prints integer and float values just fine.