Python as an array(?)

For questions about using Classic.

Post » Sun Apr 11, 2010 8:32 am

I was thinking it might be useful to use the python script editor with its copy and paste ability as a way to import an array into Construct with having to import an external file.
The problem is how to get things hashed out correctly...
Apparently ampersand doesn't concatenate the same way in python.

+ System: Start of layout
-> Array: Set size to 4 x 1 x 1
[code:xua455tx]x1=5
x2=3
x3=3
x4=1[/code:xua455tx]
+ Array: For Each element
-> Array: Set index Array.CurrentX to python("x&Array.CurrentX")

+ System: Always (every tick)
-> Text: Set text to Array (1)

Any ideas on how to get them to play nice with each other?


Edit:
oops
This seems to work.
python("x"&Array.CurrentX)

Any way I guess this is no longer a question but a statement instead... Move if you like
Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Post » Sun Apr 11, 2010 4:37 pm

Oooh, Python integration... :)

There are numerous other ways to do such a thing, as well. For example, you can use a python list as an array. Here is a variation of the above, which will automatically set the Construct Array size, if you add or remove elements in x:

+ System: Start of layout
[code:26ee4b3r]x = [5, 3, 3, 1, "No", "Yes"]
Array.SetSize(len(x), 1, 1)
for index, value in enumerate(x):
Array.SetAtX(index + 1, value)[/code:26ee4b3r]

+ System: Always (every tick)
-> Text: Set text to Array (1)

No real error checking, though...
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Post » Sun Apr 11, 2010 5:28 pm

I just taught my self python last week but as of yet not tried it with Construct. Objects rock in python.

Can you do an example with the Python array in construct as a Cap file so I can see you you added them together?
B
28
S
8
G
8
Posts: 530
Reputation: 7,154

Post » Sun Apr 11, 2010 6:15 pm

I've been using relatively unused "at" or @ array access operator. It is fairly simple and requires no python.

[code:1uak4www]+ System: Start of layout
-> Array: Set size to 3 x 1 x 1
+ Array: For Each element
-> Array: Set index Array.CurrentX to {"one","two","three"}@Array.CurrentX
[/code:1uak4www]
B
79
S
24
G
54
Posts: 4,746
Reputation: 40,755

Post » Sun Apr 11, 2010 6:17 pm

Opps sorry R0J0hound. I didn't need a construct array I was just looking at how to implement my new python skills into Construct. Reading wiki now.
B
28
S
8
G
8
Posts: 530
Reputation: 7,154

Post » Sun Apr 11, 2010 7:18 pm

Interesting info, R0J0hound. Thanks.

[quote="Guyon":3n7tdzn8]I just taught my self python last week but as of yet not tried it with Construct. Objects rock in python.

Can you do an example with the Python array in construct as a Cap file so I can see you you added them together?[/quote:3n7tdzn8]

Well, I'm still learning, myself, so sorry if I got a bit scattered with my example... :oops: Here's an example of some ways to integrate Python into something like the original example:

http://dl.dropbox.com/u/5868916/PyStuff.cap

It just shows different ways to access objects using a combination of Construct events and Python scripting. The example also lets you modify the values on the fly by editing the EditBox widgets in the upper left. It can get a bit complicated in some cases, especially when picking is involved. Best to stick with Construct actions when you need to pick.

It should be noted that all objects in the layout have corresponding objects accessible through Python as well. I made a recent thread with a little utility that lets you mess around with objects interactively through Python, here:

viewtopic.php?f=16&t=6158

You can drop an object in the layout, and run it, then interact with it.
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Post » Sun Apr 11, 2010 7:28 pm

@R0J0hound
Thanks, I've wondered how that would actually be implemented.

Question tho, how would you go about using that in a 2 dimensional array?

@Silent Cacophony
Thanks, checking out that cap now.
Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Post » Sun Apr 11, 2010 8:35 pm

[quote:1gnjibfz]how would you go about using that in a 2 dimensional array?[/quote:1gnjibfz]
Like this:
[code:1gnjibfz]+ System: Start of layout
-> Array: Set size to 3 x 2 x 1
+ Array: For Each element
-> Array: Set index (Array.CurrentX, Array.CurrentY) to {"one","two","three","four","five","six"}@((Array.CurrentX-1)*2+Array.CurrentY)
[/code:1gnjibfz]
B
79
S
24
G
54
Posts: 4,746
Reputation: 40,755

Post » Sun Apr 11, 2010 9:11 pm

Ok, but I'll have to take your word for it. Now that brings up another question. Does "@((Array.CurrentX-1)*2+Array.CurrentY)" work for any array size?
Well except for z that is.
Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Post » Sun Apr 11, 2010 10:18 pm

In general if you have an array with sizes: sizeX, sizeY and sizeZ,
and X,Y and Z are the current positions,
then here is the general formula:
X+sizeX*(Y-1)+sizeX*sizeY*(Z-1)

or if written in construct:
[code:exdklix3]@(Array.CurrentX + Array.SizeX*(Array.CurrentY - 1) + Array.SizeX*Array.SizeY*(Array.CurrentZ - 1))[/code:exdklix3]
and if it's just a 2d array:
[code:exdklix3]@(Array.CurrentX + Array.SizeX*(Array.CurrentY - 1))[/code:exdklix3]


The other formula I gave you fills the Y column before increasing X. It was in this form:
Y+sizeY*(X-1)
B
79
S
24
G
54
Posts: 4,746
Reputation: 40,755

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 2 guests