MV Script to execute common event with timing-based intervals

Hi everyone,

I've tried really really hard to figure this one out on my own, but I am not proficient in code. I'm trying a rather ambitious task of getting an animation to play on a particular event via a common event that gets executed when certain conditions are met.

The condition is a timed period that is determined by a random number chosen between 1 and 4.

1 = 60 frames
2 = 120 frames
3 = 180 frames
4 = 240 frames

Additionally, I also need to make sure that the player isn't standing on the same tile of the event that the script is executed in.

If they are, the event continues past this script and uses other scripts instead (which work).

Here's what I tried to come up with - this is only the latest iteration of it, so there's probably a lot of double-handling as I was trying to figure out why I couldn't get it to work.

Code:
var ani = Math.random() * (5 - 1) + 1; var wait = 0; var px = $gamePlayer.x; var py = $gamePlayer.y; var ex = $gameMap.event(this._eventId).x; var ey = $gameMap.event(this._eventId).y;
if (ani == 1){
while ((px!== ex) && (py !== ey) && (wait < 61)){
if (wait >= 60){
$gameTemp.reserveCommonEvent(40);
}
if (wait >= 61){
break;
}
wait + 1;
}
}
if (ani == 2){
while ((px !== ex) && (py !== ey) && (wait < 121)){
if (wait >= 120){
$gameTemp.reserveCommonEvent(40);
}
if (wait >= 121){
break;
}
wait + 1;
}
}
if (ani == 3){
while ((px!== ex) && (py !== ey) && (wait < 181)){
if (wait >= 180){
$gameTemp.reserveCommonEvent(40);
}
if (wait >= 181){
break;
}
wait + 1;
}
}
if (ani == 4){
while ((px!== ex) && (py !== ey) && (wait < 241)){
if (wait >= 240){
$gameTemp.reserveCommonEvent(40);
}
if (wait >= 241){
break;
}
wait + 1;
}
}

Any thoughts to help me with this mess appreciated. Thanks!

EDIT: I tested the above script on a stand-alone parallel event without anything else going on with it to make sure nothing else on the same event was interfering with it. It still didn't work.

The common event it calls, for the moment, it just a simple "Show Text" command with nothing else in it for testing purposes.
Your script will run as fast as the processor can handle it, i.e. almost certainly within one frame. The engine has no way of knowing you want the while loop to update once per frame, and since the script is evaluated as a single block (from the event interpreter's perspective) there's no neat way to tell the interpreter to wait 1 frame between loops. 😅

I think this can all be done with event commands (1 switch + 2 variables), e.g. (untested):
Spoiler: event suggestion
Code:
◆Comment:Initialise wait counter
◆Control Variables:#0020 Time = Random 1..4
◆Control Variables:#0020 Time *= 60
◆Comment:Wait that many frames
◆Loop
  ◆If:Time ≤ 0
    ◆Break Loop
    ◆
  :Else
    ◆Control Variables:#0020 Time -= 1
    ◆Wait:1 frame
    ◆
  :End
  ◆
:Repeat Above
◆Comment:Check player position - OK if not sharing tile with this event.
◆Control Variables:#0001 Position = Map X of Player
◆Control Variables:#0001 Position -= Map X of This Event
◆If:Position = 0
  ◆Control Variables:#0001 Position = Map Y of Player
  ◆Control Variables:#0001 Position -= Map Y of This Event
  ◆If:Position = 0
    ◆Control Switches:#0005 Position OK = OFF
    ◆
  :Else
    ◆Control Switches:#0005 Position OK = ON
    ◆
  :End
  ◆
:Else
  ◆Control Switches:#0005 Position OK = ON
  ◆
:End
◆If:Position OK is ON
  ◆Comment:Call common event!
  ◆Common Event:Main event
  ◆
:Else
  ◆Comment:Call alternative~
  ◆Common Event:Alternative
  ◆
:End
This event would have parallel trigger so it runs in the background. 🙂
Thanks @caethyril - you've always got an answer for me!

Unfortunately, this is an event I want to copy and paste en masse so I don't really want to be using up game resources like switches and variables - does that make sense?

I wanted the event to be entirely self-contained and only reference itself, do you know if this is possible?
look for one of the self-variables plugins and change all switches to self-switches and all variables to self-variables
Thanks @Andar, I'll look into this. Cheers!
Hi again @caethyril / @Andar ,

The issue here is that if the player stands on the same X and Y coordinates as the event itself and the wait tick down is already in process, this will not break and execute the common event set up below it. Am I reading that right?

Let me explain this further.

I need one event to do more than just what I've listed. I didn't specify this in my original post (regretfully), because I just wanted a method of breaking the (terrible) script every 1 frame to check for other criteria.

Let me fully explain. I need this singular event to:

1) Check the player X and Y against the event X and Y, and turn a switch on if this is the case.

2) Check the player X and Y against the event X and Y, and also check if they are pressing OK to trigger a common event if this is the case.

3) Run a timer that has 1 of 4 potential values (60 frames, 120 frames, 180 frames, 240 frames) than when complete, runs an animation on the event.

At any point during the timer process, if the player is fulfilling the criteria of 2, then I need to break the timer and just prioritize 2.

This is why I'm trying to figure out a way of using 1 frame intervals once the timer value has been established, to check if 2 has been fulfilled, so that it can break this timer if necessary. If the timer loop is not broken when the player is in position and pressing OK, then the common event will not trigger.

The reason I can't just use parallel events, is because this is an event I'm going to be using as a principle component of gameplay, and very often. if I have to use more than one event, it will double the workload.

Does this all make sense? Let me know if I've missed anything.

EDIT: I feel like I'm very close with getting what I need, but having trouble with one aspect. First of all, here's my event set up, head to toe:

Spoiler
Code:
◆Label:Top
◆Script:var px = $gamePlayer.x; var py = $gamePlayer.y; var ex = $gameMap.event(this._eventId).x; var ey = $gameMap.event(this._eventId).y; var key = [this._mapId, this._eventId, "A"];
:      :if ((px == ex) && (py == ey) && (Input.isPressed('ok'))){
:      :$gameTemp.reserveCommonEvent(35); $gameSelfSwitches.setValue(key, 1);
:      :}
◆Script:var px = $gamePlayer.x; var py = $gamePlayer.y; var ex = $gameMap.event(this._eventId).x; var ey = $gameMap.event(this._eventId).y;
:      :if ((px == ex) && (py == ey)){
:      :$gameSwitches.setValue(32, true);
:      :} else {
:      :$gameSwitches.setValue(32, false);
:      :}
◆Comment:Initialize Wait Counter
◆Control Variables:#0121 Self Var Alpha = Random 1..4
◆Control Variables:#0121 Self Var Alpha *= 60
◆Comment:Wait Frames
◆Loop
  ◆If:Self Var Alpha ≤ 0
    ◆Break Loop
    ◆If:Script:$gamePlayer.x == $gameMap.event(this._eventId) && $gamePlayer.y == $gameMap.event(this._eventId)
      ◆Jump to Label:Top
      ◆
    :End
    ◆
  :Else
    ◆If:Script:$gamePlayer.x == $gameMap.event(this._eventId) && $gamePlayer.y == $gameMap.event(this._eventId)
      ◆Jump to Label:Top
      ◆
    :End
    ◆Control Variables:#0121 Self Var Alpha -= 1
    ◆Wait:1 frame
    ◆
  :End
  ◆
:Repeat Above
◆Show Animation:This Event, MapFX: Treasure

The issue I'm having trouble with is that Switch 32 turns on a parallel common event, which looks like this:

Spoiler
1598705936417.png

So as you can see, the common event activates a balloon icon over the player.

The issue is, it's intermittent. Sometimes the icon appears right away, sometimes it won't trigger until the animation does, which is odd as it should be breaking if the player is standing on the same X and Y as the event.

So that's really the only issue left - if I could these working in sync with JUST the one event, that would be exactly what I'm looking for.
Okay, I don't fully understand what you mean, but I can help with some ideas!
What you want is a Parallel Event (preferably Common Event for a bit more optimisation) that will handle the coordonate checking.

Don't use global variables for coordonates, use:
1. $gameMap.event(id).x and $gameMap.event(id).y for events;
2. $gamePlayer.x and $gamePlayer.y for player.
So that player can be on an event, it needs to be Below Characters, so that you can actually step on it, else if it's Same as Characters it won't allow passage. Unless player has Through ON option active, though I doubt you'd want that. Should work for Above Characters.

To observe that in a script:
if (($gamePlayer.x - $gameMap.event(id).x == 0) && ($gamePlayer.y - $gameMap.event(id).y == 0) {
// code; }
else if (($gamePlayer.x - $gameMap.event(id).x != 0) || ($gamePlayer.y - $gameMap.event(id).y != 0) {
// code; }

TO check if a button is pressed I found these scripts in another post:
Input.isTriggered('ok'))
(Input.isRepeated(ok'))
(Input.isPressed('ok')) - guessing this is what you'll need!
(Input.isLongPressed('ok'))

(TouchInput.isTriggered())
(TouchInput.isRepeated())
(TouchInput.isPressed())
(TouchInput.isLongPressed())
(TouchInput.isCancelled())
(TouchInput.isMoved())
(TouchInput.isReleased())

The problem I see with Parallel events is they cannot be closed from outside before they end their code.
So you will need a 2nd Parallel Common Event to run your "timer" and animation afterwards. You don't need this to run every frame, cause movement usually takes 30 to 60 frames, depending on speed, plus player pressing the button would be more than 1 second as action time in total.

So your second event would be something like:
var $gameVariables.setValue(ID, (Math.floor(Math.random() * 4 + 1))); // Math.floor() to remove any decimals
this being a Parallel Common event, it cannot use "this", so you're waiting will have to be command line. better to do as well with the conditionals corresponding to each value (can use script part in conditional)
if $gameVariables.value(ID) == 1 then wait 60 frames
if $gameVariables.value(ID) == 2 then wait 120 frames
if $gameVariables.value(ID) == 3 then wait 180 frames
if $gameVariables.value(ID) == 4 then wait 240 frames


either try
FIRST METHOD
// check if button is pressed, since i feel that's the more important condition, then check coordonates.
if ((Input.isPressed('ok')) && (($gamePlayer.x - $gameMap.event(id).x == 0) && ($gamePlayer.y - $gameMap.event(id).y == 0))) {
// guess you will turn on the "timer" here
$gameSwitches.setValue(y, true); // this will be your separate Parallel Common Event timer, close its switch at the very end!
OR $gameTemp.reserveCommonEvent(40); // if timer is not Parallel
} else if (($gamePlayer.x - $gameMap.event(id).x == 0) && ($gamePlayer.y - $gameMap.event(id).y == 0)) {
// no button press but coords coincide
$gameSwitches.setValue(x, true); // turn the other switch on that you mentioned.
}

Now, for the timer event, it can be parallel or simple Common Event, no problem.
var $gameVariables.setValue(ID, (Math.floor(Math.random() * 4 + 1))); // Math.floor() to remove any decimals
if $gameVariables.value(ID) == 1
if $gameVariables.value(ID) == 2
if $gameVariables.value(ID) == 3
if $gameVariables.value(ID) == 4

NO NEED FOR THE LABELS I initially put

if ((Input.isPressed('ok')) && (($gamePlayer.x - $gameMap.event(id).x == 0) && ($gamePlayer.y - $gameMap.event(id).y == 0))) {
$gameSwitches.setValue(x, false); // turn this timer's switch OFF if Parallel Common Event
OR $gameTemp.eraseCommonEvent(y); // end the timer - I would go for the Parallel version
} else if (($gamePlayer.x - $gameMap.event(id).x == 0) && ($gamePlayer.y - $gameMap.event(id).y == 0)) { // no button press
wait: 30 frames (cannot be triggered from Parallel Common Events, so use Event commands instead, add the scripts in the conditional branch script (page 4)!)
}
You have this branch that tests your conditions and if met, it breaks your "timer", else it waits 30 frames (else condition could be whatever your need is.
Add as many as you need to fulfill your time need.
Do this for each conditional, labels won't help on second thought.
So 2 of this for value == 1, 4 for value == 2, and so on.

Add
$gamePlayer.requestAnimation(id); // add this after the whole list of repetitions, since it's only gonna be triggered if this timer event was not closed anyways.
$gameSwitches.setValue(y, false); // close the switch of this timer event if Parallel, don't forget 😉



SECOND METHOD
this could be easier a bit, again I'd go again with 2 Parallel Common Events. Except the timer will use a dummy Switch (namely one that will not be used anywhere else in your game)!
Have your event that checks the conditions, and when it triggers the timer, the timer will run in Parallel turning on the dummy switch.
So it'll be like this:
MAIN EVENT will work its wonders, and if coordonates coincide AND the button is pressed you shut down the dummy switch.
The timer will be like the one present up initially, without the wait 30 frames and condition checking. Just normal wait

$gameVariables.setValue(y, Math.floor(Math.random() * 4 + 1));
$gameSwitches.setValue(x, true); // turn on dummy switch
if 1 wait 60 frames
if 2 wait 120 frames
if 3 wait 180 frames
if 4 wait 240 frames
// check if this switch is false, play animation ONLY if false, don't forget this is turned false ONLY when your main conditions are met.
if ($gameSwitches.value(x) == false) { // so you wait as normal, without any painful extra checking
$gamePlayer.requestAnimation(id);
// close switch of this timer event as well if Parallel
}


Problem here is your animation will trigger most likely with a delay, since player still has to wait!
To solve this, change your main condition (if button is pressed AND coordonates coincide) to have the PLAYER wait instead of the event. Namely, instead of using wait: x frames command line, use Set Movement Route then the Wait funciton. Kind of got lost to what exactly would trigger your timer, so if this main condition triggers your timer, then simply replace wait frames in it with Set Movement Route: Player and use Wait amount in them, associated with your randomiser conditionals. Hope this helped!
If you have it already or are willing to pay for it, you can set it up as a physical event and call it from any map using YEP Call Event
Hi everyone,

Thanks so much for the help! I've solved the issue!

Using a bit of input from all the suggestions, a friend and I continued to work on the script and figure out a way to get the optimal result. For anyone who's curious - I'll stick it here. It's a bit scrunched because of the 12 line MV Script box limitation:

Spoiler
Code:
var count = this.getSelfVariableValue(this._mapId, this._eventId, 121); var px = $gamePlayer.x; var py = $gamePlayer.y; var ex = $gameMap.event(this._eventId).x; var ey = $gameMap.event(this._eventId).y; var playerOnTile = (px == ex && py == ey);

if (count == 0){

    var min = 60; var max = 240; count = Math.floor(Math.random() * (max - min)) + min;

    if (!playerOnTile){    $gameMap.event(this._eventId).requestAnimation(155);}

}

--count; this.setSelfVariableValue(this._mapId, this._eventId, 121, count);

if (playerOnTile){$gameSwitches.setValue(32, true);if (Input.isPressed('ok'))$gameTemp.reserveCommonEvent(35);}

else if (px == ex && py == ey + 1 || px == ex && py == ey - 1 || px == ex + 1 && py == ey || px == ex -1  && py == ey){$gameSwitches.setValue(32, false);}

this.wait(1);

Thanks!

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/script-to-execute-common-event-with-timing-based-intervals.126375/