0 votes

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?

Godot version v3.3 Stable
in Engine by (20 points)

2 Answers

0 votes
Best answer

Hi!

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

by (54 points)
selected by

Managed to get it working using this. Thanks!

0 votes

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.

by (21 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.