Random Generated Arrays

0 favourites
  • 10 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
  • 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!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.

  • 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...

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

    THX again though vee41

  • 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.

  • Yup! That did it! Thanks for the help and advice vee41.

  • Thanks vee41 you helped me out too!

  • 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

  • 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.

  • vee41, that helped me out again man!

  • 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!

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