Save the state of map events.

This is a very small piece of code I've written for saving the state of map events. I mean, when you go in and out of a map, all the events are loaded from the data stored in your project...
If you want to override this mechanism and mantain for example the position reached by a moving event or its graphic, or whatever would be reset when the map is loaded again from scratch, you can go mad with local switches and their possibilities are somewhat limited... (breathes).
So I have thought to this solution.

Demo

http://sites.google.com/site/charliefleed/EventStates.zip

Here some simple state variables such as the position and the graphic can be stored, loaded from and cleared in a simple database which uses map_id and event_id as the key. When a map is created the state of all events is re-established if it has been previously stored.

I hope this can be useful to someone.
I welcome suggestions, bug reports, and ideas for its applications.
Bye.
Like, when I play my game, and save right after picking up an item, when i restart the game, the item appears again.

Will this script prevent that?
You can solve your problem by using a switch and make a second page in that even with the event on the upper left on.

Leave the rest blank.

charlie.lee nice script very useful.
You, are a lifesaver! You will be going near the top of the credits in my game! I do hate to be dumb and ask how to use it though. I inserted it but I assume that it's not automatically done. I'm new to RGSS but I'm learning.
Instructions are in the header. Basically you have commands to load, save and clear event state. When you save a state, it will be loaded automatically by entering in the map.
Tell me what you would like to do so that i can be more specific.
Ok. In the teleporting event leaving that map that I want to save, I use a script with  $game_map.events[@event_id].record_state correct? Then upon reentry it will be loaded automatically?

Edit: I had to replace @event_id with the event ID. Is there a way that I can make something so it remembers every single event place of every event by having it memorize all of them no matter how many there are, it will just memorize them all. I have over 300+ events on this map. lol

Edit 2: Still confused even replacing the event ID. There was another error. What else do I change? Sorry.
Try this:
for id in 0...$game_map.events.size
  $game_map.events[id].record_state unless $game_map.events[id]==nil
end
Expand to see the code.


if you have to break the second line, do it after a "."
Man. I am really RGSS stupid. I have no idea what to replace with what.... I am hopeless.
kdb424 wrote:
Man. I am really RGSS stupid. I have no idea what to replace with what.... I am hopeless.


Ah, ah. "RGSS stupid" is great. :D
You have an event that teleports the hero to another map, right?
Ok. In that event, before the teleport command, insert a script command (it's in the third page) and put the code i wrote in my previous post in it.
That's it.
Just a sidenote, kdb424 please check the date of the last post before replying. If it was posted more than a month ago, it's considered necroposting. Next time just PM the OP or create a new thread in a support forum.

It's okay here since the OP is still active, but generally you should avoid this.
Oh, please.
He did ask for a script like this in the request board, and I personally told him there was this script of mine here.
So he didn't do anything wrong.

Regi, please check the latest posts of the person you're going to bug with your "you! necroposterrr..."...
( just a joke ;) )
Heh Charlie that's okay then, but he's been necroposting a bit and it's one of those things that kind of bug me :o

More on topic, now that I'm looking at your script it looks kind of useful.
Reminds me of dubealex's old Memorize Location script, but a lot cleaner and easier to use ;)
I tried to put that code in a script in the event. It gave me a syntax error. I don't know where to go from there.
Is this compatible with Poccil's Pokemon Starter Kit? For when I save after doing an important event that walks next to you, it is reset when I start up that save file totally messing up gameplay.

If it is compatible then you my friend are a lifesaver.

Thanks!
-Krobe
Charlie Lee wrote:
Note that the state is not saved in your savefile.


Just so you know. You can use some variables and use RGSS on load to call a game switch to move the event back there like I did.
@kdb424
I told you to break the long lines after a ".". That's your problem.
Ok, I'll do it for you.

for id in 0...$game_map.events.size
  $game_map.events[id].
  record_state unless $game_map.
  events[id]==nil
end
Expand to see the code.



@Krobelus:
I think it can be easily modified so that states are saved in the savefile... expect some update.

What about this?
#==============================================================================
#==============================================================================
# â €“   Support for events' state (by Charlie Lee)
#  use with: $game_map.events[@event_id].record_state
#  $game_map.events[@event_id].load_state
#  $game_map.events[@event_id].clear_state
#  from inside the events through command "Script"
# 
#------------------------------------------------------------------------------

class Game_Event < Game_Character
  attr_reader   :event
 
  def record_state
    key = [@map_id, @event.id]
    $game_system.game_event_records[key]=[x, y, @character_name]
  end

  def load_state
    key = [@map_id, @event.id]
    if $game_system.game_event_records[key] != nil
      x=$game_system.game_event_records[key][0]
      y=$game_system.game_event_records[key][1]
      moveto(x,y)
      @character_name=$game_system.game_event_records[key][2]
    end
  end

  def clear_state
    key = [@map_id, @event.id]
    if $game_system.game_event_records[key] != nil
      $game_system.game_event_records[key] = nil
    end
  end
end


class Game_Map
  alias setup_record setup
  def setup(map_id)
    setup_record (map_id)
    for i in @map.events.keys
      @events[i].load_state
    end
  end
end


class Game_System
  attr_accessor :game_event_records
  alias ge_initialize initialize
  def initialize
    ge_initialize
    @game_event_records={}
  end
end

Expand to see the code.
Well I'll try it out. I got a 2 day ban, but I'm glad that this is making a comeback. It is truly amazing.
Hi i was just wondering if this script could possibly be used to solve my very annoying problem..... my game includes an ABS but my problem is i dont want the enemys hp bar to reset when you leave a map and also their position e.g. attack an enemy leaving at it with 10hp, exit the map then re-enter and the enemy is in the exact same loc with 10hp still....
Yes, it is likely that this script can do it. I need to know where are the enemies' HPs stored in this ABS.
I cant get it right, can somebody help?
I need EV015 saved and loaded but i dont know what i should do....
I read all posts here and i dont know what to do, can someone make a demo that works?
Has anyone figured out how to use this script properly?
And why does:

line 12: attr_reader :event

create an error?

This is a really helpful script, but can someone please make these minor clarifications for all?
Also:


$game_map.events[@event_id].record_state

does not fit in one line, when pasted in the "event script command" is that a problem for the script?
Do I input the event [id] e.g. "$game_map.events[@event_010].record_state" to record the state of only that event, or does this script record the state of all events in a specific map. Or does it record the event state of all events in all maps?
Demo uploaded. The text version of this script had some characters messed up.

Also,

$game_map.events[@event_id].record_state

has to be broken like this:

$game_map.events[@event_id].
record_state

The script records the state of the event in which you call $game_map.events[@event_id].record_state at the moment you call it. See how it is used in the demo.
@event_id is a variable that stores the id of the event on which the script command is executed, you are not supposed to change it into something like @event_134. However you can make a call like

$game_map.events[3].record_state

if you want to save the state of event number 3 from another event.
Useful script, but I'm not sure if I will need it or not.. If I ever make a really complicated RPG (a puzzle one, most likely) I'll use it. It's very nice!!

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=28116