Timer/whatever problem - Destiny's Player - 01-10-2015
I can't figure out, why won't the part under an "if all of these are equal to 1 then do" thingie work, even though I've checked, the entities are gotten. Are variables that are stuck under a timer always local to it? Or something else?
function chatMenu(msg,ply)
local Message = msg:lower():explode(" ")
if Message[1] == "!page" or Message[1] == "/page" or Message[1] == "page" then
Page = tonumber(Message[2])
end
end
function tactical(msg,ply)
local Message = msg:lower():explode(" ")
if Message[1] == "!shield" or Message[1] == "/shield" or Message[1] == "shield" then
if Alert < 1 then
print("Alert level is too low")
return
end
if Message[2] == "on" then
Shield["Activate"] = 1
--print("Shield turned on")
elseif Message[2] == "off" then
Shield["Activate"] = 0
--print("Shield turned off")
elseif Message[2] == "balance" then
Shield["Divert Power"] = -1
ShDivert = -1
--print("Shields prioritized to all sides")
elseif Message[2] == "separate" then
Shield["Divert Power"] = 0
ShDivert = 0
--print("Shields prioritized to no sides")
elseif Message[2] == "front" then
Shield["Divert Power"] = 1
ShDivert = 3
--print("Shields prioritized to the front")
elseif Message[2] == "back" then
Shield["Divert Power"] = 2
ShDivert = 4
--print("Shields prioritized to the back")
elseif Message[2] == "left" then
Shield["Divert Power"] = 3
ShDivert = 1
--print("Shields prioritized to the left")
elseif Message[2] == "right" then
Shield["Divert Power"] = 4
ShDivert = 2
--print("Shields prioritized to the right")
elseif Message[2] == "up" then
Shield["Divert Power"] = 5
ShDivert = 5
--print("Shields prioritized to the top")
elseif Message[2] == "down" then
Shield["Divert Power"] = 6
ShDivert = 6
--print("Shields prioritized to the bottom")
end
elseif Message[1] == "!target" or Message[1] == "/target" or Message[1] == "target" then
if Alert < 1 then
print("Alert level is too low")
return
end
local Tgt = Message[2]
for i=1,#AllCoresOwn do
local Found = string.find(AllCoresOwn[i]:name():lower(),Tgt)
if Found ~= nil then
Sensor["Target"] = AllCores[i]
TgtName = AllCoresOwn[i]:name()
return
end
end
print("No cores belonging to "..Tgt.." found.")
end
if Message[1] == "!alert" or Message[1] == "/alert" or Message[1] == "alert" then
if Message[2] == "green" then
Alert = 0
Shield["Activate"] = 0
Sensor["Target"] = nil
print("Alert is set to "..Message[2].."\nShields and weapons are blocked")
elseif Message[2] == "yellow" then
Alert = 1
print("Alert is set to "..Message[2].."\nShields are unlocked, weapons are blocked")
elseif Message[2] == "red" then
Alert = 2
print("Alert is set to "..Message[2].."\nShields and weapons are unlocked")
end
end
end
function getResources()
Constr = Self:getConstraints()
i = 0
timer.create("Res",0.01,#Constr,function()
i=i+1
Cur = Constr[i]
printConsole(Cur:class())
if Cur:class() == "ship_core" then
SCore = Cur
Co1 = 1
printColor(Color(255,255,255),"Ship Core Found")
printConsole(SCore)
elseif Cur:class() == "stargazer_node" then
Node = Cur
No1 = 1
printColor(Color(255,150,150),"Resource Node Found")
printConsole(Node)
elseif Cur:class() == "st_shield_emitter" then
Shield = Cur
Sh1 = 1
printColor(Color(100,200,250),"Shield Emitter Found")
printConsole(Shield)
elseif Cur:class() == "sensor_array" then
Sensor = Cur
Se1 = 1
printColor(Color(100,250,100),"Sensor Array Found")
printConsole(Sensor)
end
end)
AllCores = find.byClass("ship_core")
for i=1,#AllCores do
printConsole(AllCores[i])
AllCoresOwn[i] = AllCores[i]:owner()
printConsole(AllCoresOwn[i]:name():lower())
end
end
Page = 2
SCore = nil
Co1 = 0
Node = nil
No1 = 0
Shield = nil
Sh1 = 0
Sensor = nil
Se1 = 0
Alert = 0
AllCores = {}
AllCoresOwn = {}
ShDivert = 0
Plating = 0
TgtHealthMax = 0
TgtHealth = 0
TgtArmor = 0
TgtShieldOn = 0
TgtShieldMax = 0
TgtShield = 0
TgtName = ""
getResources()
if Co1 == 1 and No1 == 1 and Sh1 == 1 and Se1 == 1 then
printColor(Color(255,200,0),"Resources Assigned")
Sensor["Target"] = nil
chat.listen(chatMenu,Owner)
chat.listen(tactical,Owner)
timer.create("Refresh",1,0,function()
HPFull = SCore:getWirelink()["Max Health"]
HP = SCore:getWirelink()["Health"]
Plating = SCore:getWirelink()["Plating"]
ArmourMax = SCore:getWirelink()["Armour"]
Armour = SCore:getWirelink()["Armour Deployed"]
EnergyFull = Node:getWirelink()["Max Energy"]
Energy = Node:getWirelink()["Energy"]
ShChargeFull = Shield:getWirelink()["Max Charge"]
ShCharge = Shield:getWirelink()["Total Charge"]
ShLeft = Shield:getWirelink()["Left"]
ShRight = Shield:getWirelink()["Right"]
ShFront = Shield:getWirelink()["Front"]
ShBack = Shield:getWirelink()["Back"]
ShUp = Shield:getWirelink()["Up"]
ShDown = Shield:getWirelink()["Down"]
TgtHealthMax = Sensor:getWirelink()["Max Health"]
TgtHealth = Sensor:getWirelink()["Health"]
TgtArmor = Sensor:getWirelink()["Plating"]
TgtShieldOn = Sensor:getWirelink()["Shield Active"]
TgtShieldMax = Sensor:getWirelink()["Max Shields"]
TgtShield = Sensor:getWirelink()["Shield Emitter"]
for i=1,#AllCores do
AllCores = find.byClass("ship_core")
if AllCores[i] ~= nil and AllCores[i]:owner() ~= nil then
AllCoresOwn[i] = AllCores[i]:owner()
end
--printConsole(AllCoresOwn[i]:name():lower())
end
end)
end
RE: Timer/whatever problem - Person8880 - 01-10-2015
Timers are asynchronous. You're running your check before the timer has even run. You need to wait until it's finished, then check. Also, you're leaking global variables in that getResources function, there's no need for them to be global.
function getResources()
local Constr = Self:getConstraints()
local i = 0
timer.create("Res",0.01,#Constr,function()
i=i+1
Cur = Constr[i]
printConsole(Cur:class())
if Cur:class() == "ship_core" then
SCore = Cur
Co1 = 1
printColor(Color(255,255,255),"Ship Core Found")
printConsole(SCore)
elseif Cur:class() == "stargazer_node" then
Node = Cur
No1 = 1
printColor(Color(255,150,150),"Resource Node Found")
printConsole(Node)
elseif Cur:class() == "st_shield_emitter" then
Shield = Cur
Sh1 = 1
printColor(Color(100,200,250),"Shield Emitter Found")
printConsole(Shield)
elseif Cur:class() == "sensor_array" then
Sensor = Cur
Se1 = 1
printColor(Color(100,250,100),"Sensor Array Found")
printConsole(Sensor)
end
if i == #Const then
-- Now check your values (probably in another function so this one doesn't become a mammoth).
end
end)
AllCores = find.byClass("ship_core")
for i=1,#AllCores do
printConsole(AllCores[i])
AllCoresOwn[i] = AllCores[i]:owner()
printConsole(AllCoresOwn[i]:name():lower())
end
end
RE: Timer/whatever problem - Destiny's Player - 01-10-2015
Thanks. I've also found the reason why it didn't work before. Because the aforemental "if all of these == 1 then" check only runs on start.
|