Stepping sounds "this event" using script

So instead of using a plugin I am using scripting to do stepping sounds for the player and certain events/NPCs.

I am using the following for NPCs

$gameVariables.value(4) != $gameMap.event(1).x || $gameVariables.value(5) != $gameMap.event(1).y;

As you can see this one is targeting the first event on the map but I want the target to be "this event" so how do I change the script to reflect that?
Assuming it's in a Game_Interpreter context (e.g. an event command like Conditional Branch > Script) then you can use this.character(0), e.g.
JavaScript:
$gameVariables.value(4) !== this.character(0).x || $gameVariables.value(5) !== this.character(0).y
Or, slightly more concisely:
JavaScript:
this.character.pos($gameVariables.value(4), $gameVariables.value(5))
Note that the this keyword is context-dependent. If it's in a move route, replace this.character(0) with this to reference the character being moved~
Assuming it's in a Game_Interpreter context (e.g. an event command like Conditional Branch > Script) then you can use this.character(0), e.g.
JavaScript:
$gameVariables.value(4) !== this.character(0).x || $gameVariables.value(5) !== this.character(0).y
Or, slightly more concisely:
JavaScript:
this.character.pos($gameVariables.value(4), $gameVariables.value(5))
Note that the this keyword is context-dependent. If it's in a move route, replace this.character(0) with this to reference the character being moved~

Thanks! Here is what I have so far

1602094791093.png


Note that this a is a parallel common event and as such could very well trigger on any map at any time.

Now the question is, how do I tell it which event(s) (as there may be more than one) is "this event"?
In this case "this event" is the parallel common event, which doesn't move: it has no location because it's a common event. Also note that your setup uses two variables to store two values (x & y), but that will only suffice for one event. 😅

I believe this would be much simpler as a plugin, e.g. (untested):
Spoiler: Suggested plugin code
JavaScript:
(alias => {
  Game_CharacterBase.prototype.increaseSteps = function() {
    alias.apply(this, arguments);  // invoke original method
    const se = { name: 'step', volume: 20, pitch: 100 };
    AudioManager.playSe(se);  // footstep sound stuff
  };
})(Game_CharacterBase.prototype.increaseSteps);
This is a basic example: play a fixed sound every time any character moves. You could add features in there, e.g. random pitch/volume, change volume based on player proximity, reference event notetags or terrain tags for footstep sounds, etc. 😴

If, for whatever reason, you're set on doing this with Script commands, I can't think of a better approach than your current idea. It'll need some adjustments, though:
  • Some means of storing x & y coordinates for each relevant character. You could do this by turning your current variables into arrays, e.g. Control Variables > Set > Script: [], then assigning values via Script command, e.g.
    JavaScript:
    const ev = $gameMap.event(1); $gameVariables.value(5)[ev.eventId()] = { x: ev.x, y: ev.y };

  • A loop for relevant characters? Otherwise you'd have to do this for each character individually. E.g. instead of referencing "this event", loop through a list of events and check/update their positions every few frames, e.g.
    Spoiler: Event position check loop
    JavaScript:
    $gameMap.events().forEach(ev => {
      const loc = $gameVariables.value(5)[ev.eventId()];
      // check: stored position exists and event is not there?
      if (loc && !ev.pos(loc.x, loc.y)) {
        // if so, do the stuff
      }
    });
    While this could be done with a for loop or Array.forEach (as above), it'd be best to set up a for loop structure using event commands (Loop/Break Loop/Conditional Branch + loop counter variable). Otherwise I think you can actually script less and end up with better performance by making it a plugin. 😅

  • Maybe also consider increasing the wait time between checks. For reference, default frames per tile of movement is 2 ** (8 - moveSpeed) (not counting stop time due to move frequency). E.g. at move speed 4 a character takes 2 ** 4 = 16 frames to move from one tile to the next. 🙂
[Edit: changed "all map events" to "relevant characters".]
In this case "this event" is the parallel common event, which doesn't move: it has no location because it's a common event. Also note that your setup uses two variables to store two values (x & y), but that will only suffice for one event. 😅

I believe this would be much simpler as a plugin, e.g. (untested):
Spoiler: Suggested plugin code
JavaScript:
(alias => {[/INDENT]
[INDENT]  Game_CharacterBase.prototype.increaseSteps = function() {[/INDENT]
[INDENT]    alias.apply(this, arguments);  // invoke original method[/INDENT]
[INDENT]    const se = { name: 'step', volume: 20, pitch: 100 };[/INDENT]
[INDENT]    AudioManager.playSe(se);  // footstep sound stuff[/INDENT]
[INDENT]  };[/INDENT]
[INDENT]})(Game_CharacterBase.prototype.increaseSteps);
This is a basic example: play a fixed sound every time any character moves. You could add features in there, e.g. random pitch/volume, change volume based on player proximity, reference event notetags or terrain tags for footstep sounds, etc. 😴

If, for whatever reason, you're set on doing this with Script commands, I can't think of a better approach than your current idea. It'll need some adjustments, though:
  • Some means of storing x & y coordinates for each relevant character. You could do this by turning your current variables into arrays, e.g. Control Variables > Set > Script: [], then assigning values via Script command, e.g.
    JavaScript:
    const ev = $gameMap.event(1); $gameVariables.value(5)[ev.eventId()] = { x: ev.x, y: ev.y };

  • A loop for relevant characters? Otherwise you'd have to do this for each character individually. E.g. instead of referencing "this event", loop through a list of events and check/update their positions every few frames, e.g.
    Spoiler: Event position check loop
    JavaScript:
    $gameMap.events().forEach(ev => {
      const loc = $gameVariables.value(5)[ev.eventId()];
      // check: stored position exists and event is not there?
      if (loc && !ev.pos(loc.x, loc.y)) {
        // if so, do the stuff
      }
    });
    While this could be done with a for loop or Array.forEach (as above), it'd be best to set up a for loop structure using event commands (Loop/Break Loop/Conditional Branch + loop counter variable). Otherwise I think you can actually script less and end up with better performance by making it a plugin. 😅

  • Maybe also consider increasing the wait time between checks. For reference, default frames per tile of movement is 2 ** (8 - moveSpeed) (not counting stop time due to move frequency). E.g. at move speed 4 a character takes 2 ** 4 = 16 frames to move from one tile to the next. 🙂
[Edit: changed "all map events" to "relevant characters".]


This... sounds a bit complicated to me. 😅

The plugin idea wouldn't work for me because I also intend to use the screen shake plugin command from the Visustella Core Plugin to have the screen shake in random directions each time the event takes a step.

As for why the screen would shake? Lets just say it's very, VERY big and this event setup is supposed to reflect that.

As of right now I am using the provided example event as a parallel event on a test map to check yet another event on the same map for testing purposes. However, in this case, it is not a common event but is actually on the map itself.

Original thread: https://forums.rpgmakerweb.com/threads/stepping-sounds-this-event-using-script.128388/