Hello! I'm having some issues to finish understanding how entity's relate to each other with globals and local values. I started learning programing with wiremod e2 and i'm used to see that each chip wont relate with other chips global variables. But now i have a issue here on lua entities.
I'm trying to use local variables so different engines wont relate to each other, and they will work individualy. The thing is that they are sharing certain things in most cases which should be individual. A example of this is engineStress variable which if i set it has local, it wont work. if i dont, other engines will share the same stress and break when they shouldnt.
Here it's part of the init.lua file, i only placed the most relevant values so you dont have to figure out everything. maxAllowedSpeed and maxAllowedAngSpeed Variables are just numbers which i set on other part of the code that are not shown here.
Also the local engineStress variable is set has comment since otherwise it wont work. i'll get this error: attempt to perform arithmetic on upvalue 'engineStress' (a nil value)
[code]
// local engineStress
function ENT:Initialize()
engineStress = 0
function ENT:Think()
if IsValid(self) then
local entSpeed = self:GetVelocity():Length() / 22.65 // conversion from lua to mph
local entAngSpeed = self:GetPhysicsObject():GetAngleVelocity():Length()
if maxAllowedSpeed != nil then
if entSpeed > maxAllowedSpeed then
engineStress = engineStress + 20
end
end
if maxAllowedAngSpeed != nil then
if entAngSpeed > maxAllowedAngSpeed then
print("AngSpeeding too much bro")
print(entAngSpeed)
engineStress = engineStress + 20
end
end
if engineStress != nil then
if engineStress > 0 then
engineStress = engineStress - 5
print(engineStress)
// add some sound effect of a engine going high on rpm
if engineStress > 100 then
for k, v in pairs(constraint.GetAllConstrainedEntities(self)) do
constraint.RemoveAll(k, "Weld")
// add some nice effects :)
end
end
elseif engineStress < 0 then
engineStress = 0
end
end
end
end
[/code]
I hope i explained myself and thanks for the help in advance :)
For a variable to be local to an entity, it has to be stored in a way that relates the entity to that data. The most common way of doing this is
[code]
function ENT:Initialize()
self.engineStress = 0
end
[/code]
entities are userdata, like tables you can store variables on them, doing so creates a unique variable for each entity.
Also, the reason you're getting that error is because `engineStress` is nil, and you can't add to nil values. I'm guessing that you only added "engineStress = 0" in initialize after the fact.
You can also use this hook [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/ENTITY/SetupDataTables]ENT:SetupDataTables[/url]
You didn't end your init function.
[QUOTE=bigdogmat;51614947]For a variable to be local to an entity, it has to be stored in a way that relates the entity to that data. The most common way of doing this is
[code]
function ENT:Initialize()
self.engineStress = 0
end
[/code]
entities are userdata, like tables you can store variables on them, doing so creates a unique variable for each entity.
Also, the reason you're getting that error is because `engineStress` is nil, and you can't add to nil values. I'm guessing that you only added "engineStress = 0" in initialize after the fact.[/QUOTE]
Ahh, that's what those . are for ... i never got to fully understand those, but i was suspecting they worked in similar way. Thanks a lot!!!
[QUOTE=FlyPiggyBanks;51616089]You didn't end your init function.[/QUOTE]
That's because i kinda deleted most of the code so you have the readable parts and the ones that are involved in my actual problem..
Sorry, you need to Log In to post a reply to this thread.