Arrays

For questions about using Classic.

Post » Fri Jul 02, 2010 6:49 pm

It's the "For each element". For each cycles each z each pass, so basically its running z2 twice.
Better to do a regular loop here.
Image Image
B
161
S
48
G
91
Posts: 7,359
Reputation: 67,273

Post » Fri Jul 02, 2010 7:32 pm

Thanks newt I had a feeling it was doing something like that.

Can you or anyone help with creating the checking loop? I have tried but however I structure it, it seems to be infinite and just keeps spawning objects. I also downloading an example but that seemed to loop infinitely too.

But I would expect that as I've never used loops before. Construct is great for learning new stuff.

Thanks.

:)
B
46
S
23
G
7
Posts: 459
Reputation: 6,763

Post » Fri Jul 02, 2010 8:14 pm

Ok one thing is you were checking x,and y coords, but you need to check the value at x,y,and z.
As well as set the value at x,y,and z.
Also your using an older version this cap wont work for you.
[url:y28wftyl]http://dl.dropbox.com/u/666516/brbarray.cap[/url:y28wftyl]
Sorry Im using the latest unstable, but here's the code if you don't want to download.
btw the value's in the array are set to zero by default(if you didn't already know that).
[code:y28wftyl]+ System: Start of layout
-> Array: Clear array with 0
-+ System: Repeat 5 times
+ System: Repeat 5 times
--> Array: Set index (random(5)+1, random(5)+1, 1) to 1
--+ System: For "x" from 1 to 10
---+ System: For "y" from 1 to 10
----+ Array: Value at (LoopIndex("x"), LoopIndex("y"), 1) Equal to 1
------> System: Create object Sprite on layer 1 at (LoopIndex("x")*10-5, LoopIndex("y")*10-5)
----+ Array: Value at (LoopIndex("x"), LoopIndex("y"), 2) Equal to 0
------> System: Create object Sprite3 on layer 1 at (LoopIndex("x")*10-5, LoopIndex("y")*10-5)[/code:y28wftyl]
Image Image
B
161
S
48
G
91
Posts: 7,359
Reputation: 67,273

Post » Fri Jul 02, 2010 9:42 pm

Thanks again newt.

I've updated to the newest unstable build because when I remade your example it didn't work, I'm guessing the Array Object was bugged in 0.99.82 or whatever version I was using.

I can't see this being the last time I'm going to need help with loops though :).

Thanks again
B
46
S
23
G
7
Posts: 459
Reputation: 6,763

Post » Sat Jul 03, 2010 5:33 am

Python lists work very well, if you aren't afraid of using Python. You could set a few methods to easily modify the array and retrieve information and then leave the rest in backend.
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155

Post » Sat Jul 03, 2010 6:34 am

Speaking of Python, it also has array objects that can be imported via the internal array module or the external NumPy module. It may be a bit more than you'd want to bite off right now, though. The 3-D array that you're using should work fine, too.

Don't short-sell your abilities, Minor. I've considered doing this sort of procedural generation in the past, but the idea scared me away before I even got started. ;)

Anyway, just because I feel compelled to point it out, this application practically begs for bitwise operations. Unfortunately, I don't see any support for them in Construct, other than through Python.

Basically, you can just use a 2-D list or array which contains integer values. Each integer value can have different bits set for any given on/off or true/false state that you need to track. For instance, you can keep the status of bordering land in the four cardinal directions in four bits. Expressing the values in binary notation (with 0b prefix) makes it more obvious what bits are set.

0b0001 (if land to north)
0b0010 (if land to south)
0b0100 (if land to west)
0b1000 (if land to east)

You can set the bits with the OR operator (|):

0b0001 | 0b0100 ( = 0b0101)

and test them with the AND operator (&):

0b0111 & 0b0100 ( = 0b0100, testing for bit 3 set)

Bah, too much to go into here, really. Feel free to ignore the whole bitwise thing... ;) Mostly just me reminiscing about 8-bit computer programming practices that aren't widely used anymore.

Keep up the good work. I like your creations so far.
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Post » Sat Jul 03, 2010 10:44 am

[quote="Mipey":2auq25a2]Python lists work very well, if you aren't afraid of using Python. You could set a few methods to easily modify the array and retrieve information and then leave the rest in backend.[/quote:2auq25a2]

Yep I'm very afraid of python Mipey, too many brackets & stuff. Proper written programming languages and me don't mix, I have tried since the late 80's but nothing ever sticks. The only written language I ever got a decent grasp of was AMOS on the Amiga, and when I say decent I mean it went further than

10 print "Hello"
20 goto 10

[quote="Silent Cacophony":2auq25a2]Speaking of Python, it also has array objects that can be imported via the internal array module or the external NumPy module. It may be a bit more than you'd want to bite off right now, though. The 3-D array that you're using should work fine, too.

Don't short-sell your abilities, Minor. I've considered doing this sort of procedural generation in the past, but the idea scared me away before I even got started. ;)

Anyway, just because I feel compelled to point it out, this application practically begs for [url=<a style="color: rgb(255, 0, 0);" class="linkification-ext" href="http://en.wikipedia.org/wiki/Bitwise_operation" title="Linkification: http://en.wikipedia.org/wiki/Bitwise_operation">http://en.wikipedia.org/wiki/Bitwise_operation</a>]bitwise operations[/url]. Unfortunately, I don't see any support for them in Construct, other than through Python.

Basically, you can just use a 2-D list or array which contains integer values. Each integer value can have different bits set for any given on/off or true/false state that you need to track. For instance, you can keep the status of bordering land in the four cardinal directions in four bits. Expressing the values in binary notation (with 0b prefix) makes it more obvious what bits are set.

0b0001 (if land to north)
0b0010 (if land to south)
0b0100 (if land to west)
0b1000 (if land to east)

You can set the bits with the OR operator (|):

0b0001 | 0b0100 ( = 0b0101)

and test them with the AND operator (&):

0b0111 & 0b0100 ( = 0b0100, testing for bit 3 set)

Bah, too much to go into here, really. Feel free to ignore the whole bitwise thing... ;) Mostly just me reminiscing about 8-bit computer programming practices that aren't widely used anymore.

Keep up the good work. I like your creations so far.[/quote:2auq25a2]

Thanks encouragement and for the info :). Bitwise does seem like something that would make what I'm trying to do a bit easier.

Right now on with an Array-Loopy question.

How would you go about extending a loop? for example: Below is my Array #=sea 1=land

[color=#0000FF:2auq25a2]#####
##[/color:2auq25a2][color=#008000:2auq25a2]11[/color:2auq25a2][color=#0000FF:2auq25a2]#
##[/color:2auq25a2][color=#008000:2auq25a2]1[/color:2auq25a2][color=#0000FF:2auq25a2]##
#####
#[/color:2auq25a2][color=#008000:2auq25a2]11[/color:2auq25a2][color=#0000FF:2auq25a2]##
#[/color:2auq25a2][color=#008000:2auq25a2]1[/color:2auq25a2][color=#0000FF:2auq25a2]###
#####[/color:2auq25a2]


I want to scan the array to find land that is connected to another piece of land and give each the same number in a "z" index/cell/slot/thingy. Then find the next land unconnected to the first and find the land connected to that land etc. So my array above would look like this:

[color=#0000FF:2auq25a2]#####
##[/color:2auq25a2][color=#008000:2auq25a2]11[/color:2auq25a2][color=#0000FF:2auq25a2]#
##[/color:2auq25a2][color=#008000:2auq25a2]1[/color:2auq25a2][color=#0000FF:2auq25a2]##
#####
#[/color:2auq25a2][color=#008000:2auq25a2]22[/color:2auq25a2][color=#0000FF:2auq25a2]##
#[/color:2auq25a2][color=#008000:2auq25a2]2[/color:2auq25a2][color=#0000FF:2auq25a2]###
#####[/color:2auq25a2]


I know how to use the array to check neighbours as I use that for the Cellular automata part of the project. It's just looping through grouping them via a number per land mass and finishing the loop without it going on for ever.

Does that make any sense?

Thanks again for all the help. I really am learning stuff here that I never thought I'd learn :).
B
46
S
23
G
7
Posts: 459
Reputation: 6,763

Post » Sat Jul 03, 2010 8:54 pm

That's getting into more difficult territory. If Construct's image manipulator or canvas had a flood fill, it would be fortunate, because they could be used as helpers for this. Alas... it is not so.

The simplest way that i can think of to do that would be like so, in my own weird pseudocode:

[code:2bx2ys0n]untouched = -1 # needs to b a value different than the number 1 and above
water = 0
z = 1 # setting this as static outside the loop makes the routine operate on the array as if it were a 2-d array, only at the z depth set.


func flood_fill(array, x, y, z, fill, border):
# Fills all connected cells surrounded by border, starting at array(x, y, z), with the value in fill.
# Some flood fills use connected cells of same value instead of using a border.
# flood fill algorithms can be 4-directional or 8-directional (allows diagonals to be considered connected)
... a bunch of code here ...


count = 0
for y = 1 to array.y_size:
for x = 1 to array.x_size:
if array(x,y,z) = untouched:
count = count + 1
flood_fill(array, x, y, z, fill=count, border=water)[/code:2bx2ys0n]

I've not programmed a flood fill routine before. It should certainly be possible to implement as a function in Construct, but would certainly be easier in Python. Python doesn't work well with Construct's Array object, but that's possible, too.

I'd probably look for a non-recursive algorithm using a stack to work from, since it looks as if another Construct array could be suitable for use as a stack.

Maybe someone else will have an easier idea. :roll:
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Post » Sat Jul 03, 2010 9:22 pm

Thanks for the info Silent Cacophony.

I'm starting to think this is way too complicated for me at the minute. Best go back with something a little more simple regarding Arrays & Loops, I was on a bit of a roll and started to get ahead of myself.

Still I have learned a lot so far. I got a nice little map generator going, not bad for a programming thicko :).

B
46
S
23
G
7
Posts: 459
Reputation: 6,763

Post » Sat Jul 03, 2010 9:51 pm

That looks pretty cool, Minor :)

Anyway, if you determine that you need to implement such an idea, it wouldn't be too bad to implement in Python for one of us who program with it. I know of a nice flood fill algorithm in python that could be trivially adapted to this, but a few very specific details would be needed in order to make it correctly. A lot easier to insert python code than Construct events, too.

However, some extra overhead would be introduced, since Python cannot read Construct arrays, currently. You'd have to construct a Python list copy of the Construct array from Construct events, and call the python code, which will operate on the list, then copy the result back to the Construct array. This would likely be done in a Construct function, each time a flood fill was needed.

Just something to think about. As I mentioned, I think it's quite possible with only construct events, too, but it would be more difficult to copy between projects.
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

PreviousNext

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 6 guests