Problems with ProcessClick

Started by edmundito, Wed 20/10/2004 03:34:20

Previous topic - Next topic

edmundito

Hey,

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

under on_mouse_click
Code: ags

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?

Goot

Try a Wait(1) after the process click.

strazer

#2
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?

Scummbuddy

goot, thats been tried, but no success.
- Oh great, I'm stuck in colonial times, tentacles are taking over the world, and now the toilets backing up.
- No, I mean it's really STUCK. Like adventure-game stuck.
-Hoagie from DOTT

Radiant

Yes, this is a tricky bit of timing. What you could do is this:

Code: ags

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

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


edmundito

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!

Scummbuddy

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.
- Oh great, I'm stuck in colonial times, tentacles are taking over the world, and now the toilets backing up.
- No, I mean it's really STUCK. Like adventure-game stuck.
-Hoagie from DOTT

Scorpiorus

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.

SMF spam blocked by CleanTalk