Jump to content

[HELP] Self.


iwalidza

Recommended Posts

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!

  • Like 2
Link to comment
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

Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...