Project1

标题: 如何用脚本或指令新增事件? [打印本页]

作者: xjh01    时间: 2017-5-2 16:54
标题: 如何用脚本或指令新增事件?
请教高手,如何通过脚本或指令来新增事件?我想通过这样的方式,来实现在地图上增加小动物。虽然了解了一些js的基础,但完全不知道该如何实现。下面是我在事件脚本里胡乱写的,没有任何效果。
JAVASCRIPT 代码复制
  1. var x_Event = new Game_Event(1,1);
  2. x_Event.setImage("Nature",2);
  3. x_Event.setDirection(1);
  4. x_Event.setPattern(1);
  5. x_Event.locate(3,2);
  6. x_Event.update();


请过路的高手、大神们指点一二。
作者: raketenfaust02    时间: 2017-5-2 17:10
你还不如在编辑器里先设置好这些事件,需要的时候用开关或者变量显示出来
作者: xjh01    时间: 2017-5-2 18:19
raketenfaust02 发表于 2017-5-2 17:10
你还不如在编辑器里先设置好这些事件,需要的时候用开关或者变量显示出来 ...

这样不行,我需要能随意增加小动物数量。
作者: shitake    时间: 2017-5-3 17:52
rm的事件系统极其复杂,一个办法是在一个特定地图里设置好预定的事件。
然后在需要的时候,读取那张地图然后把需要的事件复制过来。
要记得是得把事件放到地图事件数组里。像主楼里那样只是赋值给一个局部变量,并不会起作用。
作者: xjh01    时间: 2017-5-3 22:43
shitake 发表于 2017-5-3 17:52
rm的事件系统极其复杂,一个办法是在一个特定地图里设置好预定的事件。
然后在需要的时候,读取那张地图然 ...

非常感谢你的回复,如果能更详细解释一下就好了,或者有没有什么教程可以推荐给我学习的?
事件复制,我在某个群里得到了一位高手写的脚本,已经实现。不过,现在是自己弄明白如何编写脚本。JS的基础部分已经学完了,但是在RM里,完全看不懂,一头雾水。也不知道这些默认的构造函数,该如何具体的应用。{:2_253:}
作者: fux2    时间: 2017-5-3 23:38
你还需要把这个新建的事件加入到地图事件列表
  1. var last_id = $dataMap.events.length;
  2. var map_id = $gameMap.mapId;
  3. var spriteset = SceneManager._scene._spriteset;
  4. var eventData = {"id":1,"name":"EV020","note":"","pages":[{"conditions":{"actorId":1,"actorValid":false,"itemId":1,"itemValid":false,"selfSwitchCh":"A","selfSwitchValid":false,"switch1Id":1,"switch1Valid":false,"switch2Id":1,"switch2Valid":false,"variableId":1,"variableValid":false,"variableValue":0},"directionFix":false,"image":{"tileId":0,"characterName":"Nature","direction":2,"pattern":0,"characterIndex":0},"list":[{"code":0,"indent":0,"parameters":[]}],"moveFrequency":3,"moveRoute":{"list":[{"code":0,"parameters":[]}],"repeat":true,"skippable":false,"wait":false},"moveSpeed":3,"moveType":0,"priorityType":1,"stepAnime":false,"through":false,"trigger":0,"walkAnime":true}],"x":10,"y":10};
  5. eventData.id = last_id;eventData.x=3;eventData.y=2;
  6. $dataMap.events.push(eventData);
  7. var x_Event = new Game_Event(map_id,last_id);
  8. x_Event.update();
  9. $gameMap._events[last_id] = x_Event;
  10. var spr = new Sprite_Character(x_Event);
  11. spriteset._characterSprites.push(spr);
  12. spriteset._tilemap.addChild(spr);
复制代码

注意,这种访问方法非常不规范(而且在事件里塞这么大一坨也确实很丑),还是往各个类里添加相应功能的函数比较靠谱。
楼主的做法只是生成了一个临时事件,并没有加入地图
所以正确的做法应该是创造事件(并且创造原始数据)并添加,然后创造事件相应的精灵并添加。

作者: sutao80216    时间: 2017-5-4 00:29
这个插件应该是你需要的。
https://galvs-scripts.com/2016/05/14/mv-event-spawner/
JAVASCRIPT 代码复制下载
  1. //-----------------------------------------------------------------------------
  2. //  Galv's Event Spawner
  3. //-----------------------------------------------------------------------------
  4. //  For: RPGMAKER MV
  5. //  GALV_EventSpawner.js
  6. //-----------------------------------------------------------------------------
  7. //  2017-01-21 - Version 1.5 - bug fix clearing saved events on different map
  8. //  2016-11-08 - Version 1.4 - corrected false info in help file
  9. //  2016-07-25 - Version 1.3 - create event note meta when events spawn now
  10. //  2016-05-21 - Version 1.2 - added compatibility with tower defense plugin
  11. //  2016-05-15 - Version 1.1 - fixed a bug where events wouldn't spawn at y2
  12. //  2016-05-15 - Version 1.0 - release
  13. //-----------------------------------------------------------------------------
  14. // Terms can be found at:
  15. // galvs-scripts.com
  16. //-----------------------------------------------------------------------------
  17.  
  18. var Imported = Imported || {};
  19. Imported.Galv_EventSpawner = true;
  20.  
  21. var Galv = Galv || {};              // Galv's main object
  22. Galv.SPAWN = Galv.SPAWN || {};      // Galv's stuff
  23.  
  24. //-----------------------------------------------------------------------------
  25. /*:
  26.  * @plugindesc (v.1.5) Spawn events from a specified spawn map to a desired location.
  27.  *
  28.  * @author Galv - galvs-scripts.com
  29.  *
  30.  * @param Spawn Map Id
  31.  * @desc The map ID of the map you are using to store spawnable events
  32.  * @default 0
  33.  *
  34.  * @help
  35.  *   Galv's Event Spawner
  36.  * ----------------------------------------------------------------------------
  37.  * This plugin allows you to copy events from a specified 'spawn' map and
  38.  * duplicate them onto the current map. This spawn map is designated with the
  39.  * "Spawn Map Id" plugin setting.
  40.  * You can spawn to an x,y location or a random region ID.
  41.  *
  42.  * When spawning an event, it's important to know that if the location is
  43.  * blocked by impassible terrain, the player or any other event - it will not
  44.  * spawn unless you have previously changed Galv.SPAWN.overlap script call to
  45.  * one of the settings that allows it to overlap things.
  46.  *
  47.  * If you set the s variable to true in the script calls (below), that event
  48.  * will be 'saved' and exist until it is cleared. The event is saved where it
  49.  * is spawned and will not save it's x,y position if it moves and the player
  50.  * leaves the map (like normal events).
  51.  *
  52.  * Self switches work for all spawned events while they exist.
  53.  *
  54.  * ----------------------------------------------------------------------------
  55.  *  SCRIPT commands
  56.  * ----------------------------------------------------------------------------
  57.  *
  58.  *    Galv.SPAWN.event(id,x,y,s);       // Spawn event to x,y coords
  59.  *                                      // s = true to save event
  60.  *                                      // leave s out completely to not save
  61.  *
  62.  *    Galv.SPAWN.event(id,r,s);         // Spawn event to random region tile
  63.  *                                      // s = true to save event
  64.  *                                      // leave s out completely to not save
  65.  *
  66.  *    Galv.SPAWN.event(id,[r,r,r],s);   // Spawn event to a random region tile
  67.  *                                      // from the array list.
  68.  *                                       // s = true to save event
  69.  *                                      // leave s out completely to not save
  70.  *
  71.  *    Galv.SPAWN.overlap = type;        // can be one of the following:
  72.  *                                      // 'all' spawns over characters/terrain
  73.  *                                      // 'terrain' any terrain, no characters
  74.  *                                      // 'chars' any characters, no terrain
  75.  *                                      // 'none' spawns on empty tiles only
  76.  *                                      // events spawned after changing this
  77.  *                                      // will use the new overlap type
  78.  *
  79.  *
  80.  *    Galv.SPAWN.clear(mapId);         // Remove normal spawned events from map
  81.  *    Galv.SPAWN.clear(mapId,true);    // Remove ALL spawned events inc. saved
  82.  *                                     // Make MapID = 0 to clear current map
  83.  *
  84.  *    Galv.SPAWN.unspawn(this);        // Unspawns event code is executed in.
  85.  *
  86.  * ----------------------------------------------------------------------------
  87.  * EXAMPLES
  88.  * Galv.SPAWN.event(4,6,6);        // spawn event 4 at x6, y6, not saved
  89.  * Galv.SPAWN.event(1,12,true);    // spawn event 1 on random region 12, saved
  90.  * Galv.SPAWN.event(7,[2,3]);      // spawn event 7 on region 2 or 3, not saved
  91.  * Galv.SPAWN.overlap = 'chars';   // all events will spawn on top of other
  92.  *                                 // characters from now on.
  93.  * ----------------------------------------------------------------------------
  94.  */
  95.  
  96. //-----------------------------------------------------------------------------
  97. //  CODE STUFFS
  98. //-----------------------------------------------------------------------------
  99.  
  100. (function() {
  101.  
  102.  
  103. Galv.SPAWN.spawnMapId = Number(PluginManager.parameters('GALV_EventSpawner')["Spawn Map Id"]);
  104. Galv.SPAWN.overlap = 'none';
  105. Galv.SPAWN.sSwitches = ["A","B","C","D"];
  106.  
  107. Galv.SPAWN.scenes = ['Scene_Map'];
  108.  
  109. Galv.SPAWN.onScene = function() {
  110.         if (Galv.SPAWN.scenes.indexOf(SceneManager._scene.constructor.name) > -1) return true;
  111.         return false;
  112. };
  113.  
  114. Galv.SPAWN.event = function(eventId,x,y,save) {
  115.         if (!Galv.SPAWN.onScene()) return;
  116.  
  117.         if (y == undefined || y === true) {
  118.                 var save = y;
  119.                 // Spawn random region where x = array of region Id's
  120.                 var coords = Galv.SPAWN.randomRegion(x,y);
  121.                 if (coords) $gameMap.spawnEvent(eventId,coords[0],coords[1],save);
  122.         } else {
  123.                 // Spawn X,Y position
  124.                 if (Galv.SPAWN.canSpawnOn(x,y)) $gameMap.spawnEvent(eventId,x,y,save);
  125.         };
  126. };
  127.  
  128. Galv.SPAWN.randomRegion = function(regions) {
  129.         if (regions.constructor != Array) {
  130.                 var regions = [regions];
  131.         };
  132.         var possible = [];
  133.         var width = $gameMap.width();
  134.         var height = $gameMap.height();
  135.         for (var x = 0; x < width; x++) {
  136.                 for (var y = 0; y < height; y++) {
  137.                         if (Galv.SPAWN.canSpawnOn(x,y,regions)) possible.push([x,y]);
  138.                 };
  139.         };
  140.         // Randomize between possible spawning coordinates
  141.         return possible[Math.floor(Math.random() * possible.length)];
  142. };
  143.  
  144. Galv.SPAWN.canSpawnOn = function(x,y,regions) {
  145.         var region = $gameMap.regionId(x,y);
  146.         if (regions && !regions.contains(region)) return false;                              // Incorrect region
  147.         if (Galv.SPAWN.overlap != 'all') {
  148.                 if (Galv.SPAWN.overlap != 'chars') {
  149.                         if ($gameMap.eventsXy(x, y).length > 0) return false;                        // No spawning on other events
  150.                         if ($gamePlayer.x == x && $gamePlayer.y == y) return false;                  // No spawning on player
  151.                         if (Game_CharacterBase.prototype.isCollidedWithVehicles(x,y)) return false;  // No colliding with vehicles
  152.                 };
  153.                 if (Galv.SPAWN.overlap != 'terrain') {
  154.                         if (!$gameMap.isPassable(x,y)) return false;
  155.                 };
  156.         };
  157.         return true;
  158. };
  159.  
  160. Galv.SPAWN.clear = function(mapId,clearSaved) {
  161.         var mapId = mapId || $gameMap._mapId; // 0 is to target current map
  162.         if (mapId == $gameMap._mapId) {
  163.                 // same map clear
  164.                 if (Galv.SPAWN.onScene()) SceneManager._scene._spriteset.clearSpawnedEvents(clearSaved);
  165.                 $gameMap.clearSpawnedEvents(mapId,clearSaved);
  166.         } else if (clearSaved) {
  167.                 // clear save on another map
  168.                 var sEvents = $gameMap._savedSpawnedEvents[mapId]
  169.                 for (var eId in sEvents) {
  170.                         Galv.SPAWN.clearSSwitches(mapId,eId);
  171.                 }
  172.                 $gameMap._savedSpawnedEvents[mapId] = {};
  173.         }
  174. };
  175.  
  176. Galv.SPAWN.clearSSwitches = function(mapId,eventId) {
  177.         for (var s = 0; s < Galv.SPAWN.sSwitches.length; s++) {
  178.                 var key = mapId + "," + eventId + "," + Galv.SPAWN.sSwitches[s];
  179.                 $gameSelfSwitches.setValue(key,false);
  180.         };
  181. };
  182.  
  183. Galv.SPAWN.unspawn = function(obj) {
  184.         var eId = Number(obj.eventId());
  185.         if ($gameMap._events[eId].isSpawnEvent) {
  186.                 $gameMap.unspawnEvent(eId);
  187.                 if (Galv.SPAWN.onScene()) SceneManager._scene._spriteset.unspawnEvent(eId); // eId undefined?
  188.         };
  189. };
  190.  
  191. //-----------------------------------------------------------------------------
  192. // DATA MANAGER
  193. //-----------------------------------------------------------------------------
  194.  
  195. DataManager.loadSpawnMapData = function() {
  196.         var mapId = Galv.SPAWN.spawnMapId;
  197.     if (mapId > 0) {
  198.         var filename = 'Map%1.json'.format(mapId.padZero(3));
  199.         this.loadDataFile('$dataSpawnMap', filename);
  200.     } else {
  201.         window.alert("ERROR: You didn't set a spawn map ID. Choose a map from your map list to use for the spawn map.")
  202.     }
  203. };
  204.  
  205. DataManager.loadSpawnMapData();
  206.  
  207.  
  208. //-----------------------------------------------------------------------------
  209. // GAME MAP / SPRITESET MAP / SCENE MAP
  210. //-----------------------------------------------------------------------------
  211.  
  212. Galv.SPAWN.Game_Map_initialize = Game_Map.prototype.initialize;
  213. Game_Map.prototype.initialize = function() {
  214.         this._savedSpawnedEvents = {};
  215.         Galv.SPAWN.Game_Map_initialize.call(this);
  216. };
  217.  
  218. Galv.SPAWN.Game_Map_setup = Game_Map.prototype.setup;
  219. Game_Map.prototype.setup = function(mapId) {
  220.         this._savedSpawnedEvents[mapId] = this._savedSpawnedEvents[mapId] || {};
  221.         Galv.SPAWN.Game_Map_setup.call(this,mapId);
  222. };
  223.  
  224. Galv.SPAWN.Game_Map_setupEvents = Game_Map.prototype.setupEvents;
  225. Game_Map.prototype.setupEvents = function() {
  226.         Galv.SPAWN.Game_Map_setupEvents.call(this);
  227.         $gameMap.setupSpawnEvents();
  228. };
  229.  
  230. Game_Map.prototype.setupSpawnEvents = function() {
  231.         // Adds saved spawn events to event list
  232.         for (var eId in this._savedSpawnedEvents[this._mapId]) {
  233.                 var event = this._savedSpawnedEvents[this._mapId][eId];
  234.                 var x = event.x;
  235.                 var y = event.y;
  236.                 var id = event.id;
  237.                 this._events[eId] = new Game_SpawnEvent(this._mapId,eId,x,y,id,true);
  238.         };
  239. };
  240.  
  241. Game_Map.prototype.spawnEvent = function(id,x,y,save) {
  242.         // Get highest event id available
  243.     var eId = this._events.length;
  244.         // Add event to event list
  245.     this._events[eId] = new Game_SpawnEvent(this._mapId,eId,x,y,id,save);
  246.  
  247.         // Add save data if save
  248.         if (save) this._savedSpawnedEvents[this._mapId][eId] = {id: id, x:x, y:y, eId: Number(eId)};
  249.         if (Galv.SPAWN.onScene()) SceneManager._scene._spriteset.createSpawnEvent(eId);
  250. };
  251.  
  252.  
  253. Game_Map.prototype.spawnedEvents = function(includeSaved) {
  254.         var array = [];
  255.         for (var i = 0; i < this._events.length; i++) {
  256.                 if (this._events[i] && this._events[i].isSpawnEvent) {
  257.                         if (!this._events[i].isSavedEvent || (this._events[i].isSavedEvent && includeSaved)) {
  258.                                 array.push(i);
  259.                         };
  260.                 };
  261.         };
  262.         return array;
  263. };
  264.  
  265. Game_Map.prototype.unspawnEvent = function(eId) {
  266.         this._events[eId] = null;
  267.         Galv.SPAWN.clearSSwitches(this._mapId,eId);
  268.         delete(this._savedSpawnedEvents[this._mapId][eId]);
  269. };
  270.  
  271. Game_Map.prototype.clearSpawnedEvents = function(mapId,clearSaved) {
  272.         // clear normal
  273.         var sList = this.spawnedEvents(clearSaved);
  274.         for (var i = 0; i < sList.length; i++) {
  275.                 var eId = this._events[sList[i]]._eventId;
  276.                 // clear self switches
  277.                 Galv.SPAWN.clearSSwitches(mapId,eId);
  278.                 // remove event
  279.                 this._events[sList[i]] = null;
  280.                 // remove from saved list
  281.                 delete(this._savedSpawnedEvents[this._mapId][eId]);
  282.         };
  283.         // remove all null events from end of event to prevent array bloat
  284.         this.removeNullEvents();
  285. };
  286.  
  287. Game_Map.prototype.removeNullEvents = function() {
  288.         for (var i = this._events.length - 1; i > 0; i--) {
  289.                 if (this._events[i] === null) {
  290.                         this._events.splice(i, 1);
  291.                 } else {
  292.                         break;
  293.                 };
  294.         };
  295. };
  296.  
  297.  
  298. // SPRITESET
  299.  
  300. Spriteset_Map.prototype.unspawnEvent = function(eId) {
  301.         for (var i = 0; i < this._characterSprites.length; i++) {
  302.                 var event = this._characterSprites[i]._character;
  303.                 if (event.isSpawnEvent && eId == event._eventId) {
  304.                         this._tilemap.removeChild(this._characterSprites[i]);
  305.                 };
  306.         };
  307. };
  308.  
  309. Spriteset_Map.prototype.clearSpawnedEvents = function(clearSaved) {
  310.         for (var i = 0; i < this._characterSprites.length; i++) {
  311.                 var event = this._characterSprites[i]._character;
  312.                 if (event.isSpawnEvent) {
  313.                         if (!event.isSavedEvent || (event.isSavedEvent && clearSaved)) {
  314.                                 this._tilemap.removeChild(this._characterSprites[i]);
  315.                         };
  316.                 };
  317.         };
  318. };
  319.  
  320. Spriteset_Map.prototype.createSpawnEvent = function(id) {
  321.         var event = $gameMap._events[id];
  322.         var sId = this._characterSprites.length;
  323.         this._characterSprites[sId] = new Sprite_Character(event);
  324.         this._characterSprites[sId].update(); // To remove occsaional full-spriteset visible issue
  325.         this._tilemap.addChild(this._characterSprites[sId]);
  326.  
  327. };
  328.  
  329. })();
  330.  
  331. //-----------------------------------------------------------------------------
  332. // SPAWN EVENT OBJECT
  333. //-----------------------------------------------------------------------------
  334.  
  335. function Game_SpawnEvent() {
  336.     this.initialize.apply(this, arguments);
  337. }
  338.  
  339. Game_SpawnEvent.prototype = Object.create(Game_Event.prototype);
  340. Game_SpawnEvent.prototype.constructor = Game_SpawnEvent;
  341.  
  342. Game_SpawnEvent.prototype.initialize = function(mapId,eventId,x,y,spawnEventId,saveEvent) {
  343.         this._spawnX = x;
  344.         this._spawnY = y;
  345.         this._spawnEventId = spawnEventId;
  346.         this.isSpawnEvent = true;
  347.         this.isSavedEvent = saveEvent;
  348.         Game_Event.prototype.initialize.call(this,mapId,eventId);
  349.         DataManager.extractMetadata(this.event());
  350. };
  351.  
  352. Game_SpawnEvent.prototype.event = function() {
  353.     return $dataSpawnMap.events[this._spawnEventId];
  354. };
  355.  
  356. Game_SpawnEvent.prototype.locate = function() {
  357.         var x = this._spawnX;
  358.         var y = this._spawnY;
  359.     Game_Event.prototype.locate.call(this, x, y);
  360. };

作者: aasll    时间: 2017-5-4 09:50
建议你通过 复制 现有的事件来增加
作者: xjh01    时间: 2017-5-4 09:51
sutao80216 发表于 2017-5-4 00:29
这个插件应该是你需要的。
https://galvs-scripts.com/2016/05/14/mv-event-spawner/
//------------------ ...

这个也是复制事件的,不过比我找到的那个功能要全一些,谢谢。
作者: wymwaq    时间: 2017-5-4 11:44
我是通过调用模版事件做的
用下面第一个插件把需要生成的事件放在模版地图里面
然后在新地图中用第二个插件的脚本命令动态调用

插件:
建立模版事件https://github.com/triacontane/R ... er/TemplateEvent.js
事件动态生成https://github.com/triacontane/R ... ter/EventReSpawn.js
作者: xjh01    时间: 2017-5-4 14:50
fux2 发表于 2017-5-3 23:38
你还需要把这个新建的事件加入到地图事件列表

注意,这种访问方法非常不规范(而且在事件里塞这么大一坨也 ...

非常感谢指点!




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1