"Replace" function for Strings, Text Objects, and Variables

Another user on the Discord server was wanting to be able to pass through a player name to their Yarn script in GD5.

While the documentation shows how you can pass events/commands out of Yarn, there doesn’t appear to be any way to pass commands/variables into yarn. You can manipulate Yarn variables within your Yarn script itself, but not pass data from the Global/Scene/Event variables in the main GD5 IDE.

This made us discuss workarounds, and one thought was to just store the Yarn script as a variable then do a replace on the string…only to find out there isn’t a Replace expression in GDevelop. At least not one we can find in the documentation or searching the expression finder.

Substrings are not a valid alternative to the replace() function from Javascript, especially when you consider the use case the user had. If you allow custom character names, a normal dialogue is going to have numerous instances of that player name. Meaning you’d have to do a LOT of substring/add variable/append to new string before you can pass it back. This would be overly complex and prone to error.

With that said, since Javascript natively has a replace() expression, I wanted to throw the request on here to get Replace added to GD5. Even unrelated to Yarn, this function is exceptionally helpful for multiple use cases.

5 Likes

I think there are generally many little functions like that that are required or just to make life easier that miss. We should make a list of them and someone (me or someone else with JavaScript knowledge) would make a little built-in extension for that.

2 Likes

Prefer include this in the engine directly !
I can do that, i’ve already added function like clamp() i know how to do it.

Yes please.

Yes, so it should be easy to add this one.

1 Like

Looks like @ddabrahim actually compiled a list of String Manipulation functions that would be super useful to have back in April: Word game with gdevelop? - #7 by ddabrahim

4ian already responded around which already exist, but I definitely feel like there’s no easy way to replicate Replace currently. There was mention of a card being created, but I’m not finding it on Trello.

Here’s there reference material I could find for the ones that seem to be missing.
Javascript Replace method : JavaScript String replace() Method
Javascript function to Count Character occurrence: JavaScript function: Letter count within a string - w3resource
Javascript Function to Count String Occurrence: JavaScript: Count the occurrence of a substring in a string - w3resource
Javascript String Trim method: JavaScript String trim() Method
Javascript function to count whitespace: javascript - Count Space(s) in a string - Stack Overflow
Javascript CharAt method: JavaScript String charAt() Method
Javacript Split method: JavaScript String split() Method (This one might be super difficult because we don’t have normal arrays…so it’d have to like, split them into a Container variable and a bunch of sub variables)

In general, it’d be nice that we have some way to do everything on this list (that makes sense in the engine): The Top 15 Most Popular JavaScript String Functions - Vegibit

Thank you both for your effort and time on this, whenever you get to it!

1 Like