Heres what I am trying to do. What I want is entity A to spawn multiple Entity Bs (Durgz_weed) that are tracable back to what spawned it (which is entity A) so in the method where the entity is spawned (ENT:Use) i make asign an attribute to ENT.Test called parent and I set it to a test string (“hello”). I then proceed to try and retrive the attribute in my StartTouch method but when I do I just get an error:
[ERROR] init.lua:52: bad key to string index (number expected, got string)
ENT.test = ""
self.test = ents.Create("durgz_weed")
self.test:SetPos(self:GetPos() + Vector(0,0,109))
self.test.parent = "hello"
function ENT:StartTouch( hitEnt ) -- This is line 52
It’d help if you structured your code properly and at least pointed out what line 52 is.
[editline]30th November 2013[/editline]
Anyway, it’s because you’ve assigned the “test” field to be an empty string at the top and then you’re trying to print the “parent” index of a string and that doesn’t make any sense. It simply hasn’t created the entity “durgz_weed” before you get that error.
Terribly sorry. Derp on my part with the formatting.
Whats best to do?
I have tried:
ENT.test = ents.Create(“durgz_weed”)
Which works but when I press E it olny works once then throws this error
[ERROR] /init.lua:44: Tried to use a NULL entity!
1. SetPos - [C]:-1
2. unknown - /init.lua:44
Line 44 is:
self.entT:SetPos(self:GetPos() + Vector(0,0,39))
“durgz_weed” probably isn’t a valid entity.
But it is because it spawns once and then not again any time past the first time. Sorry for the ignorance.
Can you show the whole code?
entT is the test variable by the way.
This piece of code:
ENT.entT = ents.Create("durgz_weed")
should not be where it is. You’re trying to create an entity before it’s probably even defined.
And it’s worse in your Use function, you’re “spawning” the same entity multiple times.
Just create the entity in your Use function.
DId what you just said and it works perfectly!
One issue though, I get the same error now I have added:
function ENT:StartTouch( hitEnt )
You might want to check if it’s a valid entity that it hit before removing it.
I just thought though, how will I access my parent attribute if I dont declare it outside methods?
Sorry to be a pain
Create the entity and then assign the value?