0 Favourites

# How do I Delete Duplicate 3D Arrays

• 16 posts
• Hello,

First time post, so far all my answers have been answered simply by going through the forums. My challenge is with manipulating 3D arrays.

I have a 3D array that is 50 in width, 1 high, and 3 depth. I am using this to store randomly generated coordinates before the layout has loaded. I am doing this to create an object without ever creating another object over top of one that has already been occupied when the layout gets generated using the array.

My current array setup:

-Depth 1 = "X" coordinate, -Depth 2= "Y" coordinate and "Z" stores the same value (for example "sprite1").

My dilemma:

I would like to loop through the entire array named "arrayMap" and remove all duplicates where index "x" depth 1 and depth 2 are duplicated.

For example:

Index 4, Depth 1 = 20, Depth 2 = 15, Depth 3 = "sprite1"

Index 32, Depth 1 = 20, Depth 2 = 15, Depth 3 = "sprite1"

Index 4 and Index 32 have the same values stored for an "X" and "Y" coordinate so I would like to remove the duplicate (while of course keeping one instance of it).

Hopefully my explanation makes sense. I did a lot of reading on arrays but my feeble brain gets lost as soon as nested loops and 3D arrays get involved.

I appreciate any help that can be offered as this will solve a ton of my "random generated" sprite issues that I currently have, not to mention make things a lot cleaner with less coding.

Cheers.

• Let me know if there is anything I can make more clear. I appreciate any help and time in advance. Was hoping this was an easy one so I could continue work on my project after work today.

Cheers

• Don't use a 3D for this. id beeing the id of the object.

Set id on (x=id,y=0)

set X coordinate on (x=id,y=1)

set Y coordinate on (x=id,y=2)

Set name on (x=id,y=3)

Now you can loop on the X-as and retrieve the values for (x, the y you need)

You can also delete a position on the x-as, deleting the Y values with it. Just simple with the delete action (for array).

Should be as easy as this.

> Conditions

> Array - For Each Element

>

> SubEvent Conditions

> System - While

> System - Compare two values - IndexOf(Array.At.CurX) != LastIndexOf(Array.At.CurX)

>

> Action

> Array - Delete LastIndexOf(Self.At.CurX)

>

>

This is for a 1d array, but it will still work if you add an additional sub event to check that the second value also matches in both.

• Example of using an array:

Should have posted this after your exams. But hey.

https://www.dropbox.com/s/ffbcqx2urfwig ... .capx?dl=0

• Hello,

First of all I really appreciate the time to write.

Secondly, I guess arrays flew over my head but looking at that example and your explanation I think I am on track now. Having said that, this is as far as I have got:

https://www.dropbox.com/s/0ec29x8wf5bdi ... 1.jpg?dl=0

Correct me if I am wrong, this will only delete duplicate values at the first "Y0" it sees that has been duplicated. I would like to modify this to look for when "Y0" AND "Y1" are both exactly the same.

• Update: Nope, spoke too soon, sorry for posting so much.

I think I may have figured it out, I am trying to validate the data. Maybe someone with more experience will be able to point it out before I have finished.

I changed the last "For each XY element" to:

https://www.dropbox.com/s/st05bvbfdvfgz ... 2.jpg?dl=0

• You are using your WaterMap array more like a database of locations (hence the possibility of duplicates) rather than a map to what is in your layout (or what will be).

I would structure it so that each cell in the map array represents (or "maps") to a tile on the layout. That way the array is two dimensional - just like the screen. If you know an x and y location for a tile, then you know its location in the array. That spot in the array can only hold one value - the type of tile at that location (water, or grass, or whatever).

I whipped up a sample that picks 50 random locations for water. As it picks locations, it first checks to see if that location already contains water. If it does, then it chooses another location. Otherwise it sets that location as water so it can't get picked again. (My sample also creates the water tile so we can see it on the screen. I know you want to create the map first, and then the actual layout later).

I added some mouse events at the end so you can click on tiles, and it will tell you what the map thinks is stored at that location. Or if you right-click anywhere, then it lists all locations containing water.

• I cannot wait until work is done to check this out, sometimes I think I took the wrong career path....aww well. Sounds like exactly what I have been trying to do. I truly appreciate the effort of creating a mini project for me, I will check it out and let you know. I was pulling out my hair last night trying to get the array to work.

Essentially what my plan for this is:

-Have a set amount of random bodies of water

-randomly select a location where no other tiles currently exist

-Create a random sized body of water with that information on a 32 snap grid

My next challenge after this is to find the outline of the water and replace it with the appropriate sprites so it looks more natural, something I will of course attempt before asking anyone here to help with.

Cheers!

• Well, I couldn't resist... I made a quick stab at creating lakes. It is VERY random, and does not try to prevent overlapping, or to intelligently place them in any way.

My son and I were working on random land generation last year, so this is a topic I am interested in. It gets fairly complex pretty quickly.

What we wanted was to be able to generate land in a similar way to how Minecraft does it. To do that, you need to use Perlin Noise. There is a plugin for C2 that works great: https://www.scirra.com/forum/plugin-noisejs_t93998

The nice thing about Noisejs is that once you find a setting that you like, you can use that seed number, and get the same results every time, or you can use a random seed, or let people share seeds they like with their friends. Plus you can create infinite layouts - when you have a seed, you just give it the coordinates and you can generate as much of the terrain around that point as you want.

if you want to see our random terrain, look here: http://www.rieperts.com/games/Goblin/index.html

Press ESC to regenerate the terrain. You can right-click on dirt to dig into the ground. Press F, G, or H to teleport to the left, middle or right of the map.

Hold Shift down to run, and press SpaceBar for a huge jump.

And here is the quick test I did at making lakes from the previous post (does not use the noise plugin): http://www.rieperts.com/games/forum/WaterMapLakes.capx

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

Construct 3 users don't see these ads
• Hello,

Ok, I have been trying to get this to work for hours. I easily managed to get your capx to work for me (thanks for that ;D). Now I have been trying to adopt it to store values of other sprites within the layout (this is all before the layout finishes loading). My last array I am working on is to loop through my entire layout and look for every sprite named "tree'. I thought it would be simple to do the following:

https://www.dropbox.com/s/fwgqcsp155qe2 ... 3.jpg?dl=0

All of the trees were created using "create object" at random coordinates (all of which are on 32 grid snap).

That project you made looks awesome btw.

• when you are adding the trees to the array, you are using their screen coordinate ( tree.x ) ...

just took another look, and you must have updated the image. try floor(tree.x/32) and floor(tree.y/32)

• Yeah I realized that after the fact lol.

I just went through and made sure that all of the trees were indeed on snap. All of the locations were randomized with x=round(random((0, LayoutWidth)*32)/32))

y=round(random((0, LayoutHeight)*32)/32))

The layout itself is a multiple of 32

Am I calling the sprite correctly? I need to be able to loop through all of them.

• yes, it looks like you are calling them correctly.

However, when the water is created, it is only looking for places that are not water - so it could put water over a tree...

• Update 1: It is looking like it doesn't actually see the objects until after the "on start of layout" section. It works when i throw it outside of that which is after the layout has loaded.

Strange, the json is not showing any instances of trees even though I did it correctly.

I am not too concerned about the water overlapping just yet. I am going to put the water in first, block that off so nothing can spawn there, then spawn the trees after. Right now I just want to make sure I have something that works when trying to store other sprites that I already "created" during the before load section. I have other resources that I would like to also store to the array. If i can at least get the tree sprites to work the rest should be easy.

• when you create objects, the instances can not accessed until the next top level event. (that is why they can be accessed outside the On Start of layout).

But they can be accessed in the section where they are created, so you could insert them into the array as they are created... (which is what I was doing when the water is created).

• 16 posts