Array sorting

For questions about using Classic.

Post » Sat Jun 16, 2012 9:03 pm

On a related note, can I access Construct arrays in python in a standard way or do I have to assign their values to a python array?

E.g. if I have an array SortArray in construct, can I do this:

sorted(range(len(SortArray)), key=lambda k: SortArray[k])

to get the indices of the sort, or will this fail? (For one, COnstruct arrays are 1 based and python are 0 based etc).
B
11
S
2
G
3
Posts: 283
Reputation: 1,968

Post » Sat Jun 16, 2012 10:16 pm

@Juryiel
For python you'd have to convert them to a python array:
[code]pyArray = map(lambda k:Array(k+1), range(Array.SizeX))[/code]
sort it in python:
[code]pyArray.sort()[/code]
Then put it back in the original array:
[code]for x,val in pyArray:
   Array.SetAtX(x+1,val)[/code]R0J0hound2012-06-16 22:18:38
B
79
S
24
G
54
Posts: 4,746
Reputation: 40,755

Post » Sat Jun 16, 2012 10:18 pm

Here's my second attempt (actually using S and not arrays), but it fails on a syntax error on the first line. I guess I'm not using S correctly in python:


[CODE]AggroSortArrayPy = map(lambda k:S.n{"NPCHatelist",str(SOL.Enemy1spawn.UID,k), range(S.sule({"NPCHatelist",str(SOL.Enemy1spawn.UID)})))

AggroOrderPy=sorted(range(len(AggroSortArrayPy)), key=lambda k: AggroSortArrayPy[k])[/CODE]


EDIT: Updated it to use map function instead of loop. Still don't know how to properly access my S stuctures in python, let me know if someone does while I try to figure it out :)Juryiel2012-06-16 22:37:10
B
11
S
2
G
3
Posts: 283
Reputation: 1,968

Post » Sat Jun 16, 2012 10:36 pm

You can't access array expressions in Construct like {"NPCHatelist",str(SOL.Enemy1spawn.UID)} or Sprite.XY from python. You'll have to do a hybrid approach with events and python.
B
79
S
24
G
54
Posts: 4,746
Reputation: 40,755

Post » Sat Jun 16, 2012 10:38 pm

Ok, I'll just transfer the {"NPCHatelist",str(SOL.Enemy1spawn.UID)} contents to a standard Array I suppose, then run the code on the array.

Thanks guys :)
B
11
S
2
G
3
Posts: 283
Reputation: 1,968

Post » Sun Jun 17, 2012 5:25 am

Hmm the map function is giving me an IndexError :(

EDIT: Nevermind, forgot to make the array globalJuryiel2012-06-17 07:35:02
B
11
S
2
G
3
Posts: 283
Reputation: 1,968

Post » Mon Jun 18, 2012 11:53 pm

I'm glad you made it :)
It is a solution with much less effort event-wise and it frees me from creating an example ;)
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Previous

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 3 guests