enums

Heyo. Does MV support the use of enums in script? I've tried doing a piece of code that I believe should work, but I'm getting an error.

var terrainCostEnum = {0: 0.5, 1: 0, 2: 1, 3: 1.25, 4: 1.5, 5: 1.75, 6: 2, 7: 10};
var v = terrainCostEnum.3;
$gameMessage.add(v);

The error is coming from the middle line. MV doesn't seem to recognize the "terrainCostEnum.3" method of accessing a value from the enum. Does anyone know how I can make something like this work?
Enums in javascript are literally just objects and have no special handling, but also javascript objects are just dictionaries that can pretend to be arrays when they want to, so... I'd just use an array at that point. Your 'keys' are the numbers 0-7, in numeric form, so use an 8-element array isntead and access it with [].

Something like this:
var terrainCostEnum = [0.5, 0, 1, 1.25, 1.5, 1.75, 2, 10];
var v = terrainCostEnum[3];
$gameMessage.add(v);
Also, the error occurred because property names that start with a number cannot be accessed via dot notation. Bracket notation should work:
Code:
var v = terrainCostEnum['0']
However, it's easier (not to mention more convenient and higher performance) to simply use an array like Doktor_Q suggests. 🙏
Enums in javascript are literally just objects and have no special handling, but also javascript objects are just dictionaries that can pretend to be arrays when they want to, so... I'd just use an array at that point. Your 'keys' are the numbers 0-7, in numeric form, so use an 8-element array isntead and access it with [].

Something like this:
var terrainCostEnum = [0.5, 0, 1, 1.25, 1.5, 1.75, 2, 10];
var v = terrainCostEnum[3];
$gameMessage.add(v);
Also, the error occurred because property names that start with a number cannot be accessed via dot notation. Bracket notation should work:
Code:
var v = terrainCostEnum['0']
However, it's easier (not to mention more convenient and higher performance) to simply use an array like Doktor_Q suggests. 🙏

Ah yeah, fair enough. Thanks for that 😀 I'm only just starting to learn this stuff so insight like this is exactly what I need. I'm using this to create a table of comparison to read terrain tags for tiles and turn them into land effect modifiers for trpg style movement grids, just for context why it's 0-7.
Actually, one other question along the same lines. Can you store algorithms as items in an array and have them use a variable value? For example, coordinate offsets. Would it be possible to store a list like:
[x,x+1,x+2,x+3,x+4]
Or would I have to do the computations first and store the results? It would be much more convenient if I could store the algorithms and then feed the a base number to work on each time I call from them.
Actually, one other question along the same lines. Can you store algorithms as items in an array and have them use a variable value? For example, coordinate offsets. Would it be possible to store a list like:
[x,x+1,x+2,x+3,x+4]
Or would I have to do the computations first and store the results? It would be much more convenient if I could store the algorithms and then feed the a base number to work on each time I call from them.

The key has to be a string or number value, so probably not in the way you want.

At that point, you're really trying to make a function, by the definition that you put in parameters and it calculates values from it.
That's kinda what I expected. It's never as easy as you hope Just trying to come up with a simple, modular way of calculating and storing movement range that is also more readable to a layman than an elaborate loop of calculations.

So I'm pretty much thinking I'll be writing it out long form at this point. I need to store both the x and y in a small array, and then store that array in a tile array in order for the script calls like $game.Player.pos(x,y) to understand it, right?
I actually just tried my idea in an online js editor and it appears to work.

var x = 1;
var y = 1;
var move = [[x +2,y+4],[x+3,y-1]];
var tile = move[1];

This returns 4,0 as expected.
var terrainCostEnum = {0: 0.5, 1: 0, 2: 1, 3: 1.25, 4: 1.5, 5: 1.75, 6: 2, 7: 10};
var v = terrainCostEnum.3;
$gameMessage.add(v);

Just to be pedantic, this isn't really an enum 🙂
An enum (see the Typescript section for a decent explanation) is intended to map a human-readable name/string to an integer value.
Something in the MV code that resembles an enum is below. I say resembles, because although it's not a traditional enum, it accomplishes the same use case -- being able to refer to integer values by human-readable names.

Code:
Game_Action.EFFECT_RECOVER_HP       = 11;
Game_Action.EFFECT_RECOVER_MP       = 12;
Game_Action.EFFECT_GAIN_TP          = 13;
Game_Action.EFFECT_ADD_STATE        = 21;
Game_Action.EFFECT_REMOVE_STATE     = 22;
Game_Action.EFFECT_ADD_BUFF         = 31;
Game_Action.EFFECT_ADD_DEBUFF       = 32;
Game_Action.EFFECT_REMOVE_BUFF      = 33;
Game_Action.EFFECT_REMOVE_DEBUFF    = 34;
Game_Action.EFFECT_SPECIAL          = 41;
Game_Action.EFFECT_GROW             = 42;
Game_Action.EFFECT_LEARN_SKILL      = 43;
Game_Action.EFFECT_COMMON_EVENT     = 44;
Game_Action.SPECIAL_EFFECT_ESCAPE   = 0;
Game_Action.HITTYPE_CERTAIN         = 0;
Game_Action.HITTYPE_PHYSICAL        = 1;
Game_Action.HITTYPE_MAGICAL = 2;

However, it's easier (not to mention more convenient and higher performance) to simply use an array like Doktor_Q suggests. 🙏

Also to be pedantic 😉
The performance is exactly the same, an array in Javascript is actually implemented as an object where the key values happen to be numeric (in fact, behind the scenes the numeric keys of an array are coerced to string the same as a normal object).

Code:
let myArray = ['one', 'two']
console.log(myArray['1'])

I'd say it could be more convenient to use an object if you have a lot of values. If, say, during testing you realized you needed to alter the 38th value that would be easier with an object than counting to the 38th value in an array.

Finally, if you want a truly numeric key value, you can use a Map.
Yeah, completely a novice, so I may not use terminology correctly.
I ended up using a simply array and it got me exactly where I needed to be because the indexes are 0-7 for my 8 values, and that's perfect because it matches how terrain tags are, so I can read the terrain tag and directly use that value in whatever code I write, which makes it super simple for me.

On the other hand, I did something super ugly 😛 you guys let me know how terrible this is and if there's an easier way to do it, but i tested it and it works perfectly so I'm not sure but maybe that's all that matters? haha.
So, it creates an array of tile locations of varying size depending on the unit's move value, which just arbitrarily I've decided is 1-7. I can then use this array to loop through for various things like displaying movement range on the map, detecting other units, etc. I'm not quite to that point yet, but I'm probably going to iterate through this array, do my calculations, and for each viable tile add it to a second array of tile objects, basically a tile dictionary in C# but I don't know what you'd call it in .js. Anyway, here's the ugly code. Curious if there's an easier way to get the exact effect I've built here.
Spoiler
var moveRange = 7;
var x = 0;
var y = 0;
var array1 = [];
if (moveRange >= 1) {array1.push ([x,y-1],[x+1,y],[x,y+1],[x-1,y]);
if (moveRange >= 2) {array1.push ([x-1,y-1],[x,y-2],[x+1,y-1],[x+2,y],[x+1,y+1],[x,y+2],[x-1,y+1],[x-2,y]);
if (moveRange >= 3) {array1.push ([x-2,y-1],[x-1,y-2],[x,y-3],[x+1,y-2],[x+2,y-1],[x+3,y],[x+2,y+1],[x+1,y+2],[x,y+3],[x-1,y+2],[x-2,y+1],[x-3,y]);
if (moveRange >= 4) {array1.push ([x-3,y-1],[x-2,y-2],[x-1,y-3],[x,y-4],[x+1,y-3],[x+2,y-2],[x+3,y-1],[x+4,y],[x+3,y+1],[x+2,y+2],[x+1,y+3],[x,y+4],[x-1,y+3],[x-2,y+2],[x-3,y+1],[x-4,y]);
if (moveRange >= 5) {array1.push ([x-4,y-1],[x-3,y-2],[x-2,y-3],[x-1,y-4],[x,y-5],[x+1,y-4],[x+2,y-3],[x+3,y-2],[x+4,y-1],[x+5,y],[x+4,y+1],[x+3,y+2],[x+2,y+3],[x+1,y+4],[x,y+5],[x-1,y+4],[x-2,y+3],[x-3,y+2],[x-4,y+1],[x-5,y]);
if (moveRange >= 6) {array1.push ([x-5,y-1],[x-4,y-2],[x-3,y-3],[x-2,y-4],[x-1,y-5],[x,y-6],[x+1,y-5],[x+2,y-4],[x+3,y-3],[x+4,y-2],[x+5,y-1],[x+6,y],[x+5,y+1],[x+4,y+2],[x+3,y+3],[x+2,y+4],[x+1,y+5],[x,y+6],[x-1,y+5],[x-2,y+4],[x-3,y+3],[x-4,y+2],[x-5,y+1],[x-6,y]);
if (moveRange >= 7) {array1.push ([x-6,y-1],[x-5,y-2],[x-4,y-3],[x-3,y-4],[x-2,y-5],[x-1,y-6],[x,y-7],[x+1,y-6],[x+2,y-5],[x+3,y-4],[x+4,y-3],[x+5,y-2],[x+6,y-1],[x+7,y],[x+6,y+1],[x+5,y+2],[x+4,y+3],[x+3,y+4],[x+2,y+5],[x+1,y+6],[x,y+7],[x-1,y+6],[x-2,y+5],[x-3,y+4],[x-4,y+3],[x-5,y+2],[x-6,y+1],[x-7,y]);
}}}}}}}
moveRange will not be a constant in practice, it will be supplied by whatever unit is currently active, and the x,y will be supplied by that unit's position. I just made them constants for testing. So it can be run for any unit and come up with an appropriately sized movement diamond grid. Adding the locations in this order puts them in the correct order to next iterate through the array and add terrain costs from the center outwards so that when it looks at neighbors there are no errors where it's value has not already been calculated correctly. It adds the tile costs together and is then able to ignore tiles that the unit does not have enough movement to get to, based on the land cost, and tiles that are blocked for other reasons.
... On the other hand, I did some super ugly 😛
👋 You did indeed!

Move range will be a diamond, you can iterate over the first quadrant and use symmetry to fill out the others. 😤
Spoiler: Example
Code:
var mov = 3;                // move range (tiles)
var pos = { x: 10, y: 3 };  // starting position
var res = [];               // output array
for (var x = mov+1; x--; ) {
  for (var y = mov+1-x; y--; ) {
    if (x > 0 || y > 0) res.push([pos.x - x, pos.y - y]);
    if (x > 0) res.push([pos.x + x, pos.y - y]);
    if (y > 0) res.push([pos.x - x, pos.y + y]);
    if (x > 0 && y > 0) res.push([pos.x + x, pos.y + y]);
  }
}
console.info(res.map(String).sort()); console.info('count', res.length);
^ This should exclude zero movement. Note that it iterates inward and only outputs to the console. 😮‍💨

[Edit: follow-up post, sorted in -> out link.]

The performance is exactly the same, ...
To continue the pedantry: not exactly the same. 😛
Supporting claim (includes benchmark): https://stackoverflow.com/questions/17295056/array-vs-object-efficiency-in-javascript
Either way, I wouldn't bother about such a small difference. I assume it's a compiler quirk or something~
👋 You did indeed!

Move range will be a diamond, you can iterate over the first quadrant and use symmetry to fill out the others. 😤
Spoiler: Example
Code:
var mov = 3;                // move range (tiles)
var pos = { x: 10, y: 3 };  // starting position
var res = [];               // output array
for (var x = mov+1; x--; ) {
  for (var y = mov+1-x; y--; ) {
    if (x > 0 || y > 0) res.push([pos.x - x, pos.y - y]);
    if (x > 0) res.push([pos.x + x, pos.y - y]);
    if (y > 0) res.push([pos.x - x, pos.y + y]);
    if (x > 0 && y > 0) res.push([pos.x + x, pos.y + y]);
  }
}
console.info(res.map(String).sort()); console.info('count', res.length);
^ This should exclude zero movement. Note that it iterates inward and only outputs to the console. 😮‍💨

I'll have to look at this and try to understand it. This is somehow giving the same diamond pattern? My question is how do i use this if it goes from outside to center, just iterate through the array backwards? Is there anything inherently "better" about doing this rather than doing it my way, other than it being smaller code? One of my intents was to make something that would be easy to read by a layman, rather than a complicated loop. I also have no idea how to duplicate that for the other 3 quads 😛
I'll have to look at this and try to understand it. This is somehow giving the same diamond pattern?
I believe so, yes! Imagine a diamond: a quadrant is a right-angled triangle. The first loop (x) goes from "move range" to 0. The second, inner loop (y) goes from "move range - x" to 0. Together these describe a triangle.

Then we push up to four coordinates to the output. If one of the coordinates is zero, there may be overlap; the "if (x > 0)" stuff is to account for the overlap and only add those tiles to the output once. "if (x > 0 || y > 0)" means "if either x or y is more than zero": this is for ignoring the starting/central tile. "if (x > 0 && y > 0)" means "if both x and y are more than zero". Four "ifs" for four quadrants~ 😮‍💨
My question is how do i use this if it goes from outside to center, just iterate through the array backwards?
Reversing both loops should reverse the output order, yes! 😤
Is there anything inherently "better" about doing this rather than doing it my way, other than it being smaller code?
Doing it manually has a higher chance of typos, but is probably also simpler to understand/spot errors, so I'm not sure either approach is "better". Unless you need to have huge move ranges or something, that could get tiresome to do manually. 😅
One of my intents was to make something that would be easy to read by a layman, rather than a complicated loop.
Oh. 😅
I also have no idea how to duplicate that for the other 3 quads 😛
As described above, it should already output the entire move grid, i.e. all the quads. You can check the output coordinates via the console, just press F8 during playtest. 🙂
Alright, so I ran this in an online js program and then mapped the results. It does give the diamond pattern, but it doesn't give them in anything remotely like the correct order to then be used for calculating tile costs. I am working on using your method as a base to create a loop that adds them in the correct order. Your example was pretty helpful. I'll post my result if I get it working and maybe you can tell me how to simplify it again, heh.

I still may ultimately go with manually writing them out simply because it's easier for a novice to read, but it's nice to learn how to do this with loops regardless.
I'm still unsure why the order of the outputs makes a difference? Surely you only need the x/y coordinates to fetch the terrain tag. Is it related to pathfinding or something? 😅

Also, just so you know, for some reason I included a .sort() in my example's console output, which will show the console results in alphabetical order...if you haven't already, you may want to edit it so it's simply:
Code:
console.info(res.map(String));
Probably still won't be the desired order, but may as well work with the "real" order rather than the A-Z one. 😴
I'm still unsure why the order of the outputs makes a difference? Surely you only need the x/y coordinates to fetch the terrain tag. Is it related to pathfinding or something? 😅

Also, just so you know, for some reason I included a .sort() in my example's console output, which will show the console results in alphabetical order...if you haven't already, you may want to edit it so it's simply:
Code:
console.info(res.map(String));
Probably still won't be the desired order, but may as well work with the "real" order rather than the A-Z one. 😴
Yeah it's related to pathfinding. You need to be able to read the tiles in a specific order because each one looks at it's neighbors to calculate it's own move cost, so if it's neighbors haven't been calculated yet you'll get inaccurate results. They need to go from the center (character position) outward in order for the tiles to have calculated properly, so the next tile has the correct info to work with. It's weird understanding the mechanics of what needs to happen without actually understanding the code of it, heh.

I can try to put together an image to explain why this is important if you want.
But...don't you already know the neighbours? E.g. tiles adjacent to (5, 5) are (4, 5), (5, 4), (6, 5), (5, 6). Then the pathfinding node limit will be equal to the movement range. I assumed this tile generation thing (x, y) was primarily for filtering out possible move locations for input/display purposes. Well, it sounds like you've got it all worked out, I don't need to understand it~ 🙏

Anyway, as for sorting the output from inside to out, I made a few little edits to my original suggestion and this looks promising:
Spoiler: Grid loop + distance sort
Code:
var mov = 3;                // move range (tiles)
var pos = { x: 0, y: 0 };   // starting position
var res = [];               // output array
for (var x = mov+1; x--; ) {
  for (var y = mov+1-x; y--; ) {
    if (x > 0 || y > 0) res.push([pos.x - x, pos.y - y]);
    if (x > 0)          res.push([pos.x + x, pos.y - y]);
    if (y > 0)          res.push([pos.x - x, pos.y + y]);
    if (x > 0 && y > 0) res.push([pos.x + x, pos.y + y]);
} }
res.sort(function(a, b) { return Math.abs(a[0]) + Math.abs(a[1]) - Math.abs(b[0]) - Math.abs(b[1]) > 0 ? 1 : -1; });
console.info(res.map(String)); console.info('count', res.length);
It uses the same pattern generation approach, then sorts based on difference in coordinates:
  • dx₁ - x + dy₁ - y - (dx₂ - x + dy₂ - y) <=> dx₁ + dy₁ - dx₂ - dy₂

Original thread: https://forums.rpgmakerweb.com/threads/enums.110622/