Set id for variables in array

This forum is currently in read-only mode.
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Hi.

    I have some problems with array. There is one-dimensional array with 10 variables. Variables are random and they change every tick. Is there any way to set up id for each of the variable? In result I need situation in which randomness of the variables will not affect the id number and id 1 will always be the lowest, id 2 ? second lowest and so on.

    Thanks.

  • I'm not sure I understand your question... what is wrong with array indexes?

    The first array cell is always at 0 index, the second at 1 and so on until the last cell with index of 9.

  • 0--1--2--3--4--5--6--7--8--9     Index

    -----------------------------

    56-82-31-66-23-15-08-42-11-18    Variable (random)

    08-10-06-09-05-03-01-07-02-04    ID

    On second tick variables will change their values in random order.

    0--1--2--3--4--5--6--7--8--9     Index

    -----------------------------

    89-61-49-92-35-37-86-39-12-64    Variable (random)

    09-06-05-10-02-03-08-04-01-07    ID

    ID of variables will change according to values, but variables would not change their position in array.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 0--1--2--3--4--5--6--7--8--9     Index

    -----------------------------

    89-61-49-92-35-37-86-39-12-64    Variable (random) ArrayA

    12-35-37-39-49-61-64-86-89-92    Variable (sorted) ArrayB

    09-06-05-10-02-03-08-04-01-07    ID ArrayID

    + ArrayB: For each element

    ++ ArrayA: For each element

    +++ ArrayA: Value at ArrayA.CurrentX Equal to ArrayB.CurrentValue

    ---> ArrayID: Set index ArrayB.CurrentX to ArrayA.CurrentX

    1) Array indices in Construct are 1-based (first index is 1)

    2) Copy ArrayA to ArrayB after having generated the random numbers and sort ArrayB. A simple bubble sort algorithm will do.

    3) After setting ArrayID, it will look exactly as pictured above. You now have a double reference (both index and ID) and can find either the ID from a value, a value from an ID, an index from an ID, an ID from an index, an index from a value or a value from an index. Just peek at the right positions of the appropriate arrays.

    4) The events posted above will only work, if the random generated numbers are unique! 3-1-7-4 will work, 3-3-4-4 will not.

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