Hey there. While I was working on my chip today I noticed that there seems to be a problem with changing targets on the sensor array and getting the outputs via wirelink. This is the code I am talking about:
Code:
--@name Test
--@author SuperPlayer
wire.createInputs( {"IN"}, {"ENTITY"} )
Sensor = {}
Sensor.Entity = find.byClass("sensor_array",function( Ent ) return Ent:owner() == ents.owner() end )[1]
Sensor.Wirelink = Sensor.Entity:getWirelink()
hook.add( "input", "hookname", function( input, value )
Sensor.Wirelink["Target"] = value
printStuff(value)
end )
function printStuff(E)
print(E:class())
for k,v in pairs(getStatus(E)) do
print(k,v)
end
end
function getStatus(Ent)
if Ent:class() == "player" then
return {Health = Ent:health(), Armor = Ent:armor()}
elseif Ent:class() == "ship_core" then
return {Hull = Ent:entHealth(), MaxHull = Ent:entMaxHealth(),Plating = Ent:platingStrength(), Shield = Ent:getShield():getTotalCharge(),MaxShield = Ent:getShield():getMaxCharge()}
elseif Ent:class():find("box_") then
return {Hull = Ent:entHealth(), MaxHull = Ent:entMaxHealth()}
elseif Ent:class():find("st_shuttle_") or Ent:class() == "sci_fi_shuttle" then
return {Hull = Sensor.Wirelink["Health"]or 0, MaxHull = Sensor.Wirelink["Max Health"]or 0, Shield = Sensor.Wirelink["Shields"]or 0, Armour = Sensor.Wirelink["Armour"]or 0}
else
return {Health = Ent:entHealth()}
end
end
When changing the inputs the following gets printed
As you can see there seems to be an error what the sensor array returns. It looks like some previous values dont get overidden or something. Also when changing to a ship_core or borg cube everything works fine.
But when changing to a shuttle the problem occurs. Ano figured out that if you delay printStuff(value) by 1 second everything works. Now my question is if that is intentional or a bug?
Also while we are on it, could you explain how to use
Code:
profiler.push("TargetStuff")
and
Code:
profiler.pop("TargetStuff")
properly? Because I always run into "Tried to pop too much".
I'm pretty sure sensors have a hardcoded delay if you attempt to change targets more than so many times under a certain amount of time (though i'm not sure) and profilers I don't use often but you use profiler.push(name) to start a profiler state and then just profiler.pop() to end it. If you attempt to pop without pushing one it will error.
profiler.push("test")
for i=1,2048 do
printConsole(i)
end
profiler.pop()
--Then use the profiler tool on the chip
"with it's superior software, i have no doubt it will succeed in finding fuck all" - T-17, 2016
[Super Member] [ENCOM] т-ѕсм.Red The Blue Color™: server do you support Hitler?
[Server] srcds_linux: The probabilty is about 99.99%, so interpret that how you want to.
The sensor array refreshes in a short interval rather than immediately when the target changes. It's probably better to watch the outputs for changes using Wirelink:addOutputCallback() rather than to read directly from them.
As for the profiler, as Red said, it's a stack, so the error you're getting means you're popping when you've not pushed anything yet.
I thought wirelinks (and wire links) aren't immediate, ever (unless the source entity updates before the target entity). For high-speed operations you need to use the readcell/writecell functionality (which is not well supported).
Ok thanks for the help with the sensor part.
For the profiler part: The example red posted works but when using the following code it get the error [SF - ERROR] SFamaritan/Samaritan.txt:670: Attempted to pop too many profiler states.
Code:
if Samaritan.Values.Tactical.WeaponCode ~= "0|0|0|0" then
Samaritan.Operations.UpdateTargetPos()
if not Profiling then
print("Profiling now")
profiler.push("TargetStuff")
Profiling = true
end
end
if Samaritan.Values.Tactical.WeaponCode == "0|0|0|0" and Profiling then
profiler.pop()
Profiling = false
print("Not profiling anymore")
end
This code is a function which is called every 0.2 seconds. It is always printing starting profiling but when ending it the error happens.
And i tested just letting the profiler run for a few seconds, it still happens
Ah I see the confusion. The profiler can only measure inside a single call, as soon as the event/timer exits the profiler is reset.
The idea is to profile each occurrence of a function/event/timer, so you can see how long a specific piece of code is taking. Leaving the profiler open over time will not give you useful results as it would include the actual time passed where it's not doing anything.