Page 1 of 1

Question about Objects and Method´s

Posted: Wed Mar 29, 2017 1:43 pm
by NoAim91
This works fine:

Code: Select all

if self.work == 3 then
  for i, v in pairs(listOfBuildings) do
    if self.carry == 6 then 
      if listOfBuildings[i].id == 17 and listOfBuildings[i].mortarl > 0 then
        self. = listOfBuildings[i].x  self.target2Y = listOfBuildings[i].y  self.target2 = listOfBuildings[i]
      end
    end
  end
end
Now I like to create a function for the middel part of my if statement.

Code: Select all

if self.work == 3 then
  if self.carry == 6 then lookingForMortar() end
end

Code: Select all

function lookingForMortar()

for i, v in pairs(listOfBuildings) do
  if listOfBuildings[i].id == 17 and listOfBuildings[i].mortar > 0 then
    self.target2X = listOfBuildings[i].x  self.target2Y = listOfBuildings[i].y  self.target2 = listOfBuildings[i]
  end
end
  
return self.target2X, self.target2Y, self.target2
end
and this dosn´t work because of the "self." ... it is a function and I need a method but honestly, i don´t get the difference between method and function. :-/



The next thing is that I not only have to look for mortar, but also for wood and stone. So I have to copy and paste most of the lookingForMortar function, only changing the index .. right? But is there not a better way???

Re: Question about Objects and Method´s

Posted: Wed Mar 29, 2017 2:31 pm
by NoAim91
Argh, I´m so sorry, I got it by myself.

When i call the function i have to give the selfness -> lookingForMortar(self)
And the function need to know that it receives the selfness -> function lookingForMortar(self)

that´s it .. a little thing i forgot :-/

Re: Question about Objects and Method´s

Posted: Wed Mar 29, 2017 2:42 pm
by Lucyy
The difference between a method and a function is that a method includes a reference to self and you'd have to pass it for function

E.g:
Let's pretend we have a table for an enemy:

Code: Select all

local enemy = {}
In this table we have a variable called "health" of value 100

Code: Select all

enemy.health = 100
If you want to use health in a function of enemy, you'd do:

Code: Select all

function enemy:setHealth(newHealth)
    self.health = newHealth
end

MyEnemy:setHealth(200)
This is a method
If you were to write this as a function you'd write it as such

Code: Select all

enemy.setHealth = setHealth(self, newHealth)
    self.health = newHealth
end

MyEnemy.setHealth(MyEnemy, 200)
This way you'd have to pass a reference to the enemy object to the function, if you don't it'll throw an error

TL;DR
Method already contains a reference to self and a function does not, a method is written like this:

Code: Select all

function enemy:setHealth(newHealth)
And a function is written like:

Code: Select all

enemy.setHealth = function(self, newHealth)

Re: Question about Objects and Method´s

Posted: Wed Mar 29, 2017 4:12 pm
by zorg
Luccy is technically right, but even then, in lua, there's no real major implementation difference between plain functions and "methods"; the latter is just syntax sugar (self isn't some special construct either, just a hidden parameter for functions defined with a colon (:) instead of a dot (.); This was explained quite a few times before, though i can never find those posts either :P

So...

Code: Select all

T = {}

-- Function / Method declaration + definition

function T.foo(x,y,z) -- Syntax sugar for T.foo = function(x,y,z)
    return self,x,y,z
end

function T:bar(x,y,z) -- Syntax sugar for function T.bar(self, x, y, z) which is syntax sugar for T.bar ( function(self, x, y, z)
    return self,x,y,z
end

T.baz = function(whatever,x,y,z) -- This is the generic form.
    return whatever,x,y,z
end

--------------------------------------------------------

local a,b,c,d,e

-- Function / Method call
a,b,c,d,e = T.foo(1,2,3,4,5)
print(tostring(a),tostring(b),tostring(c),tostring(d),tostring(e))

a,b,c,d,e = T:foo(1,2,3,4,5)
print(tostring(a),tostring(b),tostring(c),tostring(d),tostring(e))

a,b,c,d,e = T.bar(1,2,3,4,5)
print(tostring(a),tostring(b),tostring(c),tostring(d),tostring(e))

a,b,c,d,e = T:bar(1,2,3,4,5)
print(tostring(a),tostring(b),tostring(c),tostring(d),tostring(e))

a,b,c,d,e = T.baz(1,2,3,4,5)
print(tostring(a),tostring(b),tostring(c),tostring(d),tostring(e))

a,b,c,d,e = T:baz(1,2,3,4,5)
print(tostring(a),tostring(b),tostring(c),tostring(d),tostring(e))