garrys mod - Lua attempt to index global 'self' error (GMod Lua script) -



garrys mod - Lua attempt to index global 'self' error (GMod Lua script) -

i have been getting next error section of code:

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

the function starts @ line 57, , when remove line 58 (local pos = self.entity:getpos() , gives same error message line 61.

function cooked() local pos = self.entity:getpos() local roll = math.random(1, 5); if roll == 5 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 nutrient , bundle product!\", notify_generic, 4)") end end

it's unclear self should be. error says it's global, consistent code have shown.

but, self exclusively used formal parameter function , implicit 1 @ that.

when self implicit, function called method because intent access fields in table passed self. method value held field in same table (or @ least, available such via metamethods).

the colon syntax in function definition creates method.

if cooked method create sense access self. cooked global.

you might have meant:

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

how read lua statement above:

access sometable table assign "cooked" field function value created function definition.

(the function definition compiled method syntax, in body, self first formal parameter, implicit.)

the method called like:

sometable:cooked() -- passes sometable self

the colon syntax in field function phone call method call.

how read lua statement above:

access sometable table, index "cooked" field, call value function, passing sometable first parameter, discard result list.

oddities:

the syntax methods "syntactic sugar." methods values no different other function values: a function created function definition using syntax can called using function phone call syntax. a method need not called using method phone call syntax. a non-method can called using method phone call syntax. self not reserved can used identifier is.

lua garrys-mod

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -