Hi all,
Not sure if I should ask these questions in Newbie questions forum or here. Since I have studied very hard to figure out my confusions, I decide to ask here. For your convenience, I will describe what I learned so far, and then ask my questions after.
What I have known:
When I google articles and study around youtube tutorials to understand the meanings of self, methods, and the differences between dot/colon operators, I noticed they often mentioned OOP(Object-oriented programming). Lua is my first program-like language I have ever learned, so it’s hard for me to understand the meaning of OOP. According to what I read/watch so far, I “feel” OOP is something very like copy/instance/reference of an object in 3ds max (https://youtu.be/2E5DDduuVb4, sorry this video is in Chinese, but I cannot find any shorter and clear one in English.) Basically it’s saying, a copy of an object, any further modification is independent from the original one. The instance of an object will be treated as the same. No matter applying the further modification on either the instance or the original, both will be changed. The reference of an object has the parent-child relationship, the original object is the father and the reference is the child. The further modification on child won’t effect the father. The further modification of father will effect the child.
Table is an object in Lua. "This allows a table, that has been passed into a function, to be changed directly unlike other types that are always passed as a copy of the original. The table type is the only type that passes by reference. " (–> This is from the tutorial https://github.com/GuildSA/corona-hack-pack/blob/master/LuaSamples/lua_6_reference_vs_copy.lua ) This tutorial make sense to me. I can imagine a table goes into a function “factory” as a reference, be modified, and then came out with different content/data inside it.
The method is a function inside the table.
The other studies related to my questions are these:
https://stackoverflow.com/questions/4911186/difference-between-and-in-lua
Lua: Self Tutorial by John Lindquist https://youtu.be/F-xJq6s6lK0
I am going to ask my confusion with John Lindquist’s youtube code.
local dog ={} function dog:speak() print("bark") end function dog:reactToDoorbell() self:speak() end dog:reactToDoorbell() --[[for k,e in pairs(dog) do print ( "table["..k.."]" .." is ".. e) end ]] print(dog.speak) print(dog.reactToDoorbell) print(dog[1]) print(dog[2])
Somehow I cannot make the code line number to show up, so please let me ask questions in screenshots below.
My questions are the circle numbers in screenshots 1, 2, 3:
Q1. Does this mean we add two functions in the table dog? One is speak(), another is reactToDoorbell() ?
Q2. However, why can’t I print out the values with pairs to prove they are added?
Q3. The error message in the output panel, does it mean the functions are added? Why I cannot print them out?
Q4. Later I found out I can print them out individually. Therefore, is it a correct idea to consider that, 2 functions are added by line 3~9 ?
Also, we can say, these 2 functions are methods?
Q5: I am trying to keep the same concept and re-writing code into this way. Why doesn’t it work?
local dogSpeak = function() print("bark in function") end local reactToDoorbell = function() self:speak() end -- dog = {a="inside dog table", speak = dogSpeak, reactToDoorbell = function() self:speak() end} dog = {a="inside dog table", speak = dogSpeak, run = reactToDoorbell} print(dog.speak()) --\> bark in function print(dog.speak) --\> something like function: 0000000000528DA0 print(dog.run) --\> something like function: 0000000000528E30 print(dog.run()) print(dog:run())
Thank you in advance to check out my questions. I am looking forward to your thoughts.