I have to draw the same conclusion as Ancient lady.
My test code was a bit more extensive, but the findings are the same; in the nested call to "Vector2_Length", the Vector2.y value get's zapped for some reason -- a problem related to the UDT implementation in AGK.
Here's my test code (which I tested in AppGameKit 1076):
// ------------------------------------------------------------
// Keyboard input support
// ------------------------------------------------------------
// Declare constants
#constant KEY_0 0x30
#constant KEY_1 0x31
#constant KEY_2 0x32
#constant KEY_3 0x33
#constant KEY_4 0x34
#constant KEY_5 0x35
#constant KEY_6 0x36
#constant KEY_7 0x37
#constant KEY_8 0x38
#constant KEY_9 0x39
MyKeypress as integer
// ------------------------------------------------------------
// Buffered print output support
// ------------------------------------------------------------
// Declare data type
type TDebugOutput
CntLines as integer
NumLines as integer
MaxLines as integer
endtype
// Create a global variable using custom data type
global DebugOutput as TDebugOutput
// Set defaults
DebugOutput.CntLines = 0
DebugOutput.NumLines = 0
DebugOutput.MaxLines = 50 // Change this to fit your needs
// Create the buffer array to hold "MaxLines" entries
global dim DebugOutputArray[DebugOutput.MaxLines] as string
// ------------------------------------------------------------
// DATA TO DEMONSTRATE UDT PARAMETER BUG
// ------------------------------------------------------------
// Declare data type
type Vector2
x as float
y as float
endtype
// Declare and initialize variables (outer scope locals)
myvector as Vector2
myvector.x = 10
myvector.y = 20
// ------------------------------------------------------------
// TEST CODE
// ------------------------------------------------------------
// Setup display (percentage based landscape orientation)
SetDisplayAspect(1.33)
SetPrintSize(2.0)
// Output some help info...
ShowHelp()
DebugOutputAdd("Initial vector data:")
DebugOutputAdd(" myvector.x = " + str(myvector.x))
DebugOutputAdd(" myvector.y = " + str(myvector.y))
DebugOutputAdd("")
do
// Show output
DebugOutputShow()
// Update display (and update device input flags)
Sync()
// Did user press a key?
MyKeypress = GetAnyKey()
select MyKeypress
case KEY_1: myvector = Vector2_Reset(myvector) : endcase
case KEY_2: myvector = Vector2_Normalize_Works(myvector) : endcase
case KEY_3: myvector = Vector2_Normalize_Fails(myvector) : endcase
case KEY_4: DebugOutputClear() : ShowHelp() : endcase
case KEY_0: exit : endcase
endselect
loop
function Vector2_Reset(v as Vector2)
// Reset values and return
v.x = 10
v.y = 20
DebugOutputAdd("FUNCTION - Vector2_Reset")
DebugOutputAdd(" Output: ")
DebugOutputAdd(" v.x = " + str(v.x))
DebugOutputAdd(" v.y = " + str(v.y))
DebugOutputAdd("")
endfunction v
function Vector2_Normalize_Fails(v as Vector2)
l as float
DebugOutputAdd("FUNCTION - Vector2_Normalize_Fails")
DebugOutputAdd(" Input:")
DebugOutputAdd(" v.x = " + str(v.x))
DebugOutputAdd(" v.y = " + str(v.y))
// Compute length (via function call)
l = Vector2_Length(v) // <-- this call messes up the result
// Normalize
v.x = v.x/l
v.y = v.y/l
DebugOutputAdd(" Output: ")
DebugOutputAdd(" v.x = " + str(v.x))
DebugOutputAdd(" v.y = " + str(v.y))
DebugOutputAdd("")
endfunction v
function Vector2_Length(v as Vector2)
l as float
// Get distance from 0,0 to x,y
l = sqrt(v.x*v.x+v.y*v.y)
DebugOutputAdd("FUNCTION - Vector2_Length")
DebugOutputAdd(" Input:")
DebugOutputAdd(" v.x = " + str(v.x))
DebugOutputAdd(" v.y = " + str(v.y))
DebugOutputAdd(" Output: ")
DebugOutputAdd(" l = " + str(l))
DebugOutputAdd("")
endfunction l
// Works as expected...
function Vector2_Normalize_Works(v as Vector2)
l as float
DebugOutputAdd("FUNCTION - Vector2_Normalize_Works")
DebugOutputAdd(" Input: ")
DebugOutputAdd(" v.x = " + str(v.x))
DebugOutputAdd(" v.y = " + str(v.y))
// Compute length (inline)
l = sqrt(v.x*v.x+v.y*v.y)
DebugOutputAdd(" Length (inline calculation): ")
DebugOutputAdd(" l = " + str(l))
// Normalize
v.x = v.x/l
v.y = v.y/l
DebugOutputAdd(" Output: ")
DebugOutputAdd(" v.x = " + str(v.x))
DebugOutputAdd(" v.y = " + str(v.y))
DebugOutputAdd("")
endfunction v
// ------------------------------------------------------------
// SUPPORT ROUTINES ONLY
// ------------------------------------------------------------
function ShowHelp()
DebugOutputAdd("Keyboard help:")
DebugOutputAdd(" 1 = Initialize UDT data (reset)")
DebugOutputAdd(" 2 = Normalize vector (this works)")
DebugOutputAdd(" 3 = Normalize vector (this fails)")
DebugOutputAdd(" 4 = Clear Screen + Show help")
DebugOutputAdd(" 0 = Quit!")
DebugOutputAdd("")
endfunction
function GetAnyKey()
v_Scancode as integer
v_Result as integer
v_Result = 0
v_Scancode = GetRawLastKey()
if (GetRawKeyReleased(v_Scancode)) then v_Result = v_Scancode
endfunction v_Result
function DebugOutputAdd(a_strText as string)
INC DebugOutput.NumLines
if (DebugOutput.NumLines > DebugOutput.MaxLines)
DebugOutput.NumLines = DebugOutput.MaxLines
DebugOutputScroll() // This makes room for one more line
endif
inc DebugOutput.CntLines
DebugOutputArray[DebugOutput.NumLines] = DebugOutputPadLineNum(str(DebugOutput.CntLines), " ", 4) + ": " + a_strText
endfunction
function DebugOutputShow()
if (DebugOutput.NumLines > 0)
for i = 1 to DebugOutput.NumLines
print (DebugOutputArray[i])
next
endif
endfunction
function DebugOutputScroll()
v_iIndex as integer
if (DebugOutput.MaxLines > 1)
for v_iIndex=1 to DebugOutput.MaxLines-1
DebugOutputArray[v_iIndex] = DebugOutputArray[v_iIndex + 1]
next
endif
endfunction
function DebugOutputPadLineNum(a_StringToPad as string, a_strPadChar as string, a_iPadLen as integer)
while ( Len(a_StringToPad) < a_iPadLen)
a_StringToPad = a_StringToPad + a_strPadChar
endwhile
endfunction a_StringToPad
function DebugOutputClear()
// Reset line counter
DebugOutput.NumLines = 0
endfunction
Here's a screenshot of the result:
This is another case demonstrating how flaky the UDT implementation in AppGameKit TIER1 Basic is.
I'd love to see this UDT issue fixed ASAP. I've encountered the same problem myself many times, which has caused me to avoid passing and returning UDT's to/from functions completely.
Another finding I happened to make, is that UDT's, like arrays, are given global scope, even when declared without the "global" keyword.
Here are some related reports from the AppGameKit issues list,
with my comments below:
http://code.google.com/p/agk/issues/detail?id=146
* Although stated as fixed in 107 series, definately NOT fixed in 1076.
http://code.google.com/p/agk/issues/detail?id=270
* Stated as fixed, yet appears NOT to be fixed.
* Perhaps someone should check these in the 108 beta series, and see if they ARE actually fixed or not. (I myself am still holding back on upgrading until a stable 108 release.)
Cheers,
AgentSam - the grumpy old software engineer who's very disappointed in AGK