Why does this snippet of code eat my CPU?
Moderator: Moderators
Re: Why does this snippet of code eat my CPU?
I can see how mouse press can be get by event, but not how you can add your own time's up event.
Re: Why does this snippet of code eat my CPU?
Code: Select all
local pressed = -1
function widget:MousePress()
pressed = clock()
end
function widget:MouseRelease()
pressed = -1
end
function widget:Update()
if (pressed >= 0) and (clock() - pressed > 0.25) then
fire()
-- option 1: don't shoot until clicked again
pressed = -1
-- option 2: shoot again after timeout
-- pressed = clock()
end
end
Re: Why does this snippet of code eat my CPU?
So that widget:Update() is not a polling?
Re: Why does this snippet of code eat my CPU?
it's not polling the mouse state
I don't think spring has an event for "x amount of time passed"
compare my code to regret's. mine pretty much doesn't do *anything* unless a single variable is set.
his full polling method uses four lasting variables, calls GetMouseState to set five variables, then runs through a mess of if statements <_<

compare my code to regret's. mine pretty much doesn't do *anything* unless a single variable is set.
his full polling method uses four lasting variables, calls GetMouseState to set five variables, then runs through a mess of if statements <_<
Re: Why does this snippet of code eat my CPU?
You forgot specific mouse button checksaegis wrote:Code: Select all
local pressed = -1 function widget:MousePress() pressed = clock() end function widget:MouseRelease() pressed = -1 end function widget:Update() if (pressed >= 0) and (clock() - pressed > 0.25) then fire() -- option 1: don't shoot until clicked again pressed = -1 -- option 2: shoot again after timeout -- pressed = clock() end end
Also polling in this case has ~0 impact on performance, as do the 2 extra ifs and/or variables used, but yea congratulations.

Re: Why does this snippet of code eat my CPU?
was an example for zwzsg, not for caydr 
the point was cleanness

the point was cleanness
Re: Why does this snippet of code eat my CPU?
I think this is exactly what I needed, I'll give it a try later. Thanks as always.Regret wrote:Code: Select all
local midclicktimer --placeholder local pm1,pm2,pm3 --placeholder function widget:Update() local _,_,m1,m2,m3 = Spring.GetMouseState() --get mouse button status if (m2 and (not pm2)) then --is pressed and was not pressed before == was pressed right now midclicktimer = os.clock() + 0.25 --initialize timer end if (midclicktimer ~= nil) then --timer initialized? if (m2) then --is button still pressed? if (os.clock() >= midclicktimer) then --enough time passed? midclicktimer = nil --reset timer yourfunction() --do shit end else --nope midclicktimer = nil --reset timer end end pm1,pm2,pm3 = m1,m2,m3 --remember last mouse state end
I think that basically the brick wall I was hitting was my assumption that widgets are run parallel, as would seem pretty sensible. But that's just me, the guy that thinks a "wait" shouldn't be interpreted as "stall user's computer" and the game shouldn't allow sloppy programming to be able to damage my hardware.
Re: Why does this snippet of code eat my CPU?
we said several times that widgets didn't run in parallel and sleeping blocks the main thread
Re: Why does this snippet of code eat my CPU?
ye, polling is only good if there is no suitable interrupt, or if the interrupt is 'unreliable'
Re: Why does this snippet of code eat my CPU?
While loops are not waits, calls to sleep() are waits.
Just think about if for a bit and you see that spring has to let gadgets complete to not desync, spring has to let widgets in draw calls complete or corrupt the screen, spring has to not change while lua is doing anything with a loop of units or they'll crash horribly on random days, there is just no way to make lua wait without an explicit sleep() function. sleep could be added, but it's not at all what you were doing.
Just think about if for a bit and you see that spring has to let gadgets complete to not desync, spring has to let widgets in draw calls complete or corrupt the screen, spring has to not change while lua is doing anything with a loop of units or they'll crash horribly on random days, there is just no way to make lua wait without an explicit sleep() function. sleep could be added, but it's not at all what you were doing.