Python "IndexError"

For questions about using Classic.

Post » Sat Jan 28, 2012 11:40 am

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?
B
2
Posts: 6
Reputation: 262

Post » Sat Jan 28, 2012 3:42 pm

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
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Sat Jan 28, 2012 4:04 pm

Oh, it now works! Thank you a lot ^^
B
2
Posts: 6
Reputation: 262

Post » Sat Jan 28, 2012 5:44 pm

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
B
2
Posts: 6
Reputation: 262

Post » Sat Jan 28, 2012 7:11 pm

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
B
79
S
24
G
54
Posts: 4,749
Reputation: 40,761

Post » Sat Jan 28, 2012 10:35 pm

Oh, it works pretty fine now. Thank you, guys. ;)
B
2
Posts: 6
Reputation: 262


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 0 guests