How do I fill an array with tilemap tiles coordinates?

0 favourites
  • 8 posts
From the Asset Store
Each tile 64 by 64 pixels - Ultimate Tile Set for your awesome games
  • Hi all,

    I`m trying to locate all non-empty tiles on a tilemap and insert their ID, X (in pixels) and Y (in pixels) into an array, but not all of the tiles data appear in the array (red frame on the screenshot below).

    I log the same data in browser console and everything is ok (green frame on the screenshot below).

    Please share your ideas on how to fix this. Thnx!

    Dropbox link to my capx file: https://www.dropbox.com/s/jxuhx6p7ma1sewo/waypointsTest.capx?dl=0

    Tagged:

  • You are Looping throughout the Whole TileMap Which has 20x20 = 400 Tiles but your Array size its only 10x3

    also, it's not very clear how you trying to save you Logic as to save on the exact coordinates that match the TileMap you need to use 2D Coordinates Example Array.At(X,Y)

    then save the (TileID, X Coordinates, Y Coordinates) on the same Array Location separated by commas

    Or

    If you don't really need to match the Array.At(X,Y) to the TileMap Tile(X,Y) then you can set the Array Size at the start of the layout to Size = (Width =0, Height =3)

    Then Push Back on the X = Tilemap.TileAt(loopindex("x"), loopindex("y"))

    Then Set (Same X, 1) = Tilemap.TileToPositionX(loopindex("x"))

    Then Set (Same X, 2) = Tilemap.TileToPositionY(loopindex("y"))

    Like you were attempting to do, so you End up with

    Array X all the Tiles Number

    Array Y = 1 >>>All the X Positions

    Array Y = 2 >>>All the Y Positions

  • You are Looping throughout the Whole TileMap Which has 20x20 = 400 Tiles but your Array size its only 10x3

    also, it's not very clear how you trying to save you Logic as to save on the exact coordinates that match the TileMap you need to use 2D Coordinates Example Array.At(X,Y)

    then save the (TileID, X Coordinates, Y Coordinates) on the same Array Location separated by commas

    Or

    If you don't really need to match the Array.At(X,Y) to the TileMap Tile(X,Y) then you can set the Array Size at the start of the layout to Size = (Width =0, Height =3)

    Then Push Back on the X = Tilemap.TileAt(loopindex("x"), loopindex("y"))

    Then Set (Same X, 1) = Tilemap.TileToPositionX(loopindex("x"))

    Then Set (Same X, 2) = Tilemap.TileToPositionY(loopindex("y"))

    Like you were attempting to do, so you End up with

    Array X all the Tiles Number

    Array Y = 1 >>>All the X Positions

    Array Y = 2 >>>All the Y Positions

    Thanks for the push into array hint, it helped me, man. The only moment is I had to use push forward instead of push back, but anyway I`m satisfied with the result, though I am not quite sure about how it works, to my shame.

    Edited:

    Result:

  • Thanks for the push into array hint, it helped me, man. The only moment is I had to use push forward instead of push back, but anyway I`m satisfied with the result, though I am not quite sure about how it works, to my shame.

    No Problem mate,

    Are you sure that is working Properly?

    -Because after you Push you are using (Array.CurX) which is meant to work only with Array "For Each

    Element" if you use Push Front you can use Directly ArrayX = 0 probably that's why it worked for you as you don't use the "For Each Element" so when you use Array.CurX it will be always at = 0

    -Edited

    -And also you are storing Twice (TileToPositionX) Instead of Y

  • > Thanks for the push into array hint, it helped me, man. The only moment is I had to use push forward instead of push back, but anyway I`m satisfied with the result, though I am not quite sure about how it works, to my shame.

    >

    No Problem mate,

    Are you sure that is working Properly?

    -Because after you Push you are using (Array.CurX) which is meant to work only with Array "For Each

    Element" if you use Push Front you can use Directly ArrayX = 0 probably that's why it worked for you as you don't use the "For Each Element" so when you use Array.CurX it will be always at = 0

    -Edited

    -And also you are storing Twice (TileToPositionX) Instead of Y

    My intention was to make a path for an object step by step which begin from ID0 tile and continue along ID1, ID2, ID3, ID4 etc.

    See my new capx with a path and an object: dropbox.com/s/vq1ikzdwyfgfsek/waypointsTest2.capx

    It`s moving as it supposed to, even if I`m using (TileToPositionX) twice, actually if I change the second one to (TileToPositionY) nothing changes, atm I don`t have time to dig it, maybe in the future if it will cause a bug :-)

  • > > Thanks for the push into array hint, it helped me, man. The only moment is I had to use push forward instead of push back, but anyway I`m satisfied with the result, though I am not quite sure about how it works, to my shame.

    > >

    >

    > No Problem mate,

    >

    > Are you sure that is working Properly?

    > -Because after you Push you are using (Array.CurX) which is meant to work only with Array "For Each

    > Element" if you use Push Front you can use Directly ArrayX = 0 probably that's why it worked for you as you don't use the "For Each Element" so when you use Array.CurX it will be always at = 0

    >

    > -Edited

    >

    > -And also you are storing Twice (TileToPositionX) Instead of Y

    My intention was to make a path for an object step by step which begin from ID0 tile and continue along ID1, ID2, ID3, ID4 etc.

    See my new capx with a path and an object: dropbox.com/s/vq1ikzdwyfgfsek/waypointsTest2.capx

    It`s moving as it supposed to, even if I`m using (TileToPositionX) twice, actually if I change the second one to (TileToPositionY) nothing changes, atm I don`t have time to dig it, maybe in the future if it will cause a bug :-)

    Ho I see,

    you were on the good track then

    I still breaking my head how that (Tilemap.TileToPositionX(loopindex("Y"))) is giving the Right (Y-Coordinates) lol I gave up maybe another Day I will try again :), If anyone has any Ideas please share. As I would spect to give all the Y-Coordinates wrong like on this Example:

    https://www.dropbox.com/s/2zccpv4qk0rbx12/1-Debug.capx?dl=0

    If you click on any Tile it will give you the Correct Coordinates at the Top-Left of the Screen, as you can see if we use (Tilemap.TileToPositionX(loopindex("Y"))) it will give all Wrong Y-Coordinates

    unless you change it to (Tilemap.TileToPositionY(loopindex("Y"))) how it should be.

    Anyway I made a minor modification to your Capx at the Moment you calling "movePlayer" and loop throughout the whole Array each Time until it finds the Matching WayPoint, you could save the Looping but just calling the Waypoint Matching the Array Index you just have to sort out the Array.

    Example

    Array X=0 >>>>Waypoint 0

    Array X=1 >>>>Waypoint 1

    Array X=2 >>>>Waypoint 2

    Etc....

    You can do this Array sort out easy because you are matching the (Waypoint = TileID) so you just have to sort out the Array on the X axis

    https://www.dropbox.com/s/7403uxmfir225ip/fill%20an%20array%20with%20tilemap%20tiles%20coordinates.capx?dl=0

    Good luck and have Fun

  • Update:

    Finally, I saw some Light and understood why is working the (Tilemap.TileToPositionX(loopindex("Y")))

    It's because your Map it's perfectly aligned on the Grid and the Origin Point of the TileMap (0,0) also the Tiles are symmetric 64x64

    So even that the true it Loops Retrieving the X-Coordinates Of the Tiles using the Index of Loopindex("Y") but because the Tiles are symmetric and Both Indexes start at 32

    Example:

    (Tilemap.TileToPositionX(loopindex("Y=0"))) >>>>> 32px at the X Cordinates

    (Tilemap.TileToPositionX(loopindex("Y=1"))) >>>>> 96px at the X Cordinates

    (Tilemap.TileToPositionX(loopindex("Y=2"))) >>>>> 160px at the X Cordinates

    (Tilemap.TileToPositionX(loopindex("Y=3"))) >>>>> 224px at the X Cordinates

    Etc.....

    Will be the same as if you used TileToPositionY

    (Tilemap.TileToPositionY(loopindex("Y=0"))) >>>>> 32px at the Y Cordinates

    (Tilemap.TileToPositionY(loopindex("Y=1"))) >>>>> 96px at the Y Cordinates

    (Tilemap.TileToPositionY(loopindex("Y=2"))) >>>>> 160px at the Y Cordinates

    (Tilemap.TileToPositionY(loopindex("Y=3"))) >>>>> 224px at the Y Cordinates

    Etc...

    This Time you been Lucky as the TileMap was aligned to the Grid :)

    But if the TileMap wasn't Aligned to the Grid Example:

    TileMap Position at (250, 92)

    and the First Tile Coordinates will be at (X=282, Y=124)

    So if you use the same System as you did to Retrieve the Y-Coordinates

    (Tilemap.TileToPositionX(loopindex("Y=0"))) >>>>> 282 px at the X Cordinates (Wrong)

    Vs

    (Tilemap.TileToPositionY(loopindex("Y=0"))) >>>>> 124 px at the Y Cordinates (Correct)

    as you can see the Results can be very very wrong, Conclusion the sooner you start using the correct expressions the fewer mistakes and headache you will have in the future, even if it did work this Time swapping the Loopindexs is not guarantee it will work next Time, but if you use always the correct Expressions you will have peace

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Update:

    Finally, I saw some Light and understood why is working the (Tilemap.TileToPositionX(loopindex("Y")))

    It's because your Map it's perfectly aligned on the Grid and the Origin Point of the TileMap (0,0) also the Tiles are symmetric 64x64

    So even that the true it Loops Retrieving the X-Coordinates Of the Tiles using the Index of Loopindex("Y") but because the Tiles are symmetric and Both Indexes start at 32

    Example:

    (Tilemap.TileToPositionX(loopindex("Y=0"))) >>>>> 32px at the X Cordinates

    (Tilemap.TileToPositionX(loopindex("Y=1"))) >>>>> 96px at the X Cordinates

    (Tilemap.TileToPositionX(loopindex("Y=2"))) >>>>> 160px at the X Cordinates

    (Tilemap.TileToPositionX(loopindex("Y=3"))) >>>>> 224px at the X Cordinates

    Etc.....

    Will be the same as if you used TileToPositionY

    (Tilemap.TileToPositionY(loopindex("Y=0"))) >>>>> 32px at the Y Cordinates

    (Tilemap.TileToPositionY(loopindex("Y=1"))) >>>>> 96px at the Y Cordinates

    (Tilemap.TileToPositionY(loopindex("Y=2"))) >>>>> 160px at the Y Cordinates

    (Tilemap.TileToPositionY(loopindex("Y=3"))) >>>>> 224px at the Y Cordinates

    Etc...

    This Time you been Lucky as the TileMap was aligned to the Grid :)

    But if the TileMap wasn't Aligned to the Grid Example:

    TileMap Position at (250, 92)

    and the First Tile Coordinates will be at (X=282, Y=124)

    So if you use the same System as you did to Retrieve the Y-Coordinates

    (Tilemap.TileToPositionX(loopindex("Y=0"))) >>>>> 282 px at the X Cordinates (Wrong)

    Vs

    (Tilemap.TileToPositionY(loopindex("Y=0"))) >>>>> 124 px at the Y Cordinates (Correct)

    as you can see the Results can be very very wrong, Conclusion the sooner you start using the correct expressions the fewer mistakes and headache you will have in the future, even if it did work this Time swapping the Loopindexs is not guarantee it will work next Time, but if you use always the correct Expressions you will have peace

    Wow, you`re a real nutcracker. At the moment this is the best solution for me to create a path on a tilemap.

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