Store notetag to variable

Hello everyone,

I'm trying to store something that is in the notetag of a certain item inside a variable. I want to do this because I'm trying to add a fishing system to my game. There are a lot of different fishes and they each have a certain difficulty level written in the note tags like this : <FishLvl: n>, and I would like to extract the n and place it inside a variable. I just can't add hundreds on conditional branches so this would be super useful ! Any idea how to do it ?
Hi there!

I don't know exactly how you are doing this, but assume you can do something like that:

$dataItems[itemId].meta.FishLvl > This will return to you the "n" part of your note tag. But it will be as a string, so you need to put it as a number, inside your variable:

So go to the control variable > script field:

Number($dataItems[itemId].meta.FishLvl)

if perhaps the item id is stored inside another variable:

Number($dataItems[$gameVariables.value(varId)].meta.FishLvl)
Hi there!

I don't know exactly how you are doing this, but assume you can do something like that:

$dataItems[itemId].meta.FishLvl > This will return to you the "n" part of your note tag. But it will be as a string, so you need to put it as a number, inside your variable:

So go to the control variable > script field:

Number($dataItems[itemId].meta.FishLvl)

if perhaps the item id is stored inside another variable:

Number($dataItems[$gameVariables.value(varId)].meta.FishLvl)
It works perfectly ! Thank you for this ^^
See:

This topic has an explanation of how to use regular expression.
Please, like the person's response if it is the solution.


Correct solution already posted above.​
This will return to you the "n" part of your note tag. But it will be as a string, so you need to put it as a number
Why is that necessary? The game variables don't have any problem storing it either way, and JavaScript's operators all convert strings to numbers when compared...is there something I'm not thinking of that makes this step necessary? Or are you just doing it as good practice, to keep a number as a number?
Why is that necessary? The game variables don't have any problem storing it either way, and JavaScript's operators all convert strings to numbers when compared...is there something I'm not thinking of that makes this step necessary? Or are you just doing it as good practice, to keep a number as a number?
Hi! ^^

First, for good practice. The meta property stored in the $datas, is always as a string.
So I believe I had to explain that to the topic author, and so, we need to convert to a number.

Second, it is not really a problem with the game variable. It is just the way it works.
First, it will check if the type of the value stored, is a number. If it is a number it will Math.round it, so if you put in the script field "3", the type will return a string, if so, it will not round leaving it as a string.

JavaScript:
Game_Variables.prototype.setValue = function(variableId, value) {
    if (variableId > 0 && variableId < $dataSystem.variables.length) {
        if (typeof value === "number") {
            value = Math.floor(value);
        }
        this._data[variableId] = value;
        this.onChange();
    }
};
(Maybe taking a look at the Game Interpreter command 122 can explain more too)

If you take the test yourself:
Spoiler
aaaa.png
So I believe I had to explain that to the topic author, and so, we need to convert to a number.
That was my question - why do you say "need"? I understand how RPG Maker sets the variables internally; but I also understand that all JS comparison operators disregard type.

So unless you're using === someplace, it shouldn't matter. Am I wrong?
Hi! @ATT_Turan

That was my question - why do you say "need"? I understand how RPG Maker sets the variables internally; but I also understand that all JS comparison operators disregard type.

Ok, let me explain the things I thought when I first read the author's question. That is a little hard for me because all that happens so automatically in my mind and English is not my native language:

- I don't know if the author knows javascript. And if he knows, how much he knows? I don't know.
- He wants to extract something from the note tag. So it seems to me that he didn't know about the meta property.
- His note tag is probably a number because "Lvl" means level, so a number.
- He will put it inside a variable.
- He doesn't want to use a lot of conditional branches.

So what I need to do to help him, with only this information with a less margin of error possible?
So I say "need", because I try to follow this logic above, with the information that I had and presume, about the post.

a certain difficulty level written in the note tags like this: <FishLvl: n>

He is putting that inside a variable. And I assume that "n" is a number.

That is the only thing I know.
I don't know exactly where he is using it or how he will use it. I don't have the exact reason. He will use the default maker comparison? He will use a js comparison, so "==" or "===" ? I don't know all the details. He will use that only on the conditional branches? Don't know.

All that I know is that he need to get the "n" of his note tag and that is probably a number(Because is a level).
If it is a number, for good practice, I do prefer to explicitly convert it.

So I explain to him that the meta property exists(because it seems to me he didn't know about it) and how it works, that by default all values of a meta are string.
So I follow the good practice to explicitly convert it as a number to avoid any misunderstanding or some future error, and I believe that's one of the reasons "Good practices" exist, avoid common errors.

I will just tell you, as an example, three errors(that comes to my mind, maybe there is more) or weird behaviors he can encounter if we do not explicitly convert it:

1 - Accessing an array index or object property:

Spoiler
<FishLvl: 3> $dataItems[ID].meta.FishLvl => Will return " 3"

Let's say that we will access an array or object property with this value:

JavaScript:
let index = $dataItems[ID].meta.FishLvl
let names = ["Harold", "Therese", "Marsha", "Claude"]
let selectedName = names[index] => will return undefined

Because of the blank space " 3"

To avoid that, the author could make his note tags this way(without space):

<FishLvl:3>

So we can repeat the process above and will not throw undefined.

But if the author, sometimes adds the space and sometimes not(I think these things, because when I make plugins for other people to use, I had to think it that way)?

He could have this note tag on item id 5(Without space): <FishLvl:3>

And on item id 8(With space): <FishLvl: 3>

And during his development, depending on how he uses it, he could get into this error and not understand why, since both note tags seem the same.

When converting it to a number, it will remove the blank space. And the author will not need to worry about it.

2 - Extra blank space on message window:

Spoiler
Let us say that he is storing the property of meta inside a variable id 5, to show it as a text on the message window.

The fish level is: \v[5]

Again, because of blank space already on the meta property, he can get a message like this:

The fish level is: 3

instead of this:

The fish level is: 3

Why I have a double space if I only put one space after the ":" ?

3 - Using a number method on a string can give him an error:
Spoiler
1619552461576.png

Anyway, little things, that can bother some people a lot(By the way, If you read again the author post specifying the note tag, you will see that he add the blank space).

So in the end, I'm not following the reason why you are asking.
Are you in doubt about something? Or you are just trying to say that there is no reason for the author to do the things the way I tell him to do(Which is totally fine, no problem)?

And I also believe we can go off-topic here with this discussion, since the author already said that the solution worked, we can just move on.
So in the end, I'm not following the reason why you are asking.
Are you in doubt about something? Or you are just trying to say that there is no reason for the author to do the things the way I tell him to do(Which is totally fine, no problem)?
Kind of both! I was thinking the same thing as you, that the original poster didn't have much JavaScript knowledge; so I presumed that after reading in the variable he would be using the event commands inside of RPG Maker.

And so my intent was not to say you're wrong, but because you were using the words "need," "have to," and I didn't think he had to - I wanted to know if I was wrong.

I'm satisfied with my understanding, and your answer was absolutely right. I'm sorry if you didn't understand me well and you felt you had to go into long explanations.

Original thread: https://forums.rpgmakerweb.com/threads/store-notetag-to-variable.135915/