How to make new events by script?

I can use a pre-defined event (I can see here), using:
>> $gameMap.event(EventID, EventPage).start();

I can re-use event using, but it is not working:
>> var event = $gameMap.event(EventID, EventPage)
>> event.start();



However, I could not re-use it after changing map or returning for it. Other problem occurring when I using, but it is not working:
>> var event = new Game_Event(MapID, EventID)
>> event.start()


I saw which variables are controlling:
._starting
._locked



If anyone has any idea...
I can use a pre-defined event (I can see here), using:
>> $gameMap.event(EventID, EventPage).start();

I can re-use event using, but it is not working:
>> var event = $gameMap.event(EventID, EventPage)
>> event.start();



However, I could not re-use it after changing map or returning for it. Other problem occurring when I using, but it is not working:
>> var event = new Game_Event(MapID, EventID)
>> event.start()


I saw which variables are controlling:
._starting
._locked



If anyone has any idea...
i think it because $gameMap is only storing ONE map(current one) only...

and everytime you change map... it will 'remove' the current data and 'load' next map data from editor...

so when you're adding your custom event in a map... when changing map it will be 'erased'... and when back to that map again it will load the 'default' map data which your custom event did not exist...

to solve this you need plugins that can save the map data to something (some easy alternative is $gameSystem because it saved in savefile).... then when loading map... do some check... if there's saved mapdata... use that instead loading from editor... if not... load from editor.

hope this help...
I'd suggest a slightly different approach:
1. Whenever you create a custom event, save its json contents(including the list of id of map having that event) in $gameSystem
2. Add those json contents to $dataMap having the map wanting to use that event
3. You can ensure the uniqueness of the events by their id and/or names, so you can avoid adding duplicate events and/or overwriting existing events(no 2 different events among any map should have the same id and name)
4. Upon loading the save file, load all event json contents in $gameSystem into $dataMap if the map it's storing should have those events
That way, you don't have to store the whole map into save files, which prevents the save file from being too big(> 50000 bytes)
I'd suggest a slightly different approach:
1. Whenever you create a custom event, save its json contents(including the list of id of map having that event) in $gameSystem
2. Add those json contents to $dataMap having the map wanting to use that event
3. You can ensure the uniqueness of the events by their id and/or names, so you can avoid adding duplicate events and/or overwriting existing events(no 2 different events among any map should have the same id and name)
4. Upon loading the save file, load all event json contents in $gameSystem into $dataMap if the map it's storing should have those events
That way, you don't have to store the whole map into save files, which prevents the save file from being too big(> 50000 bytes)
yeah i got that big save file too big problem in my MV plugins that save map. (2mb and it's only because i limit the map that can create custom event)...
maybe when i convert it to MZ later i might change to that concept (only saving extra event). but the downside is we cannot Delete the real existing event... cause it will be reloaded when reenter map... maybe need some extra object container that store deleted 'real' map event...

so the concept will be when entering a map...
1. load real map event of that map
2. delete real map event that included in deleted event object container.
3. merge with custom event of that map

then when deleting event also check do the deleted event is custom event or not. if yes delete the object that store custom event directly. if not add the deleted event to the deleted event object container...

Original thread: https://forums.rpgmakerweb.com/threads/how-to-make-new-events-by-script.128425/