Global arrays

BlitzPlus Forums/BlitzPlus Programming/Global arrays

Leon Brown(Posted 2005) [#1]
Does anybody know how to identify an array as global? The instructions say that arrays are automatically set as global, but they are never recognised when I try to access them from my functions.


Kevin_(Posted 2005) [#2]
Arrays are Global by default Game Boy (not strictly true but keep to the basics first).

You may be trying to access them the wrong way. If you post a little bit of code showing your array declaration and the function that is trying to access the array, I may be able to help.


Banshee(Posted 2005) [#3]
Example of global array usage,

dim myArray(100)

myArray(1)=1

myFunction()

function myFunction()
   print myArray(1)
end function


Declare the array in the main program before the function call and your array will be accessible in all functions called thereafter.


Leon Brown(Posted 2005) [#4]
The following is identified in the main code:

Dim spellings$(10)


Then the function goes as follows:

function example()
  spellings$(0) = "test"
end function


Many thanks for your help in advance.


Kevin_(Posted 2005) [#5]
Try this...

[Code]
Graphics 640,480,32,2
SetBuffer BackBuffer()

Dim spellings$(10)
example

Text 10,10,spellings$(0)
Flip

Repeat
Until KeyHit(1)
End


Function example()
spellings$(0) = "test"
End Function

[/Code]


Leon Brown(Posted 2005) [#6]
Solved the problem :-). I've included all of the functions at the top of the code before the arrays were defined, which made the compiler think that the arrays were never defined.

Many thanks for all your help Prof and Becky - very much appreciated.


Grey Alien(Posted 2005) [#7]
Prof's code should work. you must be doing something funny Gameboy. When you say the array is not recognised, do you mean by the compiler, or that it appears that way because when you access an array slot (to print for example) later, you are getting null values? A bugbear I have with the Blitz IDE is that it doesn't warn you if you type a variable name incorrectly and so many times I have wondered why my code doesn't work only to find a misplaced/missing letter. Also if you pass a string into a funciton and forget to stick $ on the end of it, it's treated as an integer and your output is always 0 (this isn't a compiler issue really, just me being dumb)


Kevin_(Posted 2005) [#8]
Game Boy...

It always helps if you post a bit of code first :)


Leon Brown(Posted 2005) [#9]
the code looked like this
include functions here...

dim array(10)
...etc...

function example()
  array(0) = 10
end function


It was becuase the functions were at the top, the complier hadn't read the definition of the array, hence it thought that it didn't exist.

Now that I have included all extra files at the bottom of the code, everything is fine.