How to use the player's name as a variable in JSON?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By terminalSyzygy

I have a dialog system that parses all of the dialog from JSON and displays it. There’s also a part for the player to enter their name, which gets stored in a variable in one of the scripts. Say the player names themself “John Doe”. I was wondering if in the JSON, I could have a line like:

“Your name is [PLAYERNAME]”

and in-game it would print as:

“Your name is John Doe”.

The closest thing I can think of is custom BBCode but I doubt that’d actually work. Any pointers on how to implement something like this?

:bust_in_silhouette: Reply From: Mazlum


You’re looking for Python .sub() function and using Regex to search for [PLAYERNAME] and replace it with .sub()

Godot Docs about RegEx: RegEx Class

Managed to get it working using this. Thanks!

terminalSyzygy | 2021-08-06 00:20

:bust_in_silhouette: Reply From: danielhernandez

In Godot you can format Strings like this:

var name = "John Doe"
var message = "Your name is %s" % name

Now in message you have the String “Your name is John Doe”. I guess you extract the dialog from the JSON and store it in a variable. If that’s the case, you can use this technique.