iwalidza Posted March 17, 2020 Posted March 17, 2020 Sometimes I see people making a script and using something like .self What is this thing I want to explain to him and why it is used and its benefits
Moderators Patrick Posted March 17, 2020 Moderators Posted March 17, 2020 I think you talking about metatables. https://wiki.multitheftauto.com/wiki/OOP_in_Lua 2
iwalidza Posted March 17, 2020 Author Posted March 17, 2020 3 minutes ago, Patrick said: I think you talking about metatables. https://wiki.multitheftauto.com/wiki/OOP_in_Lua Yup this Meta tables
The_GTA Posted March 17, 2020 Posted March 17, 2020 1 hour ago, iwalidza said: Yup this Meta tables You don't need metatables for the self keyword. Take a look at this small sample: local police = {} -- Take a look: we use the "self" keyword here! function police:setName(name) self.name = name end function police:getName() return self.name end police:setName("Las Venturas Police Department") outputDebugString("Name of Police HQ: " .. police:getName()) Formal description: if you specify a "method" of a table in Lua, then inside the function body you can access the hidden local variable "self" which points to the table itself. This self variable is the first argument to the function specification of the method. Thus an equivalent way of writing above script is: local police = {} -- Here we explicitly specify the self. function police.setName(self, name) self.name = name end function police.getName(self) return self.name end police:setName("Las Venturas Police Department") outputDebugString("Name of Police HQ: " .. police:getName()) Hope this helps! 2
iwalidza Posted March 17, 2020 Author Posted March 17, 2020 3 hours ago, The_GTA said: You don't need metatables for the self keyword. Take a look at this small sample: local police = {} -- Take a look: we use the "self" keyword here! function police:setName(name) self.name = name end function police:getName() return self.name end police:setName("Las Venturas Police Department") outputDebugString("Name of Police HQ: " .. police:getName()) Formal description: if you specify a "method" of a table in Lua, then inside the function body you can access the hidden local variable "self" which points to the table itself. This self variable is the first argument to the function specification of the method. Thus an equivalent way of writing above script is: local police = {} -- Here we explicitly specify the self. function police.setName(self, name) self.name = name end function police.getName(self) return self.name end police:setName("Las Venturas Police Department") outputDebugString("Name of Police HQ: " .. police:getName()) Hope this helps! thank you so much for this tutorial
The_GTA Posted March 17, 2020 Posted March 17, 2020 1 minute ago, iwalidza said: thank you so much for this tutorial No problem. I am happy to hear about your thankfulness 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now