How do I show a variable in text that is drawn from a json file?

0 favourites
  • 5 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • I'd like to include the value of a variable in a text. I already found out that it is usually done by "text " &variable, but this does not work if the text comes from a json file.

    Text in json file:

    Result:

    Variation without quotation marks:

    Is there any way that I can include variables in the json file text snippets? If it was just one variable that is at the end of the text, I could include it after the text, but I want to include several variables in one text snippet.

  • You can't do it. You need to insert this variable into the text with events.

    I usually do this with replace() expression. For example, in the array file I have this text:

    Gold: %coins%
    

    Then I replace %coins% tag with the actual number of coins:

    CoinsText Set Text to Array.At(....)
    CoinsText Set Text to replace(Self.Text, "%coins%", CoinsVariable)
    
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thank you very much for your answer!

    Can I do the replacement multiple times when I want to put multiple variables into the text, or can I only have one replacement action per text?

  • You can use replace expression multiple times, either nested or as separate actions.

  • you can check this:

    cdn.discordapp.com/attachments/225550155531812865/966974946746191932/RegexMatchVariable.c3p

    ---

    Sorry, English is not my native language, but you can roughly understand the idea from here:

    ( need 'Dictionary' object type)

    {"is-c3-clipboard-data":true,"type":"events","items":[{"eventType":"variable","name":"REGEX_VARIABLE","type":"string","initialValue":"\\{(\\S+)\\}","comment":"{gold}","isStatic":true,"isConstant":true},{"functionName":"ParseFormat","functionDescription":"","functionCategory":"","functionReturnType":"string","functionIsAsync":false,"functionParameters":[{"name":"source","type":"string","initialValue":"","comment":""}],"eventType":"function-block","conditions":[],"actions":[],"children":[{"eventType":"variable","name":"parseText","type":"string","initialValue":"","comment":"","isStatic":false,"isConstant":false},{"eventType":"block","conditions":[{"id":"test-regex","objectClass":"System","parameters":{"string":"source","regex":"REGEX_VARIABLE","flags":"\"\""}}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"parseText","value":"source"}}],"children":[{"eventType":"block","conditions":[{"id":"for","objectClass":"System","parameters":{"name":"\"\"","start-index":"0","end-index":"RegexMatchCount(source, REGEX_VARIABLE, \"g\") - 1"}}],"actions":[],"children":[{"eventType":"variable","name":"MatchKey","type":"string","initialValue":"","comment":"","isStatic":false,"isConstant":false},{"eventType":"variable","name":"MatchValue","type":"string","initialValue":"","comment":"","isStatic":false,"isConstant":false},{"eventType":"block","conditions":[],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"MatchKey","value":"RegexMatchAt(source, REGEX_VARIABLE, \"g\", loopindex)"}},{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"MatchValue","value":"Dictionary.Get(mid(MatchKey, 1, len(MatchKey)-2))"}},{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"parseText","value":"RegexReplace(ParseText, REGEX_VARIABLE, \"\", MatchValue)"}}]}]},{"eventType":"block","conditions":[],"actions":[{"id":"set-function-return-value","objectClass":"Functions","parameters":{"value":"parseText"}}]}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"}],"actions":[{"id":"set-function-return-value","objectClass":"Functions","parameters":{"value":"source"}}]}]}]}
    

    ---

    It uses regular expressions to match.

    	Hi, {name}! You Have {gold} G!
    

    Use RegexMatchCount to find how many variables need to be replaced in this string. it is 2

    	RegexMatchCount(source, "\{(\S+)\}", "g")
    

    and then use a loop to through it. 0 ~ 1

    	For "" from 0 to RegexMatchCount(source, REGEX_VARIABLE, "g") - 1
    

    and then Use RegexMatchAt to replace the variable. it is '{name}' and '{gold}'.

    	MatchKey = RegexMatchAt(source, REGEX_VARIABLE, "g", loopindex)
    

    and then Use mid(text, index, count) extract content.

    	mid(MatchKey , 1, len(MatchKey)-2)
    
    	MatchValue = Dictionary.Get(mid(MatchKey, 1, len(MatchKey)-2))
    

    and then Use RegexReplace to replace the Dictionary value.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)