0 Favourites

How to find X of an Array?

  • I'm still a beginner with Arrays, but I was wondering how to pin point X based on a selected value?

    Example, if i need the X value of "58" it would be 5

    This 58 will also be sorted vertically, so I need a "real-time" value if that makes sense?

    Would it be Array.CurX? or something?

  • CurX is only for looping through the entire array.

    If you know your index you would reference it like Array.At(5)

    but if you are trying to find the row based on a value, you need to loop through the whole array and look for it. So you would use CurX and CurY (because your array above is 10x10). You have to look through on each row (x) and then in that row each column (y).

    Think of arrays like excel spreadsheets.. the rows are X and the columns are Y

    If there's more than 1 "58" in your table this loop will execute the action(s) for each "cell" that equals 58.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • maybe I'm backwards, but isn't it more like.

    (7,5) = (x,y) = 58 = (Array.curX, Array.curY)

    so the x value of 58 is 7

  • Oh right, sorry haha i got it mixed up! yes should be Y = 5

  • Man, this whole array stuff is soooo confusing

    Hmm, lemme try another example.....

    So, if I want to set (7,5) to "5" (which is basically its Y axis)

    What expression would I put down?

    something like..... Array | Set Value at (7,5) to..."Array.CurX"??

  • click action, select array, click 'set at xy', fill out the form.

    the tricky part is figuring out where to put the action, which will just depend on your specific game.

  • so you would make your array like this:

  • Just figured it out, thanks!!!

  • maybe I'm backwards, but isn't it more like.

    (7,5) = (x,y) = 58 = (Array.curX, Array.curY)

    so the x value of 58 is 7

    the x is always the row.. so it really depends on the data and how you format it. If it's laid out like Excel, the X is horizontal.. that's how it is in other programming languages. With C2 it's weird to me how they say Y is height or rather horizontal rows.

    I think of arrays storing data like this:

    John   123  y 
    Mary   392  n
    Fred   495  y[/code:1qsr2m7k]
    
    instead of this:
    
    [code:1qsr2m7k]John Mary Fred
    123  392   495
    y    n     y[/code:1qsr2m7k]
  • Man, this whole array stuff is soooo confusing

    Hmm, lemme try another example.....

    So, if I want to set (7,5) to "5" (which is basically its Y axis)

    What expression would I put down?

    something like..... Array | Set Value at (7,5) to..."Array.CurX"??

    it's not a "Y axis", once your array has more than 1 column you need X/Y coordinates... if it just has 1 column, you only need X. You would need to do: Array.At(7,5) = "5". The CurX is the loop variable, meaning it starts at 0 and increments by one with each loop - up until there are no more array elements.

    with one column:

    here you use only X

    Array [0] = "john"
    Array [1] = "fred"
    Array [2] = "mary"
    [/code:13h8jrcr]
    
    with more columns... (first name, last name, age)
    here you use X and Y
    [code:13h8jrcr]Array [0][0] = "john"
    Array [0][1] = "smith"
    Array [0][2] = 25
    
    Array [1][0] = "fred"
    Array [1][1] = "smith"
    Array [1][2] = 21
    
    Array [2][0] = "mary"
    Array [2][1] = "smith"
    Array [2][2] = 28[/code:13h8jrcr]
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)