How do I grab random from array and display in text object

Get help using Construct 2

Post » Thu Jun 15, 2017 5:29 pm

I have an array with 3 names in it. I want the user to be able to click a button, and a text object will display a random name stored inside that array. I seem to be struggling with the grabbing random value and displaying it with a Text object. Help?

Thanks in advance.
B
14
S
4
G
1
Posts: 25
Reputation: 1,614

Post » Thu Jun 15, 2017 5:44 pm

text -> set_text -> array_name(random(0,2)

Use that.
Like my work? Fallow me on social media, and see my games on the arcade.
B
7
S
5
G
9
Posts: 298
Reputation: 5,367

Post » Thu Jun 15, 2017 5:48 pm

imhotep22 wrote:text -> set_text -> array_name(random(0,2)

Use that.


Thanks for the response. I've tried this one, but when I enter it and hit "Done", I get this error:

''arrayName" needs an expression after it
This is an object name, so you need a dot and an expression name after it.

Here is what I entered: arrayName(random(0,2))
B
14
S
4
G
1
Posts: 25
Reputation: 1,614

Post » Thu Jun 15, 2017 5:55 pm

Array.At(int(random(0,3))
will pick 0,1,2.
ImageImageImage
B
71
S
22
G
248
Posts: 3,761
Reputation: 138,073

Post » Thu Jun 15, 2017 5:57 pm

yeah i forgot to add at before the array call.

try blackhornet example it will work.
Like my work? Fallow me on social media, and see my games on the arcade.
B
7
S
5
G
9
Posts: 298
Reputation: 5,367

Post » Thu Jun 15, 2017 6:13 pm

blackhornet wrote:Array.At(int(random(0,3))
will pick 0,1,2.


Awesome. I stopped getting the error, but the only output on my button click is the text is set to 0. There is no variety, and the values inside the array aren't displayed.
B
14
S
4
G
1
Posts: 25
Reputation: 1,614

Post » Thu Jun 15, 2017 8:17 pm

You need to post what you are doing (CAPX).
ImageImageImage
B
71
S
22
G
248
Posts: 3,761
Reputation: 138,073

Post » Thu Jun 15, 2017 8:35 pm

blackhornet wrote:You need to post what you are doing (CAPX).


https://www.dropbox.com/s/eu6ojfyw3k77mo2/testing-name-generator.capx?dl=0
B
14
S
4
G
1
Posts: 25
Reputation: 1,614

Post » Thu Jun 15, 2017 8:39 pm

You want System.On start of layout!
ImageImageImage
B
71
S
22
G
248
Posts: 3,761
Reputation: 138,073

Post » Thu Jun 15, 2017 8:41 pm

blackhornet wrote:You want System.On start of layout!


*face palm* Right. Thanks for finding my stupid problem. Seriously. Thank you. XD
B
14
S
4
G
1
Posts: 25
Reputation: 1,614


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 21 guests