how to pass a string array to a function?

BlitzMax Forums/BlitzMax Beginners Area/how to pass a string array to a function?

kaiserpc(Posted 2008) [#1]
Hi,

I've got a multi dimensional array of strings (terrainArray) that I need to pass to a function (loadMaps)

e.g.;

global terrainArray:String[28,28]
loadMaps("map.file",terrainArray)

for the loadmaps function declaration I'm not sure how to define the variable type

e.g.;

function loadMaps(mapFile:string, mapArray:????)

should it be a string[,]?


tonyg(Posted 2008) [#2]
What happens when you try?
Believe me your machine won't explode if you try it and get it wrong.
Just in case it does :
Global terrainArray:String[28,28]
terrainarray[0,0]="Hello"
terrainarray[0,1]="World!"
loadMaps("map.file",terrainArray)
Function loadMaps(mapFile:String, mapArray:String[,])
	Print maparray[0,0] + " " + maparray[0,1]
End Function

Works for me.


kaiserpc(Posted 2008) [#3]
;-)

I'm at work at the moment so can't try it. I was just thinking out loud about how to do this.

Thanks for the answer!

Actually after looking at your answer again I'm not sure it will work in my scenario, as I'm using the function to populate the array - so when I pass in the array it's empty. I'll try it tonight to see if it will work.


grable(Posted 2008) [#4]
I'm using the function to populate the array - so when I pass in the array it's empty.

Since Arrays are passed by reference, that shouldnt be a problem.
Unless its Size is 0, in that case use a Var parameter.

Function PopulateArray( a:Int[] Var)
  a = New Int[10]
  ...
EndFunction

Local array:Int[]
PopulateArray( array)
' array.Length = 10



Gabriel(Posted 2008) [#5]
Yep, you'll want the Var if you want to do any slicing to the array too.


tonyg(Posted 2008) [#6]
Actually after looking at your answer again I'm not sure it will work in my scenario, as I'm using the function to populate the array - so when I pass in the array it's empty.

Global terrainArray:String[28,28]
loadMaps("map.file",terrainArray)
Print terrainarray[0,1] + " " + terrainarray[0,0]
Function loadMaps(mapFile:String, mapArray:String[,])
	maparray[0,0]="Hello"
	maparray[0,1]="World!"
	Print maparray[0,0] + " " + maparray[0,1]
End Function



kaiserpc(Posted 2008) [#7]
yep cheers. Tried it now and it works fine :-)