Wait_For_Event(0);

Archive of the older Feature Request Forum Posts
Locked
User avatar
MontuZ
Forum Regular
Posts: 338
Joined: Fri Feb 10, 2006 8:08 am

Wait_For_Event(0);

Post by MontuZ »

Get_Queued_Event(); - Would just return the most recent queued event.
Get_Events_Queued(); - Would return an array of queued events.

I'd like to have these for NPCs. For instance; when you have an npc chase after a mobile, that npc is only going to run after the player, unless you put in a wait for event for 1 second(I tried 0 and nothing happens), then every loop that npc will stop for 1 second, which makes for a very slow and easy to kill npc.

What some of my players are doing is sending out one player to have the npc chase them, while another player sits and attacks/kills the npc.

I'm not using the latest version of POL, but when I convert(which might be never, lol because of how incredibly stable we are atm.) It would be nice to have those options to make better AI.

Good idea?
User avatar
OldnGrey
POL Expert
Posts: 657
Joined: Sat Feb 04, 2006 6:26 pm

Post by OldnGrey »

Whenever I've see the message on the console about npc event queues being full, and events being discarded, I've wanted more tools to deal with the situation. Events trigger events in bewildering cascades of confusion at times and to be able to test and list the event queue would be extremely handy for debugging.

So for entirely different reasons, having Get_Events_Queued(); would be most welcome.
User avatar
ncrsn
Grandmaster Poster
Posts: 255
Joined: Fri Feb 10, 2006 12:15 am

Post by ncrsn »

I just tested using 097 RC5 core, Wait_For_Event(0); returned event from queue.

Using that information, scripting function Get_Events_Queued() is possible.

Code: Select all

function Get_Events_Queued( )
    // Result will be array of structs.
    var result := array;
    var ev;
    
    while (ev := Wait_For_Event(0))
        result.Append(ev);
    endwhile
    
    return result;
endfunction
That's pseudocode, but I think the idea will do.
User avatar
CWO
POL Expert
Posts: 1160
Joined: Sat Feb 04, 2006 5:49 pm

Post by CWO »

Works on 096.4 which I use for my shard. Are you completely sure in your tests that there is another event in the queue? Wait_for_event(0) does absolutely nothing if theres no event in the queue. I'm sure you know that but putting 0 for WFE never failed for me.
User avatar
MontuZ
Forum Regular
Posts: 338
Joined: Fri Feb 10, 2006 8:08 am

Post by MontuZ »

It wouldn't return events that are queued. We want to get the events that are sent to the script while the script is doing something and not listening for events... understand?

Code: Select all

function Get_Events_Queued( )
    // Result will be array of structs.
    var result := array;
    var ev;
    
    while (ev := Wait_For_Event(0))
        result.Append(ev);
    endwhile
    
    return result;
endfunction
CWO: I've tried 0 several times, but I will try again. I was probably over looking something.
User avatar
ncrsn
Grandmaster Poster
Posts: 255
Joined: Fri Feb 10, 2006 12:15 am

Post by ncrsn »

MontuZ wrote: We want to get the events that are sent to the script while the script is doing something and not listening for events
And that's exactly what it does.

When process gots event, POL saves it into memory. I call it queue, which is array of events sent to process. Using Wait_For_Event(x) POL will return first event from the queue, no matter when it was send. And if queue is empty, it will sleep x seconds waiting for one.

Wait_For_Event(0) checks if queue is empty, and whether or not it is, returns immediately. That's why using the function I wrote one could swap the queue from POL into the script process.

Tested it out using AI-script with enabled event SYSEVENT_SPEECH. First it slept for 10 seconds, while my character flooded various texts in game next to the npc. After 10 seconds, script called and printed out result of Get_Events_Queued(). Surprisingly every phrase I flooded while npc was sleeping printed into console.

Disclaimer: I do not really know how POL handles this stuff; what I told above is just a way I quess it works.
User avatar
Austin
Former Developer
Posts: 621
Joined: Wed Jan 25, 2006 2:30 am

Post by Austin »

Stop using single scripts to handle AI. If you get an event you have to process it. If you do everything in the same script, some things will have to wait.

Look into the brain AI in the distro to create npcs that can perform multiple tasks.

wait_for_event(#) waits # number of seconds before continuing on
if you have 5, it will wait UP to 5 seconds for an event
0 means it just keeps looping. If there is an event, itll take it, if not, it keeps going anyways. It gives you the most recent event in the queue.

Events_Waiting() will tell you if there is any events .... waiting.
zandor
New User
Posts: 14
Joined: Sat Feb 04, 2006 10:13 am

Post by zandor »

What about define 2 threads in single ai script?
Something like:

ai.src:

program ai_script()
..code
endprogram

events ai_scripts()
..code
endevents

I know I can always use start_script() but have all code in single src could be more readable...

THe 2 threads could share same global var defined outside program or events section....
Locked