• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Converting an int to a string.

face

Member
I'm making a sound menu and currently it's displaying the sound as 1.
I want it to display sound as 100%.

Everytime I increase it it should change to 110%, then 120%, etc...

I've been at this for hours, I can't find a way to put the int part (100) and the string part (%) in the same variable.

Edit: I've tried to use pointers but they don't seem to work in gml.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Use the string function to convert the int part to a string, then concatenate it with another string containing the % part using the addition operator (+).
 

face

Member
Use the string function to convert the int part to a string, then concatenate it with another string containing the % part using the addition operator (+).
I was able too get what I wanted but for some reason I cannot change the display value. It's stuck at 110.00%.

I have two objects, one to increase and other to the decrease the volume:

GML:
//obj_decrease
if (mouse_check_button_pressed(mb_left)) {
       
    volume = volume - 0.1; //obj_increase is the same but this line has a "+" instead of a "-"

}
   
audio_master_gain(volume);
I got another object that draws what I want:


GML:
//obj_drawvolume
//draw event

draw_self()
draw_set_valign(fa_middle);
draw_text(x, y, string(volume_percentage) + "%");
draw_set_font(fnt_volume);
And the last one, that calls and sets all the variables:


GML:
globalvar volume;
globalvar volume_percentage;

volume = 1;

volume_percentage = (volume + 0.1) * 100;
I can't figure out what's the problem.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Which event is the last code snippet in?
If it's in a repeating event, you are constantly setting volume to 1, overwriting any changes made by the player. This line needs to be outside of a repeating event.
If it's not in a repeating event, you are only setting volume_percentage once, at that exact moment. The variable will not update automatically just because there are variables involved in the assignment. Their value at the exact time this line of code runs will be taken. This line needs to be in a repeating event.

Also, globalvar is deprecated and known to cause issues on certain platforms. I therefore suggest not using it. If you want to declare a global variable, declare it using the global. prefix. You can find further information here: https://manual.yoyogames.com/#t=GameMaker_Language/GML_Overview/Variables/Global_Variables.htm
 

face

Member
Which event is the last code snippet in?
If it's in a repeating event, you are constantly setting volume to 1, overwriting any changes made by the player. This line needs to be outside of a repeating event.
If it's not in a repeating event, you are only setting volume_percentage once, at that exact moment. The variable will not update automatically just because there are variables involved in the assignment. Their value at the exact time this line of code runs will be taken. This line needs to be in a repeating event.

Also, globalvar is deprecated and known to cause issues on certain platforms. I therefore suggest not using it. If you want to declare a global variable, declare it using the global. prefix. You can find further information here: https://manual.yoyogames.com/#t=GameMaker_Language/GML_Overview/Variables/Global_Variables.htm
It's in the create event, I changed "volume = 1" to "others" -> "game start" so that I don't have that issue everytime I enter that room.

The increase and decrease are in the left mouse button pressed event. The volume variable was changing, it's something with the draw object, I just don't know what it is. The code is in the draw event, everything should be working...

I guess it's something I'll figure out eventually.
 

TsukaYuriko

☄️
Forum Staff
Moderator
It's not related to the drawing. It's this.
If it's not in a repeating event, you are only setting volume_percentage once, at that exact moment. The variable will not update automatically just because there are variables involved in the assignment. Their value at the exact time this line of code runs will be taken. This line needs to be in a repeating event.
Otherwise, you'll set that variable once and never again, and at the time it is being set, it's set to 110. ;)
 
Top