Noob question: How to make the game read the code writen in JS files.

dragoonwys

Freelance Illustrator
Veteran
Joined
Jul 26, 2016
Messages
358
Reaction score
925
First Language
english
Primarily Uses
RMMV
I just finished learning the bare basics of java scripting so I'm still pretty new to this and I'm more than willing to learn more.
And apologies if my code looks messy or wrong. I looked up a number plugins but I'm not actually sure how to integrate the code to be called from the game. I see Imported || Imported followed by var and (function () {} and I tried fiddling around with those but it hasn't been working. I might fare better it someone could point out how to do this. ^^;a

This is the code I written which works on console logs, it's a RNG dice roll script for practice.
There are two things I'm trying to do.
1. Make it possible using an option from the editor to change NumberOfDice to a different number via an event.
2. Run the Dice Rolling function when called from the game via script/plugin command and return the results so I can use them as variables for conditional branches. (The string doesn't have to stay colors, it can be numbers too as long as at the end I have a result to compare to.)

//Dice Rolling Variables
var DiceType = ['Red','Yellow','Blue','Green'];
var NumberOfDice = 0 ;

//Dice Rolling function
var rollingDice = () => {
if (NumberOfDice === 4) {
var DiceRollA = DiceType[Math.floor(Math.random() * DiceType.length)];
var DiceRollB = DiceType[Math.floor(Math.random() * DiceType.length)];
var DiceRollC = DiceType[Math.floor(Math.random() * DiceType.length)];
var DiceRollD = DiceType[Math.floor(Math.random() * DiceType.length)];

} else if (NumberOfDice === 3) {
var DiceRollA = DiceType[Math.floor(Math.random() * DiceType.length)];
var DiceRollB = DiceType[Math.floor(Math.random() * DiceType.length)];
var DiceRollC = DiceType[Math.floor(Math.random() * DiceType.length)];

} else if (NumberOfDice === 2) {
var DiceRollA = DiceType[Math.floor(Math.random() * DiceType.length)];
var DiceRollB = DiceType[Math.floor(Math.random() * DiceType.length)];

} else if (NumberOfDice === 1) {
var DiceRollA = DiceType[Math.floor(Math.random() * DiceType.length)];

} else {
return 'You have no dice!';
}
};
//Die Rolling call
rollingDice();
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,639
First Language
Czech
Primarily Uses
RMMV
If you wish to change dice amount via event, that is possible. Just don't define local variables, but global variables.
Also, if you want the function to return dice results, you will have to use more return methods. As it is now, the game only defines a couple of local variables, in other words doesn't really do much, because local variables inside a function cannot be reached from outside.
 

dragoonwys

Freelance Illustrator
Veteran
Joined
Jul 26, 2016
Messages
358
Reaction score
925
First Language
english
Primarily Uses
RMMV
Okay let's see if I understood that correctly.
I added more return methods to the functions, but I'm not sure how the NumberOfDice is not a global variable. A global variable are variables that are not inside a functions {} brackets right?

//Dice Rolling Variables
var DiceType = ['Red','Yellow','Blue','Green'];
var NumberOfDice = 1 ;

//Dice Rolling function
var rollingDice = function() {
if (NumberOfDice === 4) {
var DiceRollA = DiceType[Math.floor(Math.random() * DiceType.length)];
var DiceRollB = DiceType[Math.floor(Math.random() * DiceType.length)];
var DiceRollC = DiceType[Math.floor(Math.random() * DiceType.length)];
var DiceRollD = DiceType[Math.floor(Math.random() * DiceType.length)];
return [DiceRollA,DiceRollB,DiceRollC,DiceRollD];

} else if (NumberOfDice === 3) {
var DiceRollA = DiceType[Math.floor(Math.random() * DiceType.length)];
var DiceRollB = DiceType[Math.floor(Math.random() * DiceType.length)];
var DiceRollC = DiceType[Math.floor(Math.random() * DiceType.length)];
return [DiceRollA,DiceRollB,DiceRollC];

} else if (NumberOfDice === 2) {
var DiceRollA = DiceType[Math.floor(Math.random() * DiceType.length)];
var DiceRollB = DiceType[Math.floor(Math.random() * DiceType.length)];
return [DiceRollA,DiceRollB];

} else if (NumberOfDice === 1) {
var DiceRollA = DiceType[Math.floor(Math.random() * DiceType.length)];
return [DiceRollA];

} else {
return 'You have no dice!';
}
};


//Die Rolling call
rollingDice();
 

dahlys

Meatbun
Veteran
Joined
Aug 28, 2017
Messages
95
Reaction score
99
First Language
English
Primarily Uses
RMMV
Global variables are game variables. They are set using the $gameVariables.setValue(id, value) and their values are retrieved by $gameVariables.value(id)

As it is right now, your function will run immediately when you load the game. That would be bad since global variable values are not going to be loaded when your plugin runs. Basically, anything outside (function() { and within (function() { will run on game load as long as the plugin is enabled.

var rollingDice is a local variable. You can only use local variables within your own plugin.

To get it set to a global variable, you need to either run checks for whether the game is loaded before setting $gameVariables.setValue(id, value), or place your code inside a function that is only activated by script call.

A script call function:
Code:
Game_Interpreter.prototype.diceRoll = function(NumberOfDice, variable) {
    var rollingDice = function() {
        if (NumberOfDice === 4) {
            var DiceRollA = DiceType[Math.floor(Math.random() * DiceType.length)];
            var DiceRollB = DiceType[Math.floor(Math.random() * DiceType.length)];
            var DiceRollC = DiceType[Math.floor(Math.random() * DiceType.length)];
            var DiceRollD = DiceType[Math.floor(Math.random() * DiceType.length)];
            return [DiceRollA,DiceRollB,DiceRollC,DiceRollD];
        } else if (NumberOfDice === 3) {
            var DiceRollA = DiceType[Math.floor(Math.random() * DiceType.length)];
            var DiceRollB = DiceType[Math.floor(Math.random() * DiceType.length)];
            var DiceRollC = DiceType[Math.floor(Math.random() * DiceType.length)];
            return [DiceRollA,DiceRollB,DiceRollC];
        } else if (NumberOfDice === 2) {
            var DiceRollA = DiceType[Math.floor(Math.random() * DiceType.length)];
            var DiceRollB = DiceType[Math.floor(Math.random() * DiceType.length)];
            return [DiceRollA,DiceRollB];
        } else if (NumberOfDice === 1) {
            var DiceRollA = DiceType[Math.floor(Math.random() * DiceType.length)];
            return [DiceRollA];
        } else {
            return 'You have no dice!';
        }
    };
    $gameVariables.setValue(variable, rollingDice);
};
Now if you have an event in the game that does script call this.diceRoll(NumberOfDice, variable), the variable will be set to either the array or the string 'You have no dice!'.

What I did was add a new function into Game_Interpreter, you can do that with any game object class (Game_Temp, Game_System etc.). If you choose Game_Temp, then the script call will be $gameTemp.diceRoll(NumberOfDice, variable)

I recommend watching this and this

EDIT: In addition, your entire function can be simplified when you learn more about loops and functions.
Code:
Game_Interpreter.prototype.diceRoll = function(NumberOfDice, variable) {
    if (NumberOfDice > 0) {
        var rollingDice = [];
        for (var i = 0; i < NumberOfDice; i++) {
            rollingDice.push(DiceType[Math.floor(Math.random() * DiceType.length)]);
        }
        $gameVariables.setValue(variable, rollingDice);
    } else {
        $gameVariables.setValue(variable, 'You have no dice!');
    }
};
 
Last edited:

dragoonwys

Freelance Illustrator
Veteran
Joined
Jul 26, 2016
Messages
358
Reaction score
925
First Language
english
Primarily Uses
RMMV
Ahhh I see, I didn't know that it would run the moment the game runs too. 8o
I looked at your code and that does make more sense now, so that's how script call works.
I'll go watch those videos to understand them more, thank you for pointing them out ouo/
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
Just set the variable using the Control Variables event command. Don't do it via script or a script call.
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,639
First Language
Czech
Primarily Uses
RMMV
Actually, let me put the global variables on a straight line. Because what @dahlys said about them is not really true. I mean yes, $gameVariables are global variables, but not the only ones.

We have two kinds of variables. Local variables and global variables.
Local variables are easy to recognize, they are defined using var.
Local variable means that when it is inside a function, it is inaccessible from the outside and will be set to undefined once the function is over.
It also means that when this variable is outside of a function, but in a file, it is accessible only by functions inside the file.
Of course, there are methods to make it accessible from other files too. Which is what rpg maker has done, so pretty much every variable that is outside of a function can be considered global.

But true global variables are any variables that are defined without using var.
Global variable means I can access it not only by functions inside a script file, but also I can freely access it outside of a script file. So by other script files etc. And it doesn't matter if I define it inside a function or not, I can still access it anywhere I want once it is defined.
So for example if I say x = 3, I have defined a global variable of value 3.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

People3_5 and People3_8 added!

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.

Forum statistics

Threads
105,868
Messages
1,017,090
Members
137,586
Latest member
Usagiis
Top