Event Creation in JS

Hello, everybody-

I am working on a script where I am trying to create a new Event during gameplay:

Code:
$._bago = function() {

        var template_id = 0; // default- blank event template
        if (arguments.length != 0) template_id = Number(arguments[0]);
   
        // get event template from json and (1) load it as an object, (2) create blank page object.
        var json_fn = "EventTemplates.json";
        var data_folder = $._root() + "/data/";
        var template_json_fn = data_folder + json_fn;
   
        var json_data = $._load_text_file(template_json_fn);
   
        var x_obj = JSON.parse(json_data);
        var y_obj = JSON.parse(json_data);
   
        var json_obj = x_obj.events[template_id];
        var json_page = y_obj.events[template_id].pages[0];
   
        // new event settings

        var events_on_map = $dataMap.events.length;

        json_obj.id = events_on_map - 1;
        json_obj.x = 3;
        json_obj.y = 4;

        json_obj.pages[0].conditions.selfSwitchCh = "D";
        json_obj.pages[0].conditions.selfSwitchValid = true;    
        json_obj.pages[0].image.characterName = "!coin";
        json_obj.pages[0].image.pattern = 1;
        json_obj.pages[0].priorityType = 1;

        $gameVariables.setValue(2,"kaganap.an._pasaya(2,\x22d,l,r,u,b:0,d:1,d,u,r,l,d:1,h:12,d,u,r,l,d:1,d,l,r,u,d:1,d,i:!coin*1,d,l,r,u,i:!coin*2,d,l,r,u,i:!coin*0,b:1,v:e2*1,d:1\x22);");
        var move_commands_x = "b:0,z:2";
        var m_c_i = $._isalin(move_commands_x);

        json_obj.pages[0].moveRoute = m_c_i;

        // add blank page
        json_obj.pages.unshift(json_page);


        // create event on map
        $dataMap.events[events_on_map] = json_obj;
        $gameMap._events[events_on_map] = new Game_Event($gameMap._mapId, events_on_map);
   
        SceneManager._scene.children[0]._tilemap.addChild(new Sprite_Character($gameMap._events[events_on_map]));
   
        // turn on new event's self switch d
        var xkey = [$gameMap._mapId, json_obj.id, "D"];
        $gameSelfSwitches.setValue(xkey,true);

        $gameMap.refresh();

    };

This isn't the final function, just something I threw together to test the process.

I am able to get the object to appear on the map, however, I am not able to get it to react to the final commands in the script which turn on its self switch D so that I can get the Custom route on its second page to execute.

Any insights on what I am doing wrong / hints about what I should be doing instead?

Thank you in advance for your help!
event creation by script is possible, but generally considered a bad idea. There are too many details to be set or considered for this to be easy.

what most people do instead is to clone existing events - you create an event in the editor on a map that will never be used in the game, and then copy the event from there, only changing what is absolutely needed to be changed by script.

search around the forum, there are two or three different clone/copy plugins available and use them or their code as examples.
event creation by script is possible, but generally considered a bad idea. There are too many details to be set or considered for this to be easy.

what most people do instead is to clone existing events - you create an event in the editor on a map that will never be used in the game, and then copy the event from there, only changing what is absolutely needed to be changed by script.

search around the forum, there are two or three different clone/copy plugins available and use them or their code as examples.

@Andar - Thanks for the reply.

I appreciate the suggestion and I have seen the different clone / copy event plug-ins being offered, but I am trying to devise my own way to do it because I need to use it as part of a greater function.

I have not read the scripts of the available plug-ins, but this is in essence what is being done by the aforementioned script. Except instead of using a map, the events are stored in a JSON file that the script is reading and drawing "blank" events from (whose properties are then modified).

I have no problem creating the events- they pop up on the map after I call the script. The problem is that these events don't seem to respond to switches or execute their custom move routes.'

I have scraped the internet and various RPG Maker forums / communities for answers but have hit a wall in my efforts.

I am hoping that somebody who has successfully achieved creating events dynamically will be able to tell me what I am missing to make this happen.

Thanks for trying to help, though.
I figured it out- never mind. 🙂
I figured it out- never mind. 🙂
How? Somebody might find this conversation in the future and want to know the answer.
How? Somebody might find this conversation in the future and want to know the answer.

@Aloe Guvner - the error was in this line,

Code:
json_obj.pages[0].moveRoute.list = m_c_i;

it should have just been:

Code:
json_obj.pages[0].moveRoute = m_c_i;

I figured it out by using a text comparison web site to look at an event generated by RPG Maker itself versus the events my functions were generating.


it works!

Original thread: https://forums.rpgmakerweb.com/threads/event-creation-in-js.109261/