1

I have been getting the following error with this section of code:

[ERROR] lua/entities/cook/init.lua:58: attempt to index global 'self' (a nil value)1. cooked - lua/entities/cook/init.lua:58

The function starts at line 57, and when I remove line 58 (local Pos = self.Entity:GetPos() , it just gives the same error message for line 61.

function cooked()
    local Pos = self.Entity:GetPos()
    local roll = math.random(1, 5);
        if roll == 5 then
        self.Entity:EmitSound("phx/explode06.wav")
        self.Entity:Remove()
        else
        local createfood = ents.Create("food")
        createfood:SetPos(Pos + Vector(0,10,100)) 
        createfood:Spawn()
        self:SendLua("GAMEMODE:AddNotify(\"You finish cooking the food and package the product!\", NOTIFY_GENERIC, 4)")
        end
end
jmlemetayer
  • 4,774
  • 1
  • 32
  • 46
user3751712
  • 13
  • 1
  • 3
  • why exactly did you accept Maze's answer but not Tom's, which is not only correct but also explaining why? – nonchip Jun 27 '14 at 15:09

2 Answers2

1

Self is nil, so how do you call cooked()? It has to be self.cooked(self) or self:cooked(), where self is the table you want to use as self in your function.

Maze
  • 156
  • 10
  • actually, the function must also be defined as `function sometable:cooked()` or `function sometable.cooked(self)` – nonchip Jun 27 '14 at 15:08
1

It's unclear what self should be. The error says it's a global, which is consistent with the code you have shown.

But, self is almost exclusively used as a formal parameter to a function and an implicit one at that.

When self is implicit, the function is called a method because the intent is for it to access fields in a table passed into self. The method value is almost always held by a field in the same table (or at least, available as such via metamethods).

The colon syntax in a function definition creates a method.

If cooked was a method then it would make sense for it to access self. But cooked is a global.

You might have meant:

function sometable:cooked()
-- ...
-- self is an implicit formal parameter
-- ...
end

How to read the Lua statement above:

  1. access sometable as a table
  2. assign its "cooked" field the function value created by the function definition.

(The function definition is compiled from the method syntax, so in the body, self is the first formal parameter, which is implicit.)

The method could be called like:

sometable:cooked() -- passes sometable as self

The colon syntax in a field function call is a method call.

How to read the Lua statement above:

  1. access sometable as a table,
  2. index its "cooked" field,
  3. call its value as a function, passing sometable as the first parameter,
  4. discard the result list.

Oddities:

  • The syntax for methods is just "syntactic sugar." Methods values are no different than other function values:
    • A function created from a function definition using the any syntax can be called using any function call syntax.
      • A method need not be called using the method call syntax.
      • A non-method can be called using the method call syntax.
  • self is not reserved so it can be used as any identifier is.
Tom Blodget
  • 20,260
  • 3
  • 39
  • 72