Python "IndexError"

This forum is currently in read-only mode.
  • Hi everyone. I'm new to the forum so.. hello! My name is Felipe and I use Construct for a while making my game experiments.

    Anyway.. My real purpose of registering in the forum is to solve a annoying problem I've been having:

    Although I know that there's a particle effect built-in in Construct, I'm trying to create my own Python-scripted one.

    Here is the CParticle class:

    class CParticle:

         ID = 0;

         def __init__( self, nX, nY, nW, nH ):

              CParticle.ID += 1;

              self.ID = CParticle.ID;

              self.nX = nX;

              self.nY = nY;

              self.nW = nW;

              self.nH = nH;

              self.Create();

         def Create( self ):

              System.CreateByName( "spr", 1, self.nX, self.nY );

              spr[self.ID].Width = self.nW;

              spr[self.ID].Height = self.nH;

    And this is my second script that creates a CParticle instance:

    particle = CParticle( 200, 200, 8, 8 );

    My problem is that when I run the project, I get the following error message:

    Traceback (most recent call last):

    File "<string>", line 1, in <module>

    File "<string>", line 11, int __init__

    File "<string>", line 15, in Create

    File "<string>", line 482 in __getitem__

    IndexError

    Okay, I see that the index in "spr[self.ID]" (suposed to be 1] doesn't exist, but the sprite is created on screen. I tried to replace self.ID for 1 (since there's already another instance before the creation) and I get the same error.

    So, my question is: why Construct does not detect the new instance?

  • It does detect it and it gives it a "picked" status. That is, it is put on the selected objects list ("SOL"). And you acess picked objects using SOL.objectname:

    SOL.spr

    http://www.scirra.com/forum/python-refer-to-instance-from-event_topic44898_post281166.html

    Another way would be using "getattr", getattr(SOL, objectname):

    getattr(SOL, spr).width = self.nW

    http://www.scirra.com/forum/function-created-with-python-needs-check_topic45101_post283709.html

  • Oh, it now works! Thank you a lot ^^

  • Well, it worked well, but only when I create one object CParticle. If I create another objects CParticle, only the last one is updated. I think Construct only takes the lost picked "spr" in count.

    How am I supposed to fix this? I've tried to use SOL.spr[index] but I get the same "IndexError" as before.

    My code so far is on the .cap:

    particle.cap

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • In your Create method save a reference to the new object you just created to a variable.

    self.spr = SOL.spr[0]

    Then in your update method you can access the sprite with "self.spr".

    self.spr.x += self.velx

  • Oh, it works pretty fine now. Thank you, guys. ;)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)