How do I push a value on X,Y into array ?

0 favourites
  • 6 posts
From the Asset Store
Push the Box Puzzle Game likes Sokoban made with Illustrator
  • HI !

    I'd like to use an array to get multiple values on a 2 dimensional map.

    I know I can set a single X,Y value : Set value at(X,Y) to (value)

    or eventually several values on X,Y if I already have a Z value, like : Set value at (X,Y,Z)

    But I'd like the Z value to be auto-incremental

    as we should do in PHP

    array[x][y][] = value 1

    array[x][y][] = value 2

    etc...

    resulting in values at

    [x][y][0] = first value on X,Y

    [x][y][1] = second value on X,Y

    So you can stack as many values as you want.

    But the push and insert functions of Construct 2 just allows to "inserts a new 2D plane of elements", resulting in multiple insertions, and I just want to add one by one.

    Is the only way is to count all the X,Y values so far then guess what my next Z value will be ?

    Or eventually generate a random Z value big enough so matching will be (almost) impossible ? (wrong idea)

    I hope I explained my problem clearly enough and that someone can help me with this !

    Best

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You can use a loop and set your value/position as the loopindex.

    This will allow insertions one by one, but all at once?

    It's really hard to give an exact answer since you haven't really explained what you're trying to do... maybe a dictionary would be better? It really depends!

    ~Sol

  • I understood in the end that arrays doesent work as in PHP since they need to be already defined.

    I had to make a record of some people being present in some places at some time.

    In PHP i would have done :

    array[place][time][0] = first person

    array[place][time][1] = second person

    and so on

    In C2, the tricky thing is that you can't use text keys in your array. So I had to set up an ID for each place and each character. In the end my array works like that

    x = place ID

    y = time

    z = character ID

    value = 1 if character is there, 0 if not.

    Don't think there's an easy way to do it with dictionnary

  • Still not sure what your problem is. Your X, Y, and Z Indexes are already incremental by nature, and don't have to be fixed, the way you're using the array. To add a character, push 0 on the Z axis, then you will get a new Z layer full of 0s. The character ID would correlate to the Z index - charID 0 would be the top layer, charID 1 would be second, ect.

    Then to set the marker, you would do Set Array at (Time, Place, CharID) to 1.

    Edit: Seems like a pretty inefficient way to use an array to me... I would set up a 2d array, width (character count), height 2, depth 1. The first row would be the time, second row would be place, and the x index would be the character ID. You can use push front/back on x axis to add characters, then set the values at Array.At(0,0) and (0,1) for time and place. (Or Array.At(Array.Width-1,0) and (Array.Width-1,1) for the back)

    I think I'm still misunderstanding your question.

  • My explanation may be a bit confused I admit

    "Then to set the marker, you would do Set Array at (Time, Place, CharID) to 1."

    That's basically what I'm doing now. Yes I have set the size of array already but you're right I can change its size on demand. However I don't need this since I already know how much values I will store.

    In the end it's working just fine so ... problem solved

    I see your point in the second solution, and that's true I didn't think about using two arrays in C2 ! but all in all I don't find that using 2 arrays sounds more simple and or efficient because the only way it would have been more simple, would be a case I don't need to use IDs but strings.

    Thanks for your help !

  • The problem is that in PHP, arrays can be jagged. Multidimensional arrays in that language are basically arrays of arrays. Each array can hold another array of variable length.

    In C2, multidimensional arrays are square (or cubic). "Pushing" doesn't make much sense in this context, it's more like "add row/column" (2d) or "add plane" (3d).

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