Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: edmundito on Wed 20/10/2004 03:34:20

Title: Problems with ProcessClick
Post by: edmundito on Wed 20/10/2004 03:34:20
Hey,

So, I have this automated code that I want to happen after an action has been processed, like this:

under on_mouse_click

if (button == LEFT) {
    ProcessClick(...);
    MyCode();
}


The problem is that MyCode is being executed before process click! and I don't want that! Am I doing something wrong, or is processclick one of those oddball functions like NewRoom, etc?
Title: Re: Problems with ProcessClick
Post by: Goot on Wed 20/10/2004 03:41:31
Try a Wait(1) after the process click.
Title: Re: Problems with ProcessClick
Post by: strazer on Wed 20/10/2004 04:14:05
Quoteis processclick one of those oddball functions like NewRoom, etc?

Possibly. Since AGS can only run one script at a time, it can't run any interactions initiated by the mouse click until the on_mouse_click function has finished.

If you tell us what MyCode is for, what you're trying to do, we could suggest a workaround.

Edit:

On the other hand, does it work if you put the commands from the MyCode function in the on_mouse_click function directly?
Title: Re: Problems with ProcessClick
Post by: Scummbuddy on Wed 20/10/2004 04:15:14
goot, thats been tried, but no success.
Title: Re: Problems with ProcessClick
Post by: Radiant on Wed 20/10/2004 08:33:17
Yes, this is a tricky bit of timing. What you could do is this:


function on_mouse_click () {
  if (button == LEFT) {
    ProcessClick ();
    runmycode = 1;
  }
}

function repeatedly_execute () {
  if (runmycode) {
     MyCode ();
     runmycode = 0;
  }
}

Title: Re: Problems with ProcessClick
Post by: edmundito on Wed 20/10/2004 22:57:39
Radiant's suggestion worked! I guess it's kind of tricky, but It'll do.

To respond other questions, Wait(1) did not work, and MyCode actually represented my code, which is too secret for me to reveal here! heh.

Thanks for you help!
Title: Re: Problems with ProcessClick
Post by: Scummbuddy on Thu 21/10/2004 00:02:01
radiant, that was the next step beyond what I had suggested to netmonkey earlier, but he was right in that my way wouldnt work. nice job.
Title: Re: Problems with ProcessClick
Post by: Scorpiorus on Fri 22/10/2004 12:56:51
Yeah, it's rather tricky. That's what I had investigated:

As strazer already pointed out, the AGS engine runs one script (i.e. a function) at a time.

When the player clicks the mouse, AGS runs the on_mouse_click function in which it is usually checked what button is pressed in order to run the related block of script code. If, for example, the left mouse button is pressed, ProcessClick calls into the engine; the engine checks what interaction (if any) should be triggered and then executes its action commands specified in the Interaction Editor. If there is a Run script action to execute, the engine can't run an attached script code because we are inside the on_mouse_click function which must be finished first. Therefore, the Run Script action's script is queued up and its execution is delayed until on_mouse_click has been done with.

In this case, for instance, MyCode runs first because it's called from within on_mouse_click which is then followed by Run Script's interaction code.

If, for example, you remove Run Script and do everything with interaction editor commands your code will work as expected since actions can run along with on_mouse_click whereas script code can't. ProcessClick won't return until all actions have been done. That actually may be an advantage of doing certain things with Interaction Editor rather than scripting.

Radiant's workaround would work just fine because, while ProcessClick is in action, repeatedly_execute (as any other scripts) is also blocked. Once ProcessClick and after on_mouse_click is done the repeatedly_execute is called and we can do all the stuff we want afterwards.


By the way, the number of scripts that can be queued up is limited to 4, IIRC. So, if we have 5 different Run script actions, only four of them run. That's a good reason for not having too much Run Scipts within a single interaction.