Introducing: analogue.jsEver used tung's system but found it to be too slow, or that it doesn't work right at times? Well have no fear since this sleek and lightweight alternative has arrived. I call it "analogue". It's a direct code analogue of the entities on the map, that loads persist scripts and can interact with scripts exactly like how persist can.
Source: revision 10This can be used a a direct replacement of tung's 'persist.js'.
How to use:You make a script and store it at "../scripts/maps/your_map.js" for the map 'your_map.rmp' in the /maps directory. This logic holds true for maps in subfolders. "/maps/town/inside.rmp" would need a code stored in "/scripts/maps/town/inside.js" to function.
The code for a town looks like this:
({
enter: function() {
/* code to run on entry of a map */
},
leave: function() {
/* code to run on leave */
},
leaveNorth: function() {
/* code to run when you leave north of the map */
/* also: South, West, and East variants. */
},
// people:
Bob: {
talk: function() {
/* code to run when talked to Bob */
},
create: function() {
/* code to run when Bob is created */
},
destroy: function() {
/* code to run when Bob is destroyed */
},
generate: function() {
/* code to run when Bob generates commands */
},
touch: function() {
/* code to run when Bob is touched by the player */
},
}
})
When you write code in those 'handlers', it'll directly add it to the person on the map. People also persist values, so you can give bob a property and he'll retain it, like if he were talked to and said something different on the first visit:
Bob: {
visited: false,
talk: function() {
if (!this.visited) {
/* do something when he wasn't visited */
}
this.visited = true;
},
}
In the code above, visiting Bob means he'll do the action in talk once. Have fun and don't shy away with persisting variables!
Alternatively you can reach the world, map, and person scopes by using three passed variables to the function calls:
({
event: 21324, // some event number, or whatever
Bob: {
visited: false,
talk: function(me, map, world) {
if (!me.visited) {
me.visited = true;
/* do something when he wasn't visited. */
}
else if (world.majorEvent) {
/* talk about some major event in the world. */
}
else if (map.event == 21324) {
/* or perhaps something local to just this map. */
}
},
}
})