Python question

For questions about using Classic.

Post » Thu Jun 09, 2011 3:45 am

I've tried to look through some tutorials but haven't figured this out yet so I thought I'd ask:

1. Is there a way to get the array indices for the list of Sprite (instances of the object Sprite) in the SOL?

2. Will those indices always be the same if I create and destroy Sprites the same way? E.g. if i run two instances of the same program and do the same stuff in them, will the Sprite indices always be the same or is there some random factor involved? (I'm pretty sure they should be but just making sure)
B
11
S
2
G
3
Posts: 283
Reputation: 1,968

Post » Fri Jun 10, 2011 3:19 pm

[quote="Juryiel":2el5nzri]1. Is there a way to get the array indices for the list of Sprite (instances of the object Sprite) in the SOL?[/quote:2el5nzri]
Not directly, but you can use UID to find identical objects:
[code:2el5nzri]index=0
for i,obj in enumerate(Sprite):
if obj.uid==SOL.Sprite.uid:
index=i
break[/code:2el5nzri]

[quote:2el5nzri]2. Will those indices always be the same if I create and destroy Sprites the same way? [/quote:2el5nzri]
The indices will be the same until any of that object type is destroyed, in which case the order will be scrambled a bit.
B
79
S
24
G
52
Posts: 4,725
Reputation: 39,713

Post » Fri Jun 10, 2011 3:53 pm

Yeah I've been trying to do something similar with UID. I guess syntax like Sprite.index() won't work? And I'm curious, what exactly is in Sprite.SOL? or for that matter, what's in py.SOL and py.SOLi? I got these from a thread abbout the python picking plugin and I see how they're being used but not sure what they contain. E.g. how can you do while py.SOL as in that thread? viewtopic.php?f=2&t=5305&hilit=python+picking
B
11
S
2
G
3
Posts: 283
Reputation: 1,968

Post » Fri Jun 10, 2011 4:06 pm

[quote="R0J0hound":2vs3xkyj][quote="Juryiel":2vs3xkyj]
Not directly, but you can use UID to find identical objects:
[code:2vs3xkyj]index=0
for i,obj in enumerate(Sprite):
if obj.uid==SOL.Sprite.uid:
index=i
break[/code:2vs3xkyj]
[/quote:2vs3xkyj][/quote:2vs3xkyj]
Does this work with indirect references? I mean the selected objects list:

[code:2vs3xkyj]def getIndex(constructObject):
index=0
for i,obj in enumerate(constructObject):
if obj.uid==SOL.constructObject.uid:
index=i
break
return index[/code:2vs3xkyj]
If it isn't possible, is there a workaround without direct access like SOL.Sprite ?
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Fri Jun 10, 2011 4:42 pm

Not all objects have an uid attribute, so that could be an issue.

The best way to get direct access is to just save a reference of that object in a variable.

Pick the object somehow and save it's reference as 'player'.
[code:2rwl2uht]player=SOL.Sprite[0][/code:2rwl2uht]

then after that is you want to access that object just use player:
[code:2rwl2uht]player.x += 1
player.angle=45[/code:2rwl2uht]

This will avoid the issue of the index changing.
B
79
S
24
G
52
Posts: 4,725
Reputation: 39,713


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 0 guests