[Solved] Trying to insert a player name variable in Yarn

I’m fairly new to gDevelop and still wrapping my head around its functionality. Currently, I’m working on a game with visual novel-like elements, and using Yarn to manage the dialogue trees. It’s working really well, with the one exception that I can’t for the life of me figure out how to replace a placeholder (i.e. {$playerName}) in the dialogue with the actual user entry.

I have the name data stored as the global variable playerName and it’s populating correctly from the text entry box. I also easily replaced the name label field with the player name with a "The BBCode text of namesText contains “{$playerName}” condition and the “Change the BBCode text of namesText to playerName” action. However, I can’t work out how to do the same for instances of {$playerName} in the dialogue. I’ve read through several threads on the topic and installed RegEx, but nothing seems to work.

Can anyone help me understand what I’m doing wrong? Bonus points if it’s something minor and stupid I should have caught two hours ago.

Here is a post with instructions on how to add a username into yarn dialogue. sing out if you still have trouble after trying it out.

Hi MrMen! Your solution from that thread was what I tried to use :slight_smile: Unfortunately, it’s just not functioning for me - I’m sure it’s my error. The steps I followed were:

  1. Store the player’s name input into a global variable called playerName
  2. Export my dialogue tree from Yarn as a JSON
  3. Create a variable called placeholderDialogue and load the exported JSON into it
  4. Create a variable called updatedDialogue and use an action to change its content to RegEx::Replace(“{$playerName}”,“g”,placeholderDialogue,playerName
  5. Load the dialogue from the variable updatedDialogue

Does this seem right to you? I think I might have missed something. Thank you for your help!

Can you screen shot those events? The steps you describe seem correct. It may be a mistake in the implementation.

Yes, not a problem!

The problem is with the curly brackets “{}” - they’re special characters in the Yarn dialogue system and are used for commands, functions, and conditionals inside a Yarn script.

Replace them with the “@” symbol (and possibly remove the $ symbol too).

That’s good to know, thank you! I did try replacing it, and I made sure to export a new JSON, but it still doesn’t seem to be replacing - the dialogue when I preview just says @playerName@ now instead of {!playerName}. I’ve attached more screenshots in case I misinterpreted what you meant.


A few things:

  1. Is that event (where you replace the player name in the dialogue text) in the “dialogue is running” event? It should be done either at the beginning of the scene or once the player’s name has been entered.
  2. Can you screen shot the events that set the text object to the dialogue text?
  3. Could you run the preview with debugger, and then look at what updatedDialogue contains?

Yes, for sure:

  1. It was - I’ve now moved it to happen once the player’s name has been entered. Thank you!
  2. Yes, no problem. At the beginning of the scene:

    Screenshot 2025-02-10 161216
  3. Something is going on here, I’m getting an “error while loading from scene variable” message. Here’s the message and the debug of the dialogue variables at the end of the preview.

So are you loading the dialogue from one file at the beginning of the scene, and then changing the dialogue by loading it from another file later on?

If you scroll up on the error list, is there an earlier error regarding the loading of the JSON file?

That’s what I’ve been doing for this scene, yes. The character’s name isn’t input until partway through the scene. But if it would help to put these loading steps at the beginning of the scene and not load the version I’m currently using, I can try that!

And there is not - the errors in the screenshot are the only ones that occur ¯_(ツ)_/¯

An update - I moved the events related to placeholderDialogue and updatedDialogue to the beginning of the scene, and it’s working better now, in that the @playerName@ is being replaced! The only problem is, it’s replacing it with null, the original value of playerName. It’s not a problem with the storage, because the player’s name label still updates correctly, so I think it’s an issue with updating the updatedDialogue variable after the name is submitted. Messing around with it now…

Ah! I think I have it! The events trigger when the scene loads and then once more after the playerName value has been changed. This should be the only scene where I need to do that, since the player’s name has been established after this point. Thank you so much for your help, it’s working as expected now!

As a side note - I’ve read many threads while working on this project, and you are the person who helps figure out the problem in a lot of them, MrMen. Thank you for all you do, it’s a huge help to beginners like me!

1 Like