Spawning event with a parallel process/move route

Hello everyone! I wanted to add to my game some enemies who can shoot projectiles. To do this I have created a database map with all the different kind of bullets stored as events, then I copy them to the current map with this method:

Spoiler
class Game_Mapdef add_event(mapid, eventid, x, y) size = $game_map.events.lenght map = load_data(sprintf("Data/Map%03d.rvdata2", mapid)) map.events.each do |i, event| if event.id == eventid event.id = size + 1 e = Game_Event.new(@map_id, event) e.moveto(x,y) @events[e.id] = e SceneManager.scene.get_spriteset.refresh_characters #get_spriteset returns Scene_Map @spriteset return e.id end endendend
The main problem is that I can't call it within a parallel process, and not even during a move route, because it will just throw "Can't add a new key into hash during iteration", the iteration in question should be the "update_events" method inside the Game_Map class, so I tried overwriting it this way:

Spoiler
#-------------------------------------------------------------------------- # * Update Events #-------------------------------------------------------------------------- def update_events #============== if @events_2 != nil @events = @events_2.clone @events_2 = nil end #============== @events.each_value {|event| event.update } @common_events.each {|event| event.update } end
I then modified the add_event() method by adding  "@events_2 = @events.clone" and changing "@events[e.id] = e" to "@events_2[e.id] = e", this way it shouldn't touch the @events hash while it is iterating.

But it doesn't work properly, all of the spawned events will have a great delay after each action, and the game freezes after a while, what am I doing wrong?

I also use this to destroy spawned events:

Spoiler
class Game_Interpreterdef kill_event(event_id)map = $game_mapmap_id = map.map_id["A","B","C","D"].each{|letter|$game_self_switches[[map_id, event_id,letter]] = false}map.events[event_id].erasemap.events[event_id].moveto(-1,-1)map.events.delete(event_id)map.need_refresh = trueendend
Any help is greatly appreciated  🙂
why do you want to spawn/clone the events from inside a parallel process?


Creating a projectile should be triggered, not on parallel...
I use the parallel process to check if the conditions to shoot are met, if the player is within a certain range, or if the event has to wait and recharge, I also wanted to use it in custom move routes, but it gives me the same problems as the parallel event. If I add them directly to @events I receive "Can't add a new key into hash during iteration", if I put them in another hash and then add it to @events strange things happen. 
Hello! Paste the following code above all custom scripts and below Game_Map:

class Game_Map def update_events @events.values.each { |event| event.update } @common_events.each { |event| event.update } endendThe error you mentioned says that you can't add a new key to a hash during its iteration, so... the above code changes it so the hash isn't iterated. @events.values returns an array with the events (the values from the @events hash), and that is what is iterated instead of the hash (the update_events method originally uses each_value, an iterator working on the hash).

Let me know if you have any questions. 🙂
^^^ It works, Source helped me with this problem earlier today, I've been looking for a workaround for a few weeks now.
Thank you for your help Source, I've been trying some other way to fix this and ended up with this (I don't know why I didn't think about this earlier :|  ) : 

#-------------------------------------------------------------------------- # * Update Events #-------------------------------------------------------------------------- def update_events a = @events.clone a.each_value {|event| event.update } @common_events.each {|event| event.update } endBut your method is quite good too, I'll do some tests with both of them to see which one affects performance the least, thanks for the answer!  🙂

Original thread: https://forums.rpgmakerweb.com/threads/spawning-event-with-a-parallel-process-move-route.40185/