AGK2 is shaping up well. One question: How can I return a reference to an object from within a function? Here's some code from my program:
...
UpdateBadge(GetBadgeByLabel(Player1, "badge 1"), 50)
...
function GetBadgeByLabel(player ref as TPlayer, lbl as string)
badge as TBadge // ***
for i = 0 to player.badges.length
if lower(player.badges[i].label) = lower(lbl)
badge = player.badges[i] // ***
exit
endif
next
endfunction badge // ***
function UpdateBadge(badge ref as TBadge, amount as integer)
badge.progress = badge.progress + amount
if badge.progress > 100 then badge.progress = 100
endfunction
I'd like my GetBadgeByLabel function to return a reference to an existing TBadge object. The function compiles as is, but doesn't seem to return a reference. My faulty logic, silly mistake, or not possible?
Runnable example:
SetVirtualResolution(1024, 768)
type TBadge
label as string
progress as integer //0 to 100 percent.
score as integer
endtype
type TPlayer
name as string
badges as TBadge[]
endtype
global Player1 as TPlayer
Player1 = CreatePlayer("Player 1")
UpdateBadge(GetBadgeByLabel(Player1, "badge 1"), 50)
//--------------------
do
PrintPlayer(Player1)
Print(ScreenFPS())
Sync()
loop
end
//-------------------
function CreatePlayer(nam as string)
player as TPlayer
player.name = nam
LoadBadges(player.badges)
endfunction player
Function PrintPlayer(player ref as TPlayer)
Print(player.name)
Print("------------------")
for i = 0 to player.badges.length
Print(BadgeToString(player.badges[i]))
next
Print("")
endfunction
// ##########
function CreateBadge(lbl as string, sc as integer)
badge as TBadge
badge.label = lbl
badge.score = sc
endfunction badge
Function LoadBadges(badges ref as TBadge[])
badges.insert(CreateBadge("Badge 1", 10))
badges.insert(CreateBadge("Badge 2", 15))
badges.insert(CreateBadge("Badge 3", 8))
badges.insert(CreateBadge("Badge 4", 10))
badges.insert(CreateBadge("Badge 5", 12))
endfunction
function GetBadgeByLabel(player ref as TPlayer, lbl as string)
badge as TBadge // ***
for i = 0 to player.badges.length
if lower(player.badges[i].label) = lower(lbl)
badge = player.badges[i] // ***
exit
endif
next
endfunction badge // ***
function UpdateBadge(badge ref as TBadge, amount as integer)
badge.progress = badge.progress + amount
if badge.progress > 100 then badge.progress = 100
endfunction
function BadgeToString(badge ref as TBadge)
s as string
s = badge.label + ": " + str(badge.progress) + " (" + str(badge.score) + ")"
endfunction s