Create new event by script

I managed to create a new event (I hope I did it properly?):

class Game_Interpreter  def create_ev(x, y)    ev = RPG::Event.new(x, y)        game_ev = Game_Event.new($game_map.map_id, ev)    $game_map.events[ev.id] = game_ev    game_ev.refresh    $game_map.refresh_tile_events # is this line required at all?        ev_page = game_ev.find_proper_page # performing a puts-command here returns a valid page    ev_page.graphic = RPG::Event:😛age::Graphic.new    ev_page.graphic.character_name = 'Actor1'    ev_page.graphic.character_index = 5    ev_page.graphic.direction = 2 # isn't 2 default?    return game_ev  endend- No error but my event is not visible.

- I suppose that loading/saving the game will not store this new event automatically?

Also tried (same problem):

Spoiler
Code:
class Game_Interpreter  def create_ev(x, y)    ev = RPG::Event.new(x, y)        game_ev = Game_Event.new($game_map.map_id, ev)    $game_map.events[ev.id] = game_ev    game_ev.set_graphic('Actor1', 5)        game_ev.refresh    $game_map.refresh_tile_events # is this line required at all?    return game_ev  endend
- No error but my event is not visible.
(1) You didn't create a sprite for it and add it to the map spriteset

I suppose that loading/saving the game will not store this new event automatically?
Correct.

$game_map.refresh_tile_events # is this line required at all?
It doesn't matter because of (1)
Thanks.

Some ugly code but it works:

Spoiler
Code:
class Game_Interpreter  def create_ev(x, y)            ev = RPG::Event.new(x, y)    game_ev = Game_Event.new($game_map.map_id, ev)    $game_map.events[ev.id] = game_ev        game_ev.set_graphic("Actor#{Random.rand(4) + 1}", 5)    game_ev.refresh     SceneManager.scene.spriteset.character_sprites.push(Sprite_Character.new(SceneManager.scene.spriteset.viewport1, game_ev))    return game_ev  endendclass Scene_Map < Scene_Base  attr_accessor :spritesetendclass Spriteset_Map  attr_accessor :character_sprites  attr_reader   :viewport1end
Test that your game doesn't crash when you F12.
It doesn't crash with [F12]. But the events are automatically saved&loaded. Because the entire $game_map is stored/loaded in the DataManager the newly created events (by code) are also stored and loaded.

But when I open the menu and close it again, only the last placed event is still on the map.

Spoiler
class Game_Interpreter def create_ev(x, y) ev = RPG::Event.new(x, y) game_ev = Game_Event.new($game_map.map_id, ev) $game_map.events[ev.id] = game_ev game_ev.step_anime = true game_ev.set_graphic("Actor#{Random.rand(4) + 1}", 5)#~ game_ev.refresh SceneManager.scene.spriteset.character_sprites.push(Sprite_Character.new(SceneManager.scene.spriteset.viewport1, game_ev)) return game_ev endendclass Scene_Map < Scene_Base attr_accessor :spritesetendclass Spriteset_Map attr_accessor :character_sprites attr_reader :viewport1endclass Game_CharacterBase attr_accessor :step_animeend
ev.id and game_ev.id are always 0... So the new events will overwrite the previous one. This code is still not good. I can't locate the code that initializes the event_id.
It doesn't. You need to set it yourself.

I use this for simplicity

Code:
@events.empty? 1 : @events.keys.max + 1
But you can do a smart search for any unused ID's if needed.
Ah that explains 🙂 .

I suppose that the regular events have their id set in the editor (closed source code). So I can't see it.

The default code obviously was never intended to create events by scripting. But it seems to work now. I might want to pre-create the events on some other map though and then just clone them to the current map instead. That might be a better solution.
That would work for static events that can be cloned.


For more dynamic events like the ones that can be created using the Event Wrapper, cloning wouldn't work too well. Imagine building an event that involves looping over your party.

Original thread: https://forums.rpgmakerweb.com/threads/create-new-event-by-script.22795/