self.Entity:GetPos() brings up nil


lua\entities\drug\cl_init.lua:9: attempt to index global 'self' (a nil value)

What i’m trying to do is have a particle emitter shoot up particles from my entity however, I am not sure how to get its position.

[lua]include(‘shared.lua’)

function ENT:draw()

self.DrawEntityOutline( 1.0 )
self.Entity:DrawModel()

end

local center = self.Entity:GetPos()
local em = ParticleEmitter(center)

if Entity:IsValid( self ) then
local part = em:Add(“sprites/light_glow02_add”, center)
if part then
part:SetColor(0,255,0,math.random(255))
part:SetVelocity(Vector(math.random(-1,1),math.random(-1,1),math.random(-1,1)):GetNormal() * 20)
part:SetDieTime(0.5)
part:SetLifeTime(0)
part:SetStartSize(10)
part:SetEndSize(0)
end
end

em:Finish()
[/lua]

Also i know the particle part probably isn’t right yet but I would like to attempt that on my own.

You can’t use self outside of a method. You’ll have to enclose that code in one.

What do you mean method? Like a function?

Yeah, one of the ENT: functions.

So if I use one of the shared ENT: functions to define the local variable, am I able to still use that variable in the cl_init file?

Put
[lua]
local center
[/lua]
Somewhere at the top of cl_init, then put
[lua]
center = self:GetPos()
[/lua]
In and ENT: function (ENT:Initialize()?)

Then you can use it in the other functions in that file.

I am thoroughly confused by your post mind if I pm you for help?

Wait, disregard that post I misread.
Your problem is you are trying to create particles outside the functions. You need to pick a suitable function depending on when you want the particles to appear and place the particle code in there.

So the Think function would be fine for this?

Only if you want your code to be executed every think, which is often.

Use **[ENT.Initialize

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=ENT.Initialize)** to execute code once when the entity is being created.

See also :

Basically I want one particle every think which isn’t that much considering they disappear quite quickly, i’m confused as to where to define variables. If i’m not mistaken I should hook a function that I make to the think function correct?

My problem is getting the vector because the particle emitter is client side and when i use self.ENT:GetPos() self is nil. How would I go about getting the vector to use in the particle emitter?

Automerge…?

self only exists inside ENT: functions. It’s an invisible parameter created when using : instead of . so
[lua]
function ENT:Initialize()
[/lua]
is just a shorter way of writing
[lua]
function ENT.Initialize(self)
[/lua]
Hope you understand better now.

Okay, so how would i go about obtaining coordinates for my emitter?

You retrieve them in the function where you need them using self:GetPos()

Okay, I’m going to try and help explain this further.

For a SENT, you can only use “self” inside a function that starts with “ENT:”

You have to make your particle inside one such function.

Why does this bring an error then?


include('shared.lua')

function ENT:draw()

	self.DrawEntityOutline( 1.0 )
	self.Entity:DrawModel()
	
end

function ENT:Emit()
	

	local Position = self.GetPos()
	local em = ParticleEmitter(Position)
	
	if math.random(1,5) == 3 then


     local part = em:Add("sprites/light_glow02_add", Position)
     if part then
          part:SetColor(0,255,0,math.random(255))
          part:SetVelocity(Vector(math.random(-1,1),math.random(-1,1),math.random(-1,1)):GetNormal() * 20)
          part:SetDieTime(0.5)
          part:SetLifeTime(0)
          part:SetStartSize(10)
          part:SetEndSize(0)
     
end
em:Finish()
end
end
hook.Add( "Think", "Particle", ENT:Emit() )

self:GetPos() not self.GetPos()

It is still bringing up an error.

lua\entities\drug\cl_init.lua:12: attempt to call method ‘GetPos’ (a nil value)

Becuse your calling on the entities table instead of the entity change Emit to Think and renive the hook.Add