How do I make variable text?

Just started using Construct 2? Post your questions here

Post » Sat Apr 26, 2014 12:57 pm

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.
B
5
S
2
Posts: 53
Reputation: 792

Post » Sat Apr 26, 2014 1:24 pm

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.
B
19
S
7
Posts: 161
Reputation: 1,751

Post » Sat Apr 26, 2014 1:45 pm

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?
B
5
S
2
Posts: 53
Reputation: 792

Post » Sat Apr 26, 2014 2:08 pm

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.
B
19
S
7
Posts: 161
Reputation: 1,751

Post » Sat Apr 26, 2014 2:21 pm

Works perfectly! Thanks very much - greatly appreciated.
B
5
S
2
Posts: 53
Reputation: 792

Post » Sat Apr 26, 2014 2:40 pm

Glad it worked ^_^ and good luck!
B
19
S
7
Posts: 161
Reputation: 1,751

Post » Sun Apr 27, 2014 8:06 am

else wrote:...
. 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")
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
33
G
47
Posts: 1,194
Reputation: 30,832

Post » Sun Apr 27, 2014 9:12 am

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. :)
B
5
S
2
Posts: 53
Reputation: 792

Post » Wed Apr 30, 2014 7:59 pm

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?
B
5
S
2
Posts: 53
Reputation: 792

Post » Thu May 01, 2014 12:42 am

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.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

Next

Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 1 guest