Random Generated Arrays

Get help using Construct 2

Post » Wed Aug 07, 2013 2:49 pm

Hi Folks! Sorry if this is an obvious fix, but ive been going crazy trying to figure it out.

I want to create a very simple system where the user will click a button and it will generate one of many pre-set text values. For example, an array with various names in each X value, then when the text is clicked it will randomly generate one of the values, and display one of the names.

I cant seem to figure out how to make the system generate a random X, then set the text to the value of the X that i have pre-set.....does that make sense?

Thx!

B
4
Posts: 9
Reputation: 394

Post » Wed Aug 07, 2013 3:41 pm

Assuming you have array with text values pushed into it here.

On button pressed
.. set text to array.at(floor(random(0,array.width)))

random(0,array.width) generates a number that we sue to index the array. That number is between 0 and amount of values in the array.

The floor() around that rounds it down, so we can use it for indexing without going off boundaries.

The array.at() indexes the array at index defined inside the brackets.
B
24
S
9
G
7
Posts: 756
Reputation: 7,292

Post » Wed Aug 07, 2013 4:46 pm

vee41 you are the MAN!!

That worked really well man thx! Just one lil prob still tho, it seems to drop in a number value of "0" on occasion. I have set a text value for "0" on the X, but it reads it as "0" instead of the text value i set, would you know why that is?


here is an example of the problem...

https://dl.dropboxusercontent.com/u/51560464/Random%20Array/Random%20Array.capx



THX again though vee41
B
4
Posts: 9
Reputation: 394

Post » Wed Aug 07, 2013 5:29 pm

It seems that size of your array is 10, so there are 'undefined' values in the array. That is probably the cause, click on your array object and you can set the initial size of the array at left hand menu. :)

A bit more 'dynamic' way to do this is set the initial size of the array to 0, and use 'Push to X axis' action instead of set value. That way you don't have to worry about setting the array size manually, as you are basically filling up and empty array so it won't have any blank values.
B
24
S
9
G
7
Posts: 756
Reputation: 7,292

Post » Wed Aug 07, 2013 5:45 pm

Yup! That did it! Thanks for the help and advice vee41.
B
4
Posts: 9
Reputation: 394

Post » Wed Aug 07, 2013 5:59 pm

Thanks @vee41 you helped me out too!
B
15
S
6
G
3
Posts: 87
Reputation: 4,304

Post » Wed Sep 04, 2013 5:29 pm

Hey @vee41

I was wondering if it was possible to always match a randomised name with it's meaning.

So when you press the button it will randomise a name but will always generate the same meaning for that particular name.

Iv got 2 arrays. One for Names and one for Meanings. Is it possible to connect the Meanings to the Names sequentially? So the name and meaning sequence matches each other. :D

Capx


NECROKRIEG2013-09-04 17:29:59
B
15
S
6
G
3
Posts: 87
Reputation: 4,304

Post » Thu Sep 05, 2013 8:09 am

The way you set it up, you could achieve that with these minor changes:

On button pressed
-> set Name to aName.at(floor(random(0,aName.width)))
-> set Meaning to aMeaning.at(aName.IndexOf(Name.Text))

The meaning basically checks what is in the text field, finds it's index and looks for description for that index. This assumes that name at index 2 has it's description at index 2 etc.
B
24
S
9
G
7
Posts: 756
Reputation: 7,292

Post » Thu Sep 05, 2013 2:21 pm

@Vee41, that helped me out again man!
B
4
Posts: 9
Reputation: 394

Post » Thu Sep 05, 2013 6:36 pm

Thanks @Vee41 it works a treat! I understand the logic of the expression, it's quite nifty! You guys should come to Scirra's Meetup Event if you're in London!
B
15
S
6
G
3
Posts: 87
Reputation: 4,304

Next

Return to How do I....?

Who is online

Users browsing this forum: blissfulwizard, DevinMurray and 15 guests