Page 1 of 1

class system lua general understanding [Solved]

Posted: Tue Mar 27, 2018 6:53 am
by Anakin
Hey there,

i'm really confused by the class system from lua. Maybe someone can explain it to me

this is the original function
Hidden/Spoiler:
[code]
function ObjectiveConquest:AddCommandPost(cp)
--make sure we have a table to add the cp to
self.commandPosts = self.commandPosts or {}

--do all the error checking we can on the cp
assert(cp.name, "WARNING: no name supplied for the command post")
cp.name = string.lower(cp.name)

self.commandPosts[cp.name] = cp

--keep a running tally of the bleedValue relative to each team
if not self.totalBleedValue then
self.totalBleedValue = {}
self.totalBleedValue[self.teamATT] = 0
self.totalBleedValue[self.teamDEF] = 0
end
self.totalBleedValue[self.teamATT] = self.totalBleedValue[self.teamATT] + GetCommandPostBleedValue(cp.name, self.teamATT)
self.totalBleedValue[self.teamDEF] = self.totalBleedValue[self.teamDEF] + GetCommandPostBleedValue(cp.name, self.teamDEF)
end
[/code]
that i try to wrap this way:
Hidden/Spoiler:
[code]
if ObjectiveConquest then

aih_backup = ObjectiveConquest.AddCommandPost

ObjectiveConquest.AddCommandPost = function(cp, ...)

print(">>", cp.name)

uf_print(cp, false, 0)
return aih_backup(cp, unpack(arg))
end
else
print(">> go and find a solution")
end
[/code]
output log:
Hidden/Spoiler:
[code]
>> nil
0: uf_print(): Starting: table: 085B5ACC function: 085D2904 false 0
0: Key, Value: multiplayerRules true
0: Key, Value: textDEF game.modes.con2
0: Key, Value: teamDEF 2
0: Key, Value: textATT game.modes.con
0: Key, Value: teamATT 1
0: uf_print(): Finished: table: 085B5ACC false 0
>> nil
0: uf_print(): Starting: table: 085B5ACC function: 085D2904 false 0
0: Key, Value: totalBleedValue table: 085B5BCC
0: Key, Value: commandPosts table: 085B5BAC
0: Key, Value: multiplayerRules true
0: Key, Value: textDEF game.modes.con2
0: Key, Value: teamDEF 2
0: Key, Value: textATT game.modes.con
0: Key, Value: teamATT 1
0: uf_print(): Finished: table: 085B5ACC false 0
>> nil
0: uf_print(): Starting: table: 085B5ACC function: 085D2904 false 0
0: Key, Value: totalBleedValue table: 085B5BCC
0: Key, Value: commandPosts table: 085B5BAC
0: Key, Value: multiplayerRules true
0: Key, Value: textDEF game.modes.con2
0: Key, Value: teamDEF 2
0: Key, Value: textATT game.modes.con
0: Key, Value: teamATT 1
0: uf_print(): Finished: table: 085B5ACC false 0
>> nil
0: uf_print(): Starting: table: 085B5ACC function: 085D2904 false 0
0: Key, Value: totalBleedValue table: 085B5BCC
0: Key, Value: commandPosts table: 085B5BAC
0: Key, Value: multiplayerRules true
0: Key, Value: textDEF game.modes.con2
0: Key, Value: teamDEF 2
0: Key, Value: textATT game.modes.con
0: Key, Value: teamATT 1
0: uf_print(): Finished: table: 085B5ACC false 0
>> nil
0: uf_print(): Starting: table: 085B5ACC function: 085D2904 false 0
0: Key, Value: totalBleedValue table: 085B5BCC
0: Key, Value: commandPosts table: 085B5BAC
0: Key, Value: multiplayerRules true
0: Key, Value: textDEF game.modes.con2
0: Key, Value: teamDEF 2
0: Key, Value: textATT game.modes.con
0: Key, Value: teamATT 1
0: uf_print(): Finished: table: 085B5ACC false 0
>> nil
0: uf_print(): Starting: table: 085B5ACC function: 085D2904 false 0
0: Key, Value: totalBleedValue table: 085B5BCC
0: Key, Value: commandPosts table: 085B5BAC
0: Key, Value: multiplayerRules true
0: Key, Value: textDEF game.modes.con2
0: Key, Value: teamDEF 2
0: Key, Value: textATT game.modes.con
0: Key, Value: teamATT 1
0: uf_print(): Finished: table: 085B5ACC false 0
[/code]
why does the cp look like that?? In my understanding it should like similar with different name value. But there is no name value.

Re: class system lua general understanding

Posted: Tue Mar 27, 2018 7:21 am
by SleepKiller
Removing some of the syntactic sugar from this should make it easier to understand what is going on here.

Code: Select all

function ObjectiveConquest:AddCommandPost(cp)
end
The above snippet is expanded by the compiler to.

Code: Select all

ObjectiveConquest.AddCommandPost = function (self, cp)
end
We can now see that the presence of the ':' causes the compiler to add the implicit self parameter as the first argument. Now with that knowledge when we look at the closure you're using to hook the function the issue is quite easy to spot.

Code: Select all

ObjectiveConquest.AddCommandPost = function(cp, ...)
end
You're simply treating 'self' as 'cp' and the real 'cp' argument is lost in the variadic pack. Defining your closure accounting for 'self' should fix your issue.

Code: Select all

ObjectiveConquest.AddCommandPost = function(self, cp, ...)
end
The free first edition of Programming in Lua has some excellent info on classes in Lua, it's well worth a read if you have the interest and time. https://www.lua.org/pil/16.html

Re: class system lua general understanding

Posted: Tue Mar 27, 2018 7:57 am
by Anakin
works fine, thank you :D