Posted: 24th Aug 2011 23:22
Hi All

Is it possible in Tier 1 to pass a user defined type as a parameter to a function?

Thanks
Posted: 25th Aug 2011 2:39
Yes it is.

Method 1 - global
+ Code Snippet
//Create the type
Type MyType
   Myint
EndType

//Set it as a global so it reaches all functions in the program
Global MyUTD as MyType

//Start the main loop
Do
    //Call the function
    PrintCount()
//Update the screen data
Sync()
//End the loop
Loop

//Create the function and assign temp as a value
Function PrintCount()
    //Increase the value of the created global
    MyUTD.Myint = MyUTD.Myint + 1
    //Print the value of creatd global
    Print(MyUTD.Myint)
//End the function
EndFunction


Method 2 - Passing into function and back out
+ Code Snippet
//Create the type
Type MyType
   Myint
EndType

//Assign MyUDT to MyType
MyUTD as MyType

//Start the main loop
Do
    //Assign the content of the PrintCount function to MyUDT.Myint and pass it back into the function.
    MyUTD.Myint = PrintCount(MyUTD.Myint)
//Update the screen data
Sync()
//End the loop
Loop

//Create the function and assign temp as a value
Function PrintCount(temp)
    //Increase the value of temp
    temp = temp + 1 
    //Print the value of temp
    Print(temp)
//Pass temp out of the function and into the main program
EndFunction temp
Posted: 25th Aug 2011 3:18
AGK supports passing a UDT into a function, and also returning a UDT from a function.
But at the moment passing a UDT into a function causes a crash. The bug as been reported and it will be fixed in the next update.

Passing UDT to Function (Will be fixed next update):
+ Code Snippet
TYPE testType
xPos as Integer
yPos as Integer
ENDTYPE

myvar as testType
myvar.xPos = 0
myvar.yPos = 0

adtTest(myvar)

do
 Print(myvar.xPos)
 Print(myvar.yPos)
 Sync()
loop


FUNCTION adtTest(test as testType)

    test.xPos = 100
    test.yPos = 150

ENDFUNCTION


Returning UDT from function(Works!):
+ Code Snippet
TYPE testType
xPos as Integer
yPos as Integer
ENDTYPE

myvar as testType
myvar = new_Test()

do
 Print(myvar.xPos)
 Print(myvar.yPos)
 Sync()
loop


FUNCTION new_Test()

    temp as testType
    temp.xPos = 100
    temp.yPos = 250

ENDFUNCTION temp