Why does this snippet of code eat my CPU? - Page 2

Why does this snippet of code eat my CPU?

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Why does this snippet of code eat my CPU?

Post by zwzsg »

I can see how mouse press can be get by event, but not how you can add your own time's up event.
User avatar
aegis
Posts: 2456
Joined: 11 Jul 2007, 17:47

Re: Why does this snippet of code eat my CPU?

Post by aegis »

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
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Why does this snippet of code eat my CPU?

Post by zwzsg »

So that widget:Update() is not a polling?
User avatar
aegis
Posts: 2456
Joined: 11 Jul 2007, 17:47

Re: Why does this snippet of code eat my CPU?

Post by aegis »

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 <_<
Regret
Posts: 2086
Joined: 18 Aug 2007, 19:04

Re: Why does this snippet of code eat my CPU?

Post by Regret »

aegis 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
You forgot specific mouse button checks

Also polling in this case has ~0 impact on performance, as do the 2 extra ifs and/or variables used, but yea congratulations. :regret:
User avatar
aegis
Posts: 2456
Joined: 11 Jul 2007, 17:47

Re: Why does this snippet of code eat my CPU?

Post by aegis »

was an example for zwzsg, not for caydr ;)
the point was cleanness
User avatar
Caydr
Omnidouche
Posts: 7179
Joined: 16 Oct 2004, 19:40

Re: Why does this snippet of code eat my CPU?

Post by Caydr »

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 this is exactly what I needed, I'll give it a try later. Thanks as always.

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.
User avatar
aegis
Posts: 2456
Joined: 11 Jul 2007, 17:47

Re: Why does this snippet of code eat my CPU?

Post by aegis »

we said several times that widgets didn't run in parallel and sleeping blocks the main thread
zerver
Spring Developer
Posts: 1358
Joined: 16 Dec 2006, 20:59

Re: Why does this snippet of code eat my CPU?

Post by zerver »

ye, polling is only good if there is no suitable interrupt, or if the interrupt is 'unreliable'
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: Why does this snippet of code eat my CPU?

Post by lurker »

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.
Post Reply

Return to “Lua Scripts”