How do I call a variable from inside another variable?

0 favourites
  • 7 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Let's say I have the following Global text variables:

    var1 = "stuff"

    var2 = "I have "&var1&" here."

    {On start of Layout} -> {Text object} -> {Set text to} [var2]

    How would I get it to say: "I have stuff here." and not "I have "&var1&" here."

    Is there an approach to doing this I do not know or is there no way to do this?

    Also, it would be nice if it worked when variables were being called from objects such as arrays too so:

    Array.At(0,1) = "stuff"

    Array.At(1,1) = "I have "&var1&" here."

    {On start of Layout} -> {Text object} -> {Set text to} [Array.At(1,1)]

    Thanks for your help

  • Create global variable string, set its value to something

    Add text object to scene

    On layout load set text value to something like

    "I ran across a " & Globaltext & " on the way to work."

    Make sure to have spaces before and after the & signs.

    Additionally, I added 2 local variables to the text object for testing, one called Name and another called Age, then I put in the following at layout load

    Text.Name & " enjoys " & Globalstuff & " at age " & Text.Age

    That worked too with global and local variables

    Now for the arrays, either load from Json, or maybe on layout load set array elements like setxy 0,0 to value, setxy 0,1 to something then call it as Text - set text - Array.At(0,0) & " has " & Array.At(0,1)

    Make sure to put the right dimensions on the array sizes on X and Y

  • zumwaltwood answered the question. I want to point out why your original approach didn't work.

    The reason why what your doing doesn't work is because a variable is stored data. You are trying to store a line of code which is a computer process. These types of misconceptions happen with non programmers, but they are valuable lessons. Even if you don't plan on learning to code it will help if you watch a few videos or read up on programming basics.

  • I appreciate your answers, they will help others but the way I asked the question disregarded aspects of my specific problem because I underestimated the complexity. I think I need to be more specific now:

    I am using google sheets which has text data (script for story/narration), using AJAX request to put it into a CSV plugin which acts mostly like an array.

    The script in google sheets would say stuff like: ["Hey there, "& playerName &"."] where the variable playerName is stored in Construct, but to call this line, it would be [CSV.At(row,col)].

    AnomalyHatchery made me understand why this approach would not work, but I am not sure how to implement an alternative method to overcome this. I need to be able to freely put variables in google sheets within the text since it would be a lot better than writing them as "set texts" inside Construct and also it would allow me to update them whenever I need to without having to re-upload the game.

    I being naive, assumed that there existed a function that allowed the text inside [CSV.At(row,col)] to be processed; something like: "process(CSV.At(row,col))" which there probably isn't. But is there a way I have not seen yet to do this or is this simply impossible to do and a whole new approach is needed?

    Thanks for your help

  • I have an idea, but i can't do any testing until i get back in town later tonight.

    My thought is this is going to require you to write your own plugin, i think this can't be done natively. I could be very wrong about that, but now that i know the source, that gives me something to look at directly.

  • We'll use your example: ["Hey there, "& playerName &"."]

    In the sheet's cell write "Hey there, playerName."

    Then when you set text use this replace(CSV.At(row,col),"playerName",playerName)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • AnomalyHatchery, this is great, it worked like a dream! I was hoping there existed a function like replace() that I had not yet considered.

    Thank you both for your help.

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