How do I Delete Duplicate 3D Arrays

Get help using Construct 2

Post » Mon Jun 27, 2016 4:57 am

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.
B
35
S
16
G
3
Posts: 16
Reputation: 4,109

Post » Mon Jun 27, 2016 8:00 pm

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
B
35
S
16
G
3
Posts: 16
Reputation: 4,109

Post » Mon Jun 27, 2016 11:01 pm

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.
oosyrag wrote:From the other thread -

oosyrag wrote: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.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Mon Jun 27, 2016 11:03 pm

Example of using an array:

99Instances2Go wrote:Should have posted this after your exams. But hey.

https://www.dropbox.com/s/ffbcqx2urfwig ... .capx?dl=0
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Tue Jun 28, 2016 2:16 am

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.
B
35
S
16
G
3
Posts: 16
Reputation: 4,109

Post » Tue Jun 28, 2016 2:59 am

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
B
35
S
16
G
3
Posts: 16
Reputation: 4,109

Post » Tue Jun 28, 2016 6:26 am

@Maydie
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.

capx: http://www.rieperts.com/games/forum/WaterMap.capx
B
99
S
35
G
20
Posts: 438
Reputation: 17,910

Post » Tue Jun 28, 2016 3:51 pm

@AllanR

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!
B
35
S
16
G
3
Posts: 16
Reputation: 4,109

Post » Tue Jun 28, 2016 7:06 pm

@Maydie
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
B
99
S
35
G
20
Posts: 438
Reputation: 17,910

Post » Wed Jun 29, 2016 2:46 am

@AllanR

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.
B
35
S
16
G
3
Posts: 16
Reputation: 4,109

Next

Return to How do I....?

Who is online

Users browsing this forum: max2612 and 30 guests