is it possible to use a single variable as multiple function parameters?

Hey guys,

I have an example plugin with a function, and a call script, but i just cant seem to get this to work:

Scriptcall:
Code:
◆Control Variables:#0018 = "James " + "Sunday"
◆Script:console.log($gameVariables.value(18))
:      :console.log("James", "Sunday")
◆Script:hey($gameVariables.value(18));

Plugin content:
Code:
hey = function(name, day) {
  return console.log("hey" + " " + name +  " " + "its" +  " " + day);
};

Is it possible to use that single variable to replace both(or more) parameters in a function? And if so, what would i need to put inside it?
3 things unrelated:
  • Don't need to return a console.log function, simply call it. Maybe this is a slimmed down version of something else though
  • You don't have to separate out the spaces in your string concatenation, doing this is perfectly fine
    • "hey " + name + " it's " + day
  • If you use MV v1.6.0+, try using Template Literals, it's simpler/easier
    • `hey ${name} it's ${day}`
Back on topic.
So you have a single variable that you want to split into multiple arguments? You can use the apply method, where you supply an Array of arguments and it will unpack the arguments for you.

Code:
hey.apply(this, $gameVariables.value(18).split(" "));

The string is first split into an Array, and then applied as an Array of arguments to the 'hey' function.

In this simple example, "this" doesn't matter. In more complex cases, it's important to supply the proper value of "this".


Edit: When you define the 'hey' function, you should also declare it with a keyword like const (new) or var (old)
The variable will have "James Sunday". It is one variable. When you pass it into the function it will be through the variable name. Day will be null or undefined or throw an error. If you want your function to receive two arguments, you have to pass two arguments.
@Aloe Guvner I think thats exactly what i was looking for thanks alot - Also for the additional tips, especially the template literals! Also Im not using v1.6+ because i heard it was buggy, and i wouldnt know how to deal with that, haha.

@Shaz Aloe Guvner gave a great workaround that works tho!
Why use an approach that requires a workaround though? Why not just pass through two arguments?

If it works and gives what you want using a method that you want to use, that's great. Lots of ways to achieve some things, so whichever is most suitable to you is the one to go with 🙂
@Shaz I was looking for methods to store alot of data in a single variable, because I dislike the global variables rpg maker offers, or atleast the way they get sorted. It feels extremely unorganized if you dont know beforehand how many variables youre gonna use for a specific thing youre making. So you could have 10 variables for one thing, then move on to the next thing, but lateron you find out that you actually need more variables for that first thing, but the variables that come next to it are already occupied by the second thing, so you need to place them further away, which makes it confusing.
Someone wrote a plugin that would let you insert new variables/switches (and other things in the database) and update all the events accordingly.

Original thread: https://forums.rpgmakerweb.com/threads/is-it-possible-to-use-a-single-variable-as-multiple-function-parameters.95161/