• 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!

Having a hard time with string_replace?

K

KC Frye

Guest
I'm trying to make a game where I can insert the player's name into the dialogue, but I can't get the string_replace function to work?

The dialogue of each character originates in either the Step or Create function of each character and will read as follows:

myDialogue [0,0] = "playername, over here!"
With "playername" being the substring I'm trying to replace. The character then runs a script in the step function called scr_dialogue which contains this code:

DialogueSystemOn = true;
dialogueBox = instance_create_layer(camera_get_view_x(view_camera[0]) + 6, camera_get_view_y(view_camera[0]) + 335, "DialogueBox", obj_DialogueBox);
if messageGiver.hasExpression = true {
expressionBox = instance_create_layer(camera_get_view_x(view_camera[0]) + 64,camera_get_view_y(view_camera[0]) + 152, "DialogueBox", obj_ExpressionBox);
}

with (dialogueBox) {
maxLength = sprite_width - 36;
maxHeight = sprite_height - 24;
myMessage = messageGiver.myDialogue[index1,index2];
messageIndex = 0;

if (string_height_ext(myMessage, 28, maxLength) > maxHeight) {
textHeight = string_height_ext(messageGiver.myDialogue[index1, index2], 28, maxLength);
amount = (textHeight / (sprite_height - 24));;
startingAt = 0;
for (i = 0; i < amount; ++i) {
myMessage = string_copy(messageGiver.myDialogue[index1, index2], startingAt, (string_length(messageGiver.myDialogue [index1, index2]) / amount))
startingAt = string_length(myMessage[index1, index2]) * (i + 1);
}
}
else
myMessage[messageIndex] = messageGiver.myDialogue [index1, index2];
}


The text itself, once converted to the variable "myMessage" is drawn inside of the object "obj_dialogueBox" and reads as follows:

draw_self();
draw_set_color(c_black);
draw_set_font(messageGiver.fnt);
draw_text_ext(bbox_left + 20, bbox_top + 18, myMessage[messageIndex], 28, maxLength);
The code I am trying to use to replace "playername" with the actual name of the player is some variant of this code:

if (string_pos("playername", myMessage) != 0)
{
string_replace(myMessage, "playername", PlayerName)
}
where "PlayerName" is a global variable set earlier to the player's name. For example, PlayerName = "Susie".

The problem is that I can't for the life of me, figure out where to put this code to make it work. Please help?
 

chamaeleon

Member
I'm trying to make a game where I can insert the player's name into the dialogue, but I can't get the string_replace function to work?

The dialogue of each character originates in either the Step or Create function of each character and will read as follows:



With "playername" being the substring I'm trying to replace. The character then runs a script in the step function called scr_dialogue which contains this code:



The text itself, once converted to the variable "myMessage" is drawn inside of the object "obj_dialogueBox" and reads as follows:



The code I am trying to use to replace "playername" with the actual name of the player is some variant of this code:



where "PlayerName" is a global variable set earlier to the player's name. For example, PlayerName = "Susie".

The problem is that I can't for the life of me, figure out where to put this code to make it work. Please help?
string_replace() does not modify the original string, it returns a new string as the result. Since you don't store that result anywhere, or otherwise make use of it in order to display it, your call to the function is effectively not doing anything for you.
 
If you have the player's name set to a variable, you can do this:

playername = whatever the player sets their name as.
myDialogue[0,0] = string(playername) + ", over here!"
 
Top