[RMMV] Event Touch Script Call

Discussion in 'RPG Maker MV' started by EmmaB, May 22, 2018.

  1. EmmaB

    EmmaB Veteran Veteran

    Messages:
    38
    Likes Received:
    56
    First Language:
    English
    Primarily Uses:
    RMMV
    Hi everyone,

    I have been trying to make a rabbit survival game, but have come across a problem. I am trying to make an animal that chases the player when the player gets too near it, and then stops chasing the player if it is too far away. I figured out how to do that, but I can't make it that if the event touches the player, the player loses the game. The event can't have a parallel event page and an event touch page running at the same time.
    If I had a script call that said: "if this event touches player", I would be able to insert it onto the parallel page a few times and it might work. Any help would REALLY be appreciated.

    (Sorry if this is the wrong place to put this thread. I couldn't work out if I was meant to put it here, or in Learning Javascript).
     
    #1
  2. IAmJakeSauvage

    IAmJakeSauvage Veteran Veteran

    Messages:
    417
    Likes Received:
    667
    Location:
    Hamilton, ON
    First Language:
    English
    Primarily Uses:
    RMMV
    Have the event be "player touch" and then have the event setting for the rabbit either lead to the game over screen or the lose conditions that you've set. Your parallel event should be a separate event entirely - but if I've misunderstood feel free to share your event pages so we can see. :)
     
    #2
  3. Andar

    Andar Veteran Veteran

    Messages:
    28,437
    Likes Received:
    6,488
    Location:
    Germany
    First Language:
    German
    Primarily Uses:
    RMMV
    Can't be done - but why do you need an additional parallel process page?

    Depending on what exactly you need it for, one solution is to work without parallel (autonomous movement only) and the other is to have an additional event with the parallel process
     
    #3
  4. Poryg

    Poryg Dark Lord of the Castle of Javascreeps Veteran

    Messages:
    3,928
    Likes Received:
    9,925
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    One non-rabbit parallel process event on map will use script command:
    Code:
    script: if (Math.abs($gamePlayer._x - $gameMap.event(1)._x < 5) && Math.abs($gamePlayer._y - $gameMap.event(1)._y < 5)) $gameSelfSwitches.setValue([this._mapId, 1, "A"], true);
    else $gameSelfSwitches.setValue([this._mapId, 1, "A"], false)
    
    Repeat for all events you want this to apply, just replace 1 for the appropriate event ID.
    And the rabbit will have two pages, first empty, will just look like a rabbit, second page will have self switch A as a condition and that page will have a custom move route - move towards hero and condition hero touch.
     
    #4
  5. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,178
    Likes Received:
    723
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    It doesn't look pretty, but putting this line in a Script command in an event's autonomous move route seems to do what you're asking:
    Code:
    if ($gameMap.distance(this.x, this.y, $gamePlayer.x, $gamePlayer.y) < 8) { this.moveStraight(this.findDirectionTo($gamePlayer.x, $gamePlayer.y)); } else { this.moveRandom(); }
    Basically, it says "if this event is less than 8 tiles from the player, path towards the player, otherwise move randomly". Change the 8 to whatever you think is appropriate in your case. Set the event's trigger to Event Touch and it should work out! :kaothx:

    If you'd like the event to move faster when chasing the player, you can just add another Script command to the move route~:
    Code:
    this.setMoveSpeed($gameMap.distance(this.x, this.y, $gamePlayer.x, $gamePlayer.y) < 8 ? 5 : 3);
    This says "set this event's move speed to 5 if they're less than 8 tiles away from the player, or 3 otherwise". Speed 4 is the player's default walk speed, feel free to mess around with the numbers to find out what works.
     
    Last edited: May 22, 2018
    #5
  6. EmmaB

    EmmaB Veteran Veteran

    Messages:
    38
    Likes Received:
    56
    First Language:
    English
    Primarily Uses:
    RMMV
    Thanks for all the replies!

    @IAmJakeSauvage The rabbit is actually the player, so it's not an event page sadly.

    @Andar I needed the parallel process page for the "check to see if this event is close enough to the player". I wasn't sure how to do it otherwise. Here's a screenshot:
    Printscreen 1.png

    @Poryg I'm not completely sure what your saying, but the player is the rabbit so I don't think it would work. (I tried putting your code into the animal's (fox's) autonomous movement page and he wouldn't move for some reason).

    @caethyril Thanks! I tried it out and it works really well. Although when it comes to changing the movement speed with the code, I am a little confused as to which number symbolizes frequency and which number symbolizes speed.
    There is one other thing I am needing with your code. When using your movement, the animal (fox) is a little too clever and makes it impossible (I think) to get far enough away from him so that he stops chasing the player. Is it possible to make it that he walks five spaces towards the player, then one random space, then five spaces towards the player, and so on? In my previous animal (fox), I made him do that. Thanks a lot. :)
     
    #6
  7. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,178
    Likes Received:
    723
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    Frequency determines how often an event moves. Frequency 5 (Highest) means there is no delay between movements, lower frequencies put a delay between each step: the lower the frequency, the longer the delay. For this I'd recommend a frequency of 5, at least when they're chasing you. =)

    If you'd also like the frequency to change when chasing/not chasing, putting this in another Script command seems to do it:
    Code:
    this.setMoveFrequency($gameMap.distance(this.x, this.y, $gamePlayer.x, $gamePlayer.y) < 8 ? 5 : 4);
    It works much the same as the move speed one: if the event is less than 8 tiles away from the player, move frequency gets set to 5, else it gets set to 4.
    Oops, yes, good point! Poor doomed bunny. :kaocry:

    Before I go on, I'll just note that I've realised I'm basically reinventing Yanfly's Event Chase Player plugin with script calls here...you may want to give that a look instead at this point, but I'll carry on regardless. :kaoswt2:

    I'm not sure about how to make it happen every 5 steps, but giving the event a random chance to not chase every move is possible. Here's an example where the event will chase correctly 80% of the time and move randomly otherwise:
    Code:
    if (Math.randomInt(100) < 80 && $gameMap.distance(this.x, this.y, $gamePlayer.x, $gamePlayer.y) < 8) { this.moveStraight(this.findDirectionTo($gamePlayer.x, $gamePlayer.y)); } else { this.moveRandom(); }
    The only difference here is the Math.randomInt(100) < 80 bit, which means "pick a random integer from 0~99 and check if it's less than 80", i.e. return true 80% of the time.

    I noticed there's a 1-frame delay if using multiple Script commands in one move route, so I decided to try putting them all in one command (in script, semi-colons act as line breaks) and it seemed to work well! :kaopride: Here's my test example, works if pasted into a single Script command in an event's auto move route:
    Code:
    var chaseDist = 8, baseSpeed = 3, chaseSpeed = 5, baseFreq = 4, chaseFreq = 5, wanderChance = 20; var inRange = $gameMap.distance(this.x, this.y, $gamePlayer.x, $gamePlayer.y) < chaseDist; if (inRange) { this.setMoveSpeed(chaseSpeed); this.setMoveFrequency(chaseFreq); } else { this.setMoveSpeed(baseSpeed); this.setMoveFrequency(baseFreq); }; if (inRange && Math.randomInt(100) < (100 - wanderChance)) { this.moveStraight(this.findDirectionTo($gamePlayer.x, $gamePlayer.y)); } else { this.moveRandom(); }
    Yep, that's a lot of code! :kaoback: However, notice there's a collection of values defined at the start...that's so you don't have to dig through the actual code hunting them down, you can just change the starting values and everything should adjust correctly. :kaojoy: Here's what each one should do:
    • chaseDist: limits how many tiles away the event should start chasing from;
    • baseSpeed: movement speed when not chasing the player (player walk speed = 4, dash speed = 5);
    • chaseSpeed: movement speed while chasing;
    • baseFreq: movement frequency when not chasing the player;
    • chaseFreq: movement frequency when chasing;
    • wanderChance: % chance of random movement while in range for chasing;
    Feel free to make any edits you like to the script! :kaothx:

    (...incidentally, I think I'm going to use this, or something similar, in the game I'm making; it looks neat! ^_^)
     
    #7
  8. EmmaB

    EmmaB Veteran Veteran

    Messages:
    38
    Likes Received:
    56
    First Language:
    English
    Primarily Uses:
    RMMV
    @caethyril Thanks for that. I'll check it out when I'm able to and let you know how it turned out. :)

    edit: @caethyril I tried it out and it worked well. :D Thank-you so much. (Who do I notify to close the topic?).
     
    Last edited: May 25, 2018
    #8
    caethyril likes this.
  9. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,178
    Likes Received:
    723
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    Happy to help! ^_^

    I think you just report your own opening post and a moderator should see it. =)
     
    #9

Share This Page