Remember Event Position

Discussion in 'RGSS3 Scripts (RMVX Ace)' started by Shaz, May 8, 2012.

  1. Shaz

    Shaz Veteran

    Messages:
    34,307
    Likes Received:
    8,942
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV

    Remember Event Position





    Author: Shaz


    Introduction

    If you move an event on the map, then leave the map and return, the event, by default, goes back to its original place (where you put it when designing the map). To get around that, you have to have another parallel process event run every time the map loads, to see if the event has been moved, and if so, put it in its correct position. If you have a lot of these in your game, that's a lot of additional events just checking where events should be. I dislike lots of "setup" events sitting around on maps, so devised this little script to let you tell an event to save its position, and go back to that position next time the map is loaded.

    How to Use

    Paste this script into its own slot in the Materials section.

    Script

    Code:
    #--------------------------------------------------
    # Remember Event Position 1.0
    #--------------------------------------------------
    #
    # Author: Shaz
    #
    # Description: This script lets you set the position and direction
    #              where an event will appear next time the map is
    #              loaded.  Events will appear in this new position
    #              rather than the place they were put during design.
    #
    #--------------------------------------------------
    #
    # Installation: 
    #
    # Copy into a new script slot in the Materials section
    #
    #--------------------------------------------------
    #
    # Use:
    #
    # Examples below show syntax for an Event Command on an Event Tab.
    #   You can use @event_id for the current event, or specify an event number.
    # To execute from within a Set Move Route command, omit the
    #   $game_map.events[@event_id]. part, and just use save_pos() and forget_pos().
    #
    # To tell an event to move to its current position the next time
    # the map is loaded, execute the following in a script call:
    #   $game_map.events[@event_id].save_pos()
    #
    # To tell an event to move to a particular position (not its
    # current position) the next time the map is loaded,
    # execute the following in a script call:
    #   $game_map.events[@event_id].save_pos(1, 2, 8)
    # - the above will move the event to x=1, y=2, facing up
    #
    # To tell an event to forget its previously remembered position
    # and go back to its default position the next time time map
    # is loaded, execute the following in a script call:
    #   $game_map.events[@event_id].forget_pos()
    #--------------------------------------------------
    
    class Game_System
      alias shaz_mem_position_gs_init initialize
    
      attr_accessor :event_positions
    
      def initialize
        shaz_mem_position_gs_init
        @event_positions = {}
      end
    end
    
    # Check to ensure $game_system.event_positions exists (save files created
    # prior to this script being added would not have it initialized)
    class Game_Map
      alias shaz_mem_positions_gm_init initialize
    
      def initialize
        shaz_mem_positions_gm_init
        $game_system.event_positions = {} if !$game_system.event_positions
      end
    end
    
    class Game_CharacterBase
      attr_accessor
    end
    
    class Game_Event
      alias shaz_mem_position_gcb_init initialize
    
      def initialize(map_id, event)
        shaz_mem_position_gcb_init(map_id, event)
    
        if $game_system.event_positions.has_key?([map_id, @id])
          mvx, mvy, mvdir = $game_system.event_positions[[map_id, @id]]
          moveto(mvx, mvy)
          set_direction(mvdir) if mvdir
          @stop_count = 0
          refresh
        end
      end
    
      def save_pos(x = @x, y = @y, dir = @direction)
        $game_system.event_positions[[@map_id, @id]] = [x, y, dir]
      end
    
      def forget_pos
        $game_system.event_positions.delete([@map_id, @id])
      end
    end
    
    How to Call

    To save the current event's position after a move route:



    Code:
    Set Move Route: This event (Wait)
                  : $>Move away from Player
                  : $>Script: save_pos()
    
    To save the current event's position, but not within a move route:

    (Conditional branch only used as part of the example - only the last line is relevant)



    Code:
    Conditional Branch: Switch [0001: Move Away] == ON
      Set Move Route: This event (Wait)
                    : $>Move away from Player
    Else
      Set Move Route: This event (Wait)
                    : $>Move toward Player
    Branch End
    Script: $game_map.events[@event_id].save_pos()
    
    To make a different event move to a set position when the map is next loaded:



    Code:
    Script: $game_map.events[4].save_pos(15, 6)
    
    FAQ

    Conditions

    May be used freely in any game, commercial or otherwise. Please give credit.

    Please do not copy and post to other forums.

    Credits

    Shaz
     
    Last edited by a moderator: Jan 1, 2013
    #1
  2. PK8

    PK8 I sense there's something in the wind...

    Messages:
    1,220
    Likes Received:
    151
    Location:
    Spiral Hill
    Your code appears messed up. o.o
     
    Last edited by a moderator: May 8, 2012
    #2
  3. Todd

    Todd Game Developer

    Messages:
    269
    Likes Received:
    38
    First Language:
    English
    Primarily Uses:
    N/A
    This looks nice, I can think of some pretty cool reasons to use this. But ya your code got messed up trying to set the alignment.
     
    #3
  4. Shaz

    Shaz Veteran

    Messages:
    34,307
    Likes Received:
    8,942
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    Thanks guys. I hate the way this forum software messes things up after you preview. I'll see if I can fix it.

    ... and fixed :)
     
    Last edited by a moderator: May 8, 2012
    #4
    ♥SOURCE♥ likes this.
  5. Abi

    Abi Veteran

    Messages:
    191
    Likes Received:
    10
    Location:
    NW Ohio
    First Language:
    English
    I can see a lot of use for this... there's just one thing you didn't explain though... maybe you didn't even include a facility to do this sort of thing... But what if I wanted to check if the map had saved positions and prompt the player if they want to reset it, say, a single move puzzle where you have multiple exits and you can only solve the puzzle for one exit at a time. The Star Ocean series had a lot of these sorts of branching puzzles and I loved them.
     
    #5
  6. Shaz

    Shaz Veteran

    Messages:
    34,307
    Likes Received:
    8,942
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    No, I didn't. I don't know that I'd want to prompt the player too may times when a map is loaded - that would get annoying. But if it was only for one puzzle room or something, perhaps ...

    You could try something like this (untested, so may not work at all) in a script call from an event:



    Code:
    found = false
    $game_system.event_positions.keys.each { |map, id|
     found = true if map == $game_map.map_id
    }
    
    to determine if events on this map have positions remembered (instead of using a temporary variable 'found', you could use a $game_switch, so you can then use it in a conditional branch)



    Code:
    $game_system.event_positions.keys.each { |map, id|
     $game_system.event_positions.delete([map, id]) if map == $game_map.map_id
    }
    
    to forget all positions on this map

    The problem is that by the time this runs, the map has already loaded, and the above will not move the events back to their original spot. So you would either need to call this when exiting the map rather than entering, or call it from the map that teleports you here, and instead of using $game_map.map_id, hardcode the id of the map you're about to teleport to.

    Again, I haven't tested this so I don't know if it actually works.
     
    #6
  7. Abi

    Abi Veteran

    Messages:
    191
    Likes Received:
    10
    Location:
    NW Ohio
    First Language:
    English
    Next weekend if someone else doesn't try it out before then I'll try your suggestion out in a little test demo and let you know if it works. I only have today off this weekend, so getting to it before then is unlikely at best.
     
    #7
  8. Jonnywithoutah

    Jonnywithoutah Warper

    Messages:
    1
    Likes Received:
    0
    First Language:
    English
    So, would this allow me to remember the player's position?

    The scenario I'm after is to have a "Campsite" map, which the player would be warped to after using an item. The idea is that the player would do whatever on the Campfire map, and then be able to leave and return to wherever it was they used the item in the first place, however I'm currently unable to figure this out. Would this script allow me to make that dream a reality?

    Much obliged in advance.
     
    #8
  9. Shaz

    Shaz Veteran

    Messages:
    34,307
    Likes Received:
    8,942
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    No. This is for events which belong to maps.

    To use an item to warp the player to a specific place, you'd need to use variables to track the map number, x and y position - the transfer player command will let you send the player to a map and position identified by those variables.
     
    #9
  10. EternalShadow

    EternalShadow Veteran

    Messages:
    5,750
    Likes Received:
    1,046
    First Language:
    English
    I need to use this! Thanks, will let you know how it works out. :D
     
    #10
  11. ct_bolt

    ct_bolt Creator

    Messages:
    438
    Likes Received:
    69
    Location:
    Planet Earth
    First Language:
    Javascript
    Nice script!  :)   I was just wondering how it compared to a little script I wrote a bit back... I imagine it's probably more useful than the one I wrote... care to explain the differences?
    My script: Events on Map 1.02
     
    Last edited by a moderator: Jan 31, 2013
    #11
  12. Nicke

    Nicke Pure Evil

    Messages:
    490
    Likes Received:
    331
    Location:
    Sweden
    First Language:
    Swedish
    Primarily Uses:
    RMMV
    @ct_bolt: Why must someone explain the difference when you can try it out yourself and see them? :p
     
    #12
    ct_bolt and ♥SOURCE♥ like this.
  13. Tsukihime

    Tsukihime Veteran

    Messages:
    8,220
    Likes Received:
    2,995
    Location:
    Toronto
    First Language:
    English
    At first glance, yours would not load the event positions upon game loading.


    But if I am wrong...then I am missing something important about RM save files.


    Yours also does not provide a convenience method for forgetting stored positions, which of course is not a problem if users can read code.


    But other than that it is the same concept.
     
    Last edited by a moderator: Feb 1, 2013
    #13
    ct_bolt likes this.
  14. ct_bolt

    ct_bolt Creator

    Messages:
    438
    Likes Received:
    69
    Location:
    Planet Earth
    First Language:
    Javascript
    Because I asked nicely... lol, I did try it just wasn't sure what all was different. B) It is much nicer then the script I had wrote.

    Thank you for such a nice reply!  :D   That is basically what I've noticed too. :thumbsup-left:
     
    #14
  15. Shaz

    Shaz Veteran

    Messages:
    34,307
    Likes Received:
    8,942
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    Here are a few differences I noticed:


    - yours requires a script call to set up the events in the correct locations. Mine does it automatically.


    - yours looks at ALL events, and if the map is the current one, it moves the event. Mine looks at ONLY the events for the current map and moves them (yours would take longer to run as it's looking at events that will not be processed).


    - as already mentioned, yours does not provide a way to 'forget' the location.


    - again, as already mentioned, yours does not stick when the player saves and closes the game.


    - yours saves only the coordinates of the event - mine also saves the facing direction.


    My script is not necessarily better than yours, if you only have a need to do the things in your script. Note, when I started mine, I JUST stored the event location too. Storing the direction and forgetting the position were additions that came as I used it more and decided it needed those things as well.
     
    #15
    ct_bolt likes this.
  16. ct_bolt

    ct_bolt Creator

    Messages:
    438
    Likes Received:
    69
    Location:
    Planet Earth
    First Language:
    Javascript
    Wow! Thank you so much for that info. :D I will most likely be using yours instead for now at least.

    I may try and implement a few more things in mine and change it a bit to at least save the positions when the player closes the game though. B)
     
    #16
  17. light487

    light487 Parrot Keeper

    Messages:
    852
    Likes Received:
    26
    Location:
    Sydney, Australia
    First Language:
    English
    Just saw this linked from another thread and found the script might be useful. I wonder if there is a way to make this work with "Random" event movement or is it only possible when being used as part of a move_route command?
     
    #17
  18. Shaz

    Shaz Veteran

    Messages:
    34,307
    Likes Received:
    8,942
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    Under Autonomous Movement, choose Custom. Then enter two movement commands - one will be Move at Random, the next will be save_pos
     
    #18
    light487 likes this.
  19. light487

    light487 Parrot Keeper

    Messages:
    852
    Likes Received:
    26
    Location:
    Sydney, Australia
    First Language:
    English
    Oh right.. I didn't realise there was a random move command in there.. there's so many commands in that dialog, I guess I missed it. :)
     
    #19
  20. chigoo

    chigoo Void Walker

    Messages:
    123
    Likes Received:
    9
    Location:
    Union, NJ
    First Language:
    English
    Primarily Uses:
    N/A
    Is there a way to make the script call use zero if you want the script to tell the event it's in to remember position.

    I have over 100 events I want to remember their position, and more to be added on 1 map. so I want an easier way to do it so I can copy and paste, most of the events are identical. 
     
    #20

Share This Page