0 Favourites

How do I make variable text?

  • I'm trying to randomise certain words within dialogue so they will change each time the text object is displayed. As an example, a character might say:

    "I'm a (carpenter/smith/doctor) from the (city/coast/mountains)."

    One of the words in brackets will be randomly chosen each time.

    I can think of a couple of long-winded workarounds to achieve this but I'm wondering whether there's a quick and easy way that I'm overlooking. Ideally, I guess I would like to be able to set a global variable to a random bit of text in the same way that it can be set to a random number. So I could have a variable called 'occupation' and set it to something like this: choose(carpenter,smith,doctor). Then I could reset it each time after use. I know that exact way doesn't work but maybe there's a similar function?

    Thanks in advance for any help.

  • Hello,

    I don't know if this is the best way, but it should work: you can create a Dictionary for all "professions" for example and the keys would be using numbers so it would look like the below:

    key:1 value:carpenter

    key:2 value:smith

    key:3 value:doctor

    Then you can create another dictionary for cities and call it Cities perhaps

    Then you'd have an event to set the text:

    Textname > Set Text = "I'm a " & Professions.Get(round(random(1,3))) & " from the " & Cities.Get(round(random(1,3))) & "."

    Hope that helps.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • That does help! Thanks very much indeed.

    EDIT: Actually, it seems I can't use integers as keys in a dictionary... Perhaps I could do it with an array instead?

  • Hello,

    I didn't know that... glad you mentioned it. Still you can do the same thing, all you need to do is just cast the number as a string at the getter and set it as text with the setter.

    So when you set the key, instead of setting it to the number 1, you set it to the text "1".

    When you however do the random code from the example above; it should be like below:

    Textname > Set Text = "I'm a " & Professions.Get(str(round(random(1,3)))) & " from the " & Cities.Get(str(round(random(1,3)))) & "."

    That bold bit would change the number to text so it would match the key you entered.

    Hope that helps.

  • Works perfectly! Thanks very much - greatly appreciated.

  • Glad it worked ^_^ and good luck!

  • ...

    . So I could have a variable called 'occupation' and set it to something like this: choose(carpenter,smith,doctor).

    ...

    You could also do something like,

    set text "I am a "& choose("Doctor","Mechanic","Carpenter") & " from the " & choose("City","Mountains","Coast")

  • Ah, thank you - I forgot the quotes.

    That will be very useful because there are times when I want long lists of variables which will be used frequently (so a dictionary is perfect) and times when I want just a few options for variety in one particular piece of dialogue (so the choose method will be best).

    Thanks again to both of you - this is just about the most helpful and supportive forum I know.

  • I'm having a little trouble with this again. I now want to store different instances of randomly generated text permanently.

    I have a number of floating pages and when clicked on I'd like them to display a block of randomly chosen text (which I can now do thanks to the advice received above) but I'd then like that page to store the text which has been generated (as an instance variable?) so that from now on that particular page will always display the same thing when clicked on.

    What's the easiest way to do this?

  • Not sure how you made it, but you can add strings together.

    So if you have 3 parts (Strings), you can do Combined_text = Part_1 & " " & Part_2 & " " Part_3 and then you store the combined text.

    Another way is to use a list and you just store all the parts there as elements and then you use that to add them all together like above and then store the result in a Dictionary. Then you can just use the key in the dictionary to get the page of text when you need it.

  • Hi, Panurge

  • Thanks both very much. Nimos100 - that makes a lot of sense and I feel slightly embarrassed for not figuring it out myself! Whiteclaws - the array method is very elegant and I'll certainly make use of it (also, a tip of the hat for spotting me! Hope all is well with you ).

  • Sorry to keep resurrecting this thread but this sort of follows on from the problems I had before.

    Everything is now working fine except the dictionaries sometimes return a result of 0 and I'm not sure why. For example, I have a dictionary called 'Jobs' with keys from 1 to 6 containing job names as values and set text from it like this:

    Jobs.Get(str(round(random(1,6))))

    All of the possible jobs will sometimes appear as text but I also sometimes get a 0. This happens with other dictionaries too.

  • [quote:217x3kfl]Jobs.Get(str(round(random(1,6))))

    All of the possible jobs will sometimes appear as text but I also sometimes get a 0. This happens with other dictionaries too.

    Sounds weird the dictionary returns 0 if something doesn't exist. Have you checked the debugger to see that it actually have 6 keys and that they match the values 1 to 6? your code should work as long as the keys are there.

  • Ack! Sorry about this - I just checked through and found I'd missed out a couple of keys (I have loads in each dictionary) and hadn't picked up on them the first time I checked. My fault entirely, everything is working fine now.

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