How To Pick An Element Of A Container?

0 favourites
  • 7 posts
From the Asset Store
Pick Up Items Sound effects for your game, Take them for a ride right now and you will worry no more.
  • I'm trying to figure out a way to have multiple groups of objects where an item within a group is addressable and can have its properties changed. I read up on families and containers, and it seems using containers is the way to go. I created two containers that hold the same elements (the second container is apparently an instance), and each container can be manually selected and recolored, (shown in screenshot). But there's no name or UID displayed for either container. So how do I reference/pick one element of container 2, for example?

  • Either you misunderstand the purpose of containers or I misunderstood your question.

    Container itself is not an object, therefore there are no "container instances" and no container UIDs.

    Containter is a way to logically link together several instances of different objects.

    Only one instance of each object can be in a container.

    For example: 1 Enemy sprite, 1 EnemyHealthBar sprite, 1 EnemyName text, 1 EnemySkills dictionary etc.

    When you create an Enemy instance, all those other related objects will be created automatically.

    When you destroy the Enemy, all instances of other objects from the container will be destroyed as well.

    When one instance of any object is picked by an event, all other objects from the container are also picked automatically. So you can do things like this:

    Bullet on collision with Enemy -> EnemyHealthBar set visible

    or this:

    Mouse On clicked EnemyName -> Enemy start flashing for 1 second

    What objects are in your container? It's hard to tell from your screenshot.

    If you need to link multiple instances of one object to 1 instance of another objects, containers will not work.

    Say, if you want to group 1 HexCell, 7 Circles and 7 lines, you need to use instance variables.

    Usually, you select a "parent" object (HexCell) and add an instance variable "HexCellUID" for all dependent objects (Circles and Lines). Then you can pick them:

    Mouse On clicked HexCell

    ....Circle compare variable HexCellUID=HexCell.UID -> Circle set animation ...

    Or the other way around:

    Mouse On clicked Circle

    ....HexCell pick by unique ID = Circle.HexCellUID -> HexCell set animation frame to 1

    (If you are using Pin behavior, you can refer to pinned objects with built-in expression PinnedUID)

  • Oh, I'm sure I misunderstood the use of containers. I was hoping I could use them as a grouping mechanism to position collections of objects around the layout. But if not, I'll make do and carefully position elements by hand. I placed each set of instances on their separate layers so at least I can select them independently.

    So going back to my original question... If I don't employ containers or families, is there some way to abstract the reference to an instance, something like "shape1 of layer 2", instead of referring to the UID of each instance? At this point, I will have around 10 instances each of 14 source objects, so keeping track of UIDs is quite tedious.

    Thanks as always for your help.

  • Sure, you can use layers, but this is not a good or effective way to group instances.

    I still don't understand you game. If those complex shapes are made from several different objects (Circle and Line sprites), then you can do this:

    Add Circle and Line to a family "ShapeElements". Define an instance variable "ShapeID" on the family.

    For all the circles and lines of the first shape, set ShapeID=1

    For all the circles and lines of the second shape, set ShapeID=2

    Now all 14 elements of your shape 1 are logically linked together, you can do "Circle compare variable ShapeID=1" and this will pick 7 circles.

    Or you can "ShapeElements compare variable ShapeID=1", this will pick all 14 circles and lines.

    (Family in this example is optional, but I assume that circles and lines share some features and properties, so adding them to a family seems like a good idea)

    Linking by "parent.UID" is a bit different. You don't need to keep track of all UIDs, it's easier to give an example: ... .capx?dl=0

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks for the suggestions. Based on your comments, I decided to dynamically create game objects in position, rather than manually positioning everything in the layout. Of course, this makes things more flexible in the long run and avoids having unneeded objects present in the layout.

    You kind of have a sense of what's going on. Each element of the group of geometric shapes (line or circle) can be enabled or disabled (and potentially recolored), and there are 10 "groups" in the layout. The gameplay isn't intelligible at this point -- it's got a long way to go, but the end goal is a visual puzzle game.

  • Yes, UID is more suitable to situations where instances are dynamically created/destroyed during the runtime.

    If you are manually placing sprites on the layout, use ShapeID from my previous comment.

    Manually set ShapeID to "1" for all sprites of the first shape, "2" for all sprites of the second shape etc.

    Then use this ShapeID variable in your code to identify which sprite belongs to which shape.

  • I am doing more or less what you propose. But nothing is being destroyed, only made "visible" or "invisible" (using text characters of a custom font).

    Thanks for your recommendations!

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