MV How can I turn this into a plugin?

I want to turn this code into a plugin where I can do a simple script call like: getEventTag(gameVar, tag);
Also if you can clean up my bad code please do.


var gameVar = 19; var tag = 'fridge' function getEventTag() { return $gameMap._events.map(function (event, eventId) { if ($dataMap.events[eventId].meta[tag]) { return eventId; } }); } getEventTag = getEventTag().filter(Boolean); getEventTag = getEventTag[Math.randomInt(getEventTag.length)]; $gameVariables.setValue(gameVar, getEventTag);
I don't understand what you're trying to do with this.

Did you write this code? What does filter(Boolean) mean, and why do you keep saying "getEventTag = "?

The first part looks redundant as it's just returning a map() call, so why have the function instead of just doing the map()? Then I can't understand what the intent of the lower three lines is.

Please explain what your desired result is.
I know it looks awful but it does work in a script call. The desired result is to search all events on current map for <tag> in the note box. It then stores those event IDs in an array and chooses one at random to assign to a game variable.
filter(Boolean) gets rid of all empty values in the array because it was adding empty values for any event that did not have the specified tag. Everything else that looks wrong is because I don't know how to write my own code so I stitch together parts of other code until something works.
I want to turn this code into a plugin where I can do a simple script call like: getEventTag(gameVar, tag);
Also if you can clean up my bad code please do.


var gameVar = 19; var tag = 'fridge' function getEventTag() { return $gameMap._events.map(function (event, eventId) { if ($dataMap.events[eventId].meta[tag]) { return eventId; } }); } getEventTag = getEventTag().filter(Boolean); getEventTag = getEventTag[Math.randomInt(getEventTag.length)]; $gameVariables.setValue(gameVar, getEventTag);
In general, to create a new plugin, simply create a new text file, put your code in it, save that file with the .js tag. In this case, to make it reusable, simply put everything inside a new function

JavaScript:
function generalGetEventTag(gameVar, tag){
    function getEventTag() {
        return $gameMap._events.map(function (event, eventId) {
            if ($dataMap.events[eventId].meta[tag]) {
                return eventId;
            }
        });
    }
    getEventTag = getEventTag().filter(Boolean);
    getEventTag = getEventTag[Math.randomInt(getEventTag.length)];
    $gameVariables.setValue(gameVar, getEventTag);
}

Then at any point you can call it with these two parameters, e.g. generalGetEventTag(19, 'fridge'

What does filter(Boolean) mean
Filters only values that are true when cast to Boolean; in this case removing null (EDIT: undefined) values.

Also if you can clean up my bad code please do.
It's probably a good idea to create new variables when doing different things instead of overwriting existing, unrelated ones. E.g. instead of getEventTag = getEventTag().filter(Boolean); you could do var filteredEvents = getEventTag().filter(Boolean);. This will make your code a lot easier to read.
JavaScript:
function generalGetEventTag(gameVar, tag){
    function getEventTag() {
        return $gameMap._events.map(function (event, eventId) {
            if ($dataMap.events[eventId].meta[tag]) {
                return eventId;
            }
        });
    }
    getEventTag = getEventTag().filter(Boolean);
    getEventTag = getEventTag[Math.randomInt(getEventTag.length)];
    $gameVariables.setValue(gameVar, getEventTag);
}

Then at any point you can call it with these two parameters, e.g. generalGetEventTag(19, 'fridge'


Filters only values that are true when cast to Boolean; in this case removing null values.

That filter function reminds me of the Array#select method in Ruby.

@Mac15001900
One thing I always wanted to ask other people is why do coders insert other functions right inside the main function they're calling? Why don't they keep the function declarations separate?
Thank you Mac this worked, and thanks for the tips
You should also really avoid using variables that have the same names as functions. That's very bad practice.

instead of getEventTag = getEventTag().filter(Boolean); you could do var filteredEvents = getEventTag().filter(Boolean);. This will make your code a lot easier to read.
That one I don't have too much of a problem with, as it's still an array and you're trimming it down. The next line, where you're reassigning the array variable to a single value, is another bad habit.
I would be grateful if you would like to fix it to make it more readable and less redundant.
One thing I always wanted to ask other people is why do coders insert other functions right inside the main function they're calling? Why don't they keep the function declarations separate?
It's a matter of personal preference really. If you're only going to use a function in one place, you might as well keep it in that place. Though in this specific case I just wanted to change as little as possible to best showcase the concept of encapsulating something in a reusable function - it would look differently if I wrote it from scratch.
That one I don't have too much of a problem with, as it's still an array and you're trimming it down.
It's not an array before this assignment though - it's a function, which gets overwritten with an array, which in turn gets overwritten with a single value.
I would be grateful if you would like to fix it to make it more readable and less redundant.
Alright, here's how I would write it:
JavaScript:
function getRandomEventWithTag(gameVar, tag) {
    let validIds = []
    for (let id = 1; id < $gameMap._events.length; id++) {
        if ($dataMap.events[id].meta[tag]) validIds.push(id);
    }
    $gameVariables.setValue(gameVar, validIds[Math.randomInt(validIds.length)]);
}
As much as I like my maps and filters, a for loop in this case is a more elegant solution. Also, if it really was my code I'd also add a comment as to what this function does and why, but I'll leave that up to you 😉
It's not an array before this assignment though - it's a function, which gets overwritten with an array, which in turn gets overwritten with a single value.
Yes, fair point - my point was that I wouldn't see any problem with the first two lines using the same name (although it's another bad practice to not actually declare the variable). We're agreed that the variable shouldn't be the same name as the function.

Filters only values that are true when cast to Boolean; in this case removing null values.
How could the original function return an array with any null values in it?
How could the original function return an array with any null values in it?
Ah, JavaScript and it's non-existence types. I meant undefined xD
It will do that for every event that doesn't fit the criteria of the if statement.
Alright, here's how I would write it:
JavaScript:
function getRandomEventWithTag(gameVar, tag) {
let validIds = []
for (let id = 1; id < $gameMap._events.length; id++) {
if ($dataMap.events[id].meta[tag]) validIds.push(id);
}
$gameVariables.setValue(gameVar, validIds[Math.randomInt(validIds.length)]);
}
Awesome thank you again.
@Mac15001900 the code is alot cleaner though, but a comment is also usefull,
specially for plugins that target the "tag" of the event notebox.

@Rekkid just out of curriously, how exactly does this function do, I do it can
get the ID of the event with the tag "something", but not how it is used for
the mechanic or conditional branch if you use it.

I like to learn stuff and see how you put it to work the way you want it.

PS: if I like the mechanic you use, may I use it in my own game?
@Mac15001900 the code is alot cleaner though, but a comment is also usefull,
specially for plugins that target the "tag" of the event notebox.

@Rekkid just out of curriously, how exactly does this function do, I do it can
get the ID of the event with the tag "something", but not how it is used for
the mechanic or conditional branch if you use it.

I like to learn stuff and see how you put it to work the way you want it.

PS: if I like the mechanic you use, may I use it in my own game?
Yeah so the end goal is to find targets for my NPCs to walk to. In this example say its lunch time so Harold wants to grab something from the fridge. I would run this function and then pathfind to a random fridge so he can grab something to eat. The reason its random is because I also have a house decorating system so I can't really know which event is the fridge, or how many, or where its located, or if it even exists at all.

And yeah of course use what you want.
I try to figure it out how to use it, but it's a really cool idea.

Original thread: https://forums.rpgmakerweb.com/threads/how-can-i-turn-this-into-a-plugin.154619/