Python: Create Object and Immediately Affect It

For questions about using Classic.

Post » Sat Feb 09, 2013 11:22 pm

Hi, folks. I've used Construct for a couple of one-month game jams in the past, and having come from a Click background taken quite nicely to it. It recently caught my eye when I arbitrarily decided I'd like to make some kind of game in Python -- to my surprise, Construct supported Python (2) scripting and for the most part, it's done exactly what I'd expect it to.

But I'm having trouble referencing an object just created in the same event. This seems like a very important thing to be able to do, since I'll want to create objects programmatically and change a bunch of their properties immediately.

I've got an http://s000.tinyupload.com/?file_id=00181508901184269694 which demonstrates what I'm trying. Basically, within an event I've got a script, and I'd like that script to create a Sprite object and then tell me how many sprites exist. The script has:

      # Convenient random number generator
      from random import randint
      # Create a new Sprite object in a random position
      System.Create("Sprite", 1, randint(640), randint(480))
      # Count how many objects now exist
      i = 0
      for aSprite in Sprite:
            i = i + 1
      # Display how many objects were counted
      System.MessageBox(i, "Count", 0, 0)

Running this, the for loop is always one object behind; it never includes the Sprite created earlier in the script. So my question is, within a single script sheet, how do I create an object and then immediately reference it?
B
2
Posts: 2
Reputation: 254

Post » Sun Feb 10, 2013 8:02 pm

Use SOL.Sprite to access the newly created sprite.
[code]System.Create("Sprite", 1, 0, 0)
SOL.Sprite.angle = 45[/code]
SOL stands for "selected object list" and allows access to what was picked with events.

For your example it's better to get the sprite count with Sprite.Count as new objects are not added to the list until after the next toplevel event.
B
79
S
24
G
52
Posts: 4,725
Reputation: 39,713

Post » Mon Feb 11, 2013 12:39 am

Brilliant, thanks! Now that you mention it, it does say that on the http://sourceforge.net/apps/mediawiki/construct/index.php?title=Python_Scripting#FAQ section of the Wiki (not sure how I missed it).

As a side note, if multiple Sprites are created in the same script, SOL.Sprite only refers to the most recently created one. Not that this is a problem.
B
2
Posts: 2
Reputation: 254


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 4 guests