Call a function from notes in skills?

Hello!

I am trying to learn javascript and I am a complete noob with everything that it has to do with coding.

After some tutorials I made a test plugin

The plugin just states a variable

Code:
var rage = 0;

after that I have 2 functions

Code:
function rageadd(number) {
           rage += number;
}

function ragesub(number) {
          rage -= number;
}

So after turning on the plugin in rpg maker, I tried to call the rageadd function on a skill by typing on the notes

Code:
rageadd(5);

The variable doen't add 5 and it stays 0.


After that I tried to change the function #1 to:
Code:
function rageadd(){
     console.log("123");
}

to see if is calls the function in the console but that doesn't work either..

so.. what am I doing wrong?
Notetags and metatags are strings, they will never call functions unless you make another function that will extract the information from the notetag and use eval to execute it.
Notetags and metatags are strings, they will never call functions unless you make another function that will extract the information from the notetag and use eval to execute it.
Well I understood nothing, but at least now I know that these things are not for my level yet.

Since I don't really know what are notetags or metatags and so on, can you make a simple example?

not a complicated script.

A good example would be a variable in a plugin called x, how can I add 5 to this variable through a the notes on skills?
Go to the help section of the editor and search for the meta function. That is the default (and very limited) function to read from a notebox.

As for an alternative description of what a notebox is: consider it a textbox that has no function in the game, used for your developers notes of what you want to do with that part of the game. Similiar to the notes on skills #1 and #2 in a new project.
It does absolutely nothing ever ingame.

However, because it is stored with the game, plugin developers have used this to pass data (never commands) to their Plugins.
And then the plugins can do whatever they want with that data, including processing or executing it.
Plugins can read from a skill's notebox by evaluating the text that is stored in the notebox and seeing if certain series of characters exist there (e.g. a plugin might look for <RAGEADD: x> where x is the number of rage the designer wants to have the skill add. However, the plugin needs to be programmed to do this. This doesn't happen naturally. Your plugin is not programmed to do anything like this.

Certain other mechanisms in RPG Maker run code directly; for example, you could type rageadd(5); at the beginning of a damage formula and when that skill is used, it will add 5 to the rage variable. You could also have the skill run a common event and type rageadd(5); into a "Script..." event command inside that common event. Note that since you have defined rage as a global variable (outside of any class or object), rather than as a property of the Battler object, rage will be tracked globally, not on a per-character basis.
Plugins can read from a skill's notebox by evaluating the text that is stored in the notebox and seeing if certain series of characters exist there (e.g. a plugin might look for <RAGEADD: x> where x is the number of rage the designer wants to have the skill add. However, the plugin needs to be programmed to do this. This doesn't happen naturally. Your plugin is not programmed to do anything like this.

Certain other mechanisms in RPG Maker run code directly; for example, you could type rageadd(5); at the beginning of a damage formula and when that skill is used, it will add 5 to the rage variable. You could also have the skill run a common event and type rageadd(5); into a "Script..." event command inside that common event. Note that since you have defined rage as a global variable (outside of any class or object), rather than as a property of the Battler object, rage will be tracked globally, not on a per-character basis.

That make sense, thank you very much. Well I am still learning I was never good at coding but I really want to learn using rpg maker.

Now I made a code:

Code:
var x = 0;
var y = 0;
var z = 0;

function varadd(v, n){
    v += n ;
}

function varsub(v, n){
    v -= n;
};

for some reason the v (variable) is not replaced when I type varadd(x, 5) in a script.

it only works if I have the code like this
Code:
var x = 0;
var y = 0;
var z = 0;

function varadd(n){
    x += n ;
}

function varsub(n){
    x -= n;
};

and calling the fuction like this varadd(5), but that only adds to a specific variable..
I'm not 100% sure; some people could surely coach you better (and this is why you should be posting in the Learning JS section; that's where most of the good JS coaches hang out).

But I think that the problem is that when you pass a simple variable (numeric, string, or boolean) into a function as a parameter, it takes the value of the variable rather than the variable itself to be manipulated. I'm not 100% sure about this, because most objects will be accepted as parameters that can be manipulated (and technically speaking, I believe variables are objects).

You could rewrite your code to handle the three values as an array, like this, if you want to be able to pass in a parameter that specifies which element you want to change:

Code:
var my_array = [0, 0, 0] // Pretend these are x, y, and z

function varadd(v, n){
   my_array[v] += n ; // To modify "x", "y", or "z", pass in either 0, 1, or 2 as v.
}

function varsub(v, n){
   my_array[v] -= n;
};

(To retreve the value of an array element, such as x, just reference its location in the array, such as typing my_array[v] where you would have typed x before.)

For most practical uses in RPG Maker, you will need to learn object-oriented (OO) programming principles. You may already undersand OO programming, but based on your examples I'm inferring that you probably don't. I'll admit that the OO paradigm can be really annoying when you're starting out as a programmer, but it's mandatory to build anything that can scale (such as a video game, where similar behavior can be seen everywhere throughout the game).

Additionally, I recommend avoiding calling your variables "x", "y", or "z", as a lot of RPG Maker's internal classes use x, y, and z to refer to the x-location, y-location, and depth (visual priority) of onscreen graphics.
I'm not 100% sure; some people could surely coach you better (and this is why you should be posting in the Learning JS section; that's where most of the good JS coaches hang out).

But I think that the problem is that when you pass a simple variable (numeric, string, or boolean) into a function as a parameter, it takes the value of the variable rather than the variable itself to be manipulated. I'm not 100% sure about this, because most objects will be accepted as parameters that can be manipulated (and technically speaking, I believe variables are objects).

You could rewrite your code to handle the three values as an array, like this, if you want to be able to pass in a parameter that specifies which element you want to change:

Code:
var my_array = [0, 0, 0] // Pretend these are x, y, and z

function varadd(v, n){
   my_array[v] += n ; // To modify "x", "y", or "z", pass in either 0, 1, or 2 as v.
}

function varsub(v, n){
   my_array[v] -= n;
};

(To retreve the value of an array element, such as x, just reference its location in the array, such as typing my_array[v] where you would have typed x before.)

For most practical uses in RPG Maker, you will need to learn object-oriented (OO) programming principles. You may already undersand OO programming, but based on your examples I'm inferring that you probably don't. I'll admit that the OO paradigm can be really annoying when you're starting out as a programmer, but it's mandatory to build anything that can scale (such as a video game, where similar behavior can be seen everywhere throughout the game).

Additionally, I recommend avoiding calling your variables "x", "y", or "z", as a lot of RPG Maker's internal classes use x, y, and z to refer to the x-location, y-location, and depth (visual priority) of onscreen graphics.

Well I found that if I state a variable as object like this

var x = {value:0};

then x is an object and I can change the value with:
variable.value += number

in an function.

Thank you very much for your time. I don't know what OO is, but this is the first time that I am learning any language for coding.
Well I found that if I state a variable as object like this

var x = {value:0};

then x is an object and I can change the value with:
variable.value += number

in an function.

Thank you very much for your time. I don't know what OO is, but this is the first time that I am learning any language for coding.

Yup! You've created a hash with the {key: value} format, and a hash is a complex object just like an array. What you've done will work just as well.
I don't know what OO is, but this is the first time that I am learning any language for coding.
Leontas2007,

Your posts are so fascinating to read -- as I see a new person learning how to program 😎

I'd love to help -- though we need to figure out an easy way for you to share your plugin without spamming the board's here.

Hmm, a google document might work 😮 --- Not the most traditional use of google documents, especially for programming, but it would be a viable short term solution ....

Can anyone in the community suggest a better solution for quick code sharing (without asking people to install tools on their system like an editor, git, etc.; and without asking people to upload/download files to a common server which)?
​
Sincerely,

Joy Diamond.

Original thread: https://forums.rpgmakerweb.com/threads/call-a-function-from-notes-in-skills.87228/