Python question

This forum is currently in read-only mode.
  • 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)

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

    Not directly, but you can use UID to find identical objects:

    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? 
    
    The indices will be the same until any of that object type is destroyed, in which case the order will be scrambled a bit.
  • 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?

  • >

    > Not directly, but you can use UID to find identical objects:

    >

    index=0
    > for i,obj in enumerate(Sprite):
    >     if obj.uid==SOL.Sprite.uid:
    >         index=i
    >         break[/code:2vs3xkyj]
    > 
    > 
    

    Does this work with indirect references? I mean the selected objects list:

    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 ?
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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'.

    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.
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)