Python: Create Object and Immediately Affect It

This forum is currently in read-only mode.
From the Asset Store
Fully commented source code/event sheet & sprites to create a space shooter game
  • 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 s000.tinyupload.com 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?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Use SOL.Sprite to access the newly created sprite.

    System.Create("Sprite", 1, 0, 0)
    SOL.Sprite.angle = 45

    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.

  • Brilliant, thanks! Now that you mention it, it does say that on the sourceforge.net/apps/mediawiki/construct/index.php 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.

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