Variables: convert string to integer

I've got a value coming in from a notetag and it comes in as [1]

I want it to be 1 so I can use it as a conditional branch variable check.

any thoughts on how to strip the [ ] smybols and make it a Integer value?

Cheers,

`Andy
if it's in var,


value = var[0].to_i
Edit: Is that value of yours a string containing square brackets or an array? If former is the case you can use the methods described below, otherwise you can stick with Shaz's suggestion.

I don't know how you set up your notetags, but you could try one of these:

var[1..-2]  # Returns a copy of the substring between the second- and the second last character.

var[/\[(.*?)\]/, 1]  # Searches the string for a sequence wrapped in [] and returns the first match.

If your string can contain leading or trailing spaces, use value.strip to get a version of the string with these spaces removed.

The second form makes sense if you don't already have "[1]" separated.

For example:
var = "[5]"       # "[5]"

var = var[1..-2]  # "5"

value = var.to_i  # 5

var = "$game_switches[5]"  # "$game_switches[5]"

var = var[/\[(.*?)\]/, 1]  # "5"

value = var.to_i           # 5
If you want your note-tag to be a single number, and yet you get an array from it, than you need to fix your note-tag itself, not converting the array to integer.


While simply converting it to an integer will get you your number, it will add an extra step for the process, which is absolutely not needed.


Show us your note-tag, and the method where you get the data from the note-tag, so we can help you in that.
I am using this script to get the notetag: http://www.rpgmakervxace.net/topic/10545-est-simple-notetags-grabber/

The note tag is 

<Enviro: 1>

To populate the value I am using:

Control Variables: [0001:MapEnvironment] = $game_map.map.note_args_all_int("Enviro")

(i had to change the word Environment to "Enviro" because it wouldn't fit in the script area on the Control Variable command)

Edit: I'VE GOT IT FINALLY!!

Control Variables: [0001:MapEnvironment] = $game_map.map.note_args_all_int("Enviro")

Control Variables: [0001:MapEnvironment] = $game_variables[1][0]

 

this is working 😀
This should give you the answer - note, it's a Script event command, not a Control Variables (in order to avoid the length issue).

Code:
$game_variables[1] = $game_map.map.note_args_all_int("Enviro")[0].to_i
If you have an older version of the engine where you only have a short line and it auto-breaks, make it break like this:

Code:
$game_variables[1] = $game_map.map.note_args_all_int("Enviro")[0].to_i
Shaz, as always you're amazing! Thank You.

Original thread: https://forums.rpgmakerweb.com/threads/variables-convert-string-to-integer.38553/