Event handler and idle time

Discuss any general programming issues here
Post Reply
ilbarbax
Posts: 155
Joined: Thu Apr 01, 2010 6:41 pm

Event handler and idle time

Post by ilbarbax »

Is there a way to run a function for doing something during the event handler idle times?
Bugala
Posts: 1400
Joined: Sun Feb 14, 2010 7:11 pm

Re: Event handler and idle time

Post by Bugala »

Yes.

This is part from one of the Hollytwood examples:

Code: Select all

SetInterval(1, p_Update, 1000\60)

; main loop
Repeat
	WaitEvent
Forever
Idea with this is that you are using Waitevent as the mainloop.

Then all the functions you wish to be exewcuted during that, you will use SetInterval()

First number is ID number, second is the name of the function to be exectued and third number is how often (milliseconds) it will be executed. 1000 milliseconds means each second.
ilbarbax
Posts: 155
Joined: Thu Apr 01, 2010 6:41 pm

Re: Event handler and idle time

Post by ilbarbax »

@Bugala

This is the simple solution I already applied. I also considered a semaphore method to avoid starting the interval activities when the user is doing something else by one of the functions handled by installeventhandler.
The problem is that with this method what I need to do can't be fragmented trough the different idle times. When it start it has to complete the whole interval task locking the user event.
I was hoping if there was a better and cleaner method.

I wishing for Andreas...to have a new event handler: onidle event
Bugala
Posts: 1400
Joined: Sun Feb 14, 2010 7:11 pm

Re: Event handler and idle time

Post by Bugala »

I have to say, that would be good idea.


This isnt simple solution, but basically you could use what i use to halt the program a bit from running too fast. For thing is, when you make a game, and you make the movement based upon how much time have passed, it can happen with faster machines, that since smallest time measurement in hollywood is 1 MicroSecond, it might take less than half of that to make the cycle, and in that case it rounds it down to 0, which means that nothing moves anywhere as everytime cycle keeps going, it keeps multiplying the movement by 0 (which is the recorded time that have passed).

Hence what i roughly do is (I dont remember exact commands right now):

Code: Select all

continue = 0
repeat
timepassed = gettimepassed()
if timepassed > 1 then continue = 1
until continue=1

This could be used differently as well, you could check that if certain amount of time have not passed yet, then it would jump to function idlefunctions()

this idlefunctions, would consist of a list of subfunctions which are to be executed one at a time until time have been reached.

in practice it would be about like this:

Code: Select all

p_idlefunctions()

function1()
gettimepassed()
checkifthereisstilltime()
if timeleft < 50 then break

function2()
ettimepassed()
checkifthereisstilltime()
if timeleft < 50 then break
This alone isnt very good yet, since then it would keep checking the first functions all the time, and maybe never get to run the last, hence you would need either to use multiple if else statments perhaps, or some sort of list solution, where list contains all your functions.

I am not sure how this actually works, but in theory:

Code: Select all

idlefunctions_table = {function1(), function(2), function3()}

p_idlefunctions()

repeat
idlefunctions_table[ nextfunction ]
nextfunction = nextfunction + 1
if nextfunction > amount of functions in list    then nextfunction=1 
if timeleft < 1 break
forever

endfunction
This would work about the way that nextfunction would keep track of which function to execute next on idle time. For example, if you have 5 functions, and you are able to execute 3 of them, then next time you come to this idlefunction place, it would execute function number 4.

This way everytime you would have idle time, it would use that time to execute idlefunctions in order without skipping them or repeating the same all the time.

However, I am not sure how does that using function on a list actually work. I know list can contain functions, but how do you use functions inside a list, that i have never tried.
User avatar
airsoftsoftwair
Posts: 5887
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Event handler and idle time

Post by airsoftsoftwair »

Well, even if there was an "OnIdle" event handler it wouldn't work the way you want it because it always would run the whole event callback before returning control to WaitEvent() because that's the way Hollywood events work. Hence, you should probably do it using an interval timer... you can set a flag that skips the interval timer whenever you are handling a different event and want to block the idle timer.
Post Reply