0 Favourites

How do I tie two objects to one another with variables?

  • Hi, I'm a little bit clueless on the proper way to accomplish the thread title and I'd appreciate a bit of help.

    In my game, the player can purchase upgrades by clicking buttons like these (ignore that the icons on the button are the same, that's just for testing). This is actually three objects: The button itself, the text denoting the price, and the sprite in the top right that indicates whether the upgrade has been purchased. For reference, the buttons are placed by hand on the layout, while the indicator is spawned:

    The problem I'm having is that I have no idea how to link these objects together. I just don't know how to make a specific button and a specific indicator be tied together when it spawns so that when the upgrade is purchased, the indicator changes. How would I go about doing that?

  • Have a look at https://www.scirra.com/manual/150/containers first. There are other ways.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • What do you mean by link together? Like which button upgrades what stat, or you want to use the container to make two objects act like one?

  • If you talk about 'link' then there are 2 sides to it.

    Link (1) as in "when one moves, the other moves with it".

    &

    Link (2) as in "when one gets picked, the other needs to be in the picklist too"

    For (1) you can use the behavior 'Pin'. Use 1 sprite as the base for the others. Pin the others to it.

    Before pinning, they have to be in position. Since you have carefully crafted them in the layout, that should be not prob.

    You dont have to use 'Pin', you can also set the position of one to image points of another one EVERY tick. To do this, you need to be able to properly pick them. And that brings us by link (2).

    For (2) You can pick based on an instance variable. Give them all the same named instance variable. Say 'ID'. Now in the layout change that variable, for those that belong to each other, to the same value.

    Now when ever you pick one, you can pick the other that belongs to it.

    Example:

    Mouse > On object clicked (button) <------- this picks the button that is clicked on

    System > Pick by comparisation > object = text .. expression = text.ID .. value = button.ID <---- this adds the object text with the same ID as the button to the picklist

    System > Pick by comparisation > object = indicator.. expression = indicator.ID .. value = button.ID <---- this adds the object indicator with the same ID as the button to the picklist

    ............... and so on, for all objects ....

    ... Actions work on the picked objects only.

    So you can after a pick like do in the actions ...

    (object) text > action > Set position to another object ... object = button .. and use that imagepoint

    (object) indicator > action > Set position to another object ... object = button .. use that imagepoint

    For (2) you can also use a Container. Look them up in the manual. They are extremely handy. But not for you in this case. Since you have everything set up in the layout. Containers help by creating and linking objects during run time. And they expand the idea about linking.

    They ...

    link (1) If one gets picked, the other gets picked.

    link (2) if one gets destroyed, the other gets destroyed

    link (3) if one gets created, the other gets created

    To finish, lets break down button.ID above.

    We call this an expression. If you look in the online manual. You always see 'properties', 'conditions', 'actions' and 'expressions' explained.

    An expression is info about an object (and optional its behaviors). They are always written the same way.

    button.ID

    button = name of an object.

    the dot = kinda a separator

    ID is the expression, in this case the expression for the instance variable 'ID'.

    Another example.

    Sprite.Angle

    Sprite = name

    the dot

    Angle = angle for the Sprite

  • Interesting! I decided to go with giving each object an ID variable and that works great. Now, with that in mind, is there a way to spawn them with an incrementally increasing ID value, so that I don't have to drag in a bunch of em manually to the layout? Eventually there'll be dozens of upgrades to choose from and it'd be kind of a pain. That'd be ideal.

  • An old example.

    Same with spawning in the other order.

    https://www.dropbox.com/s/xupduervio26y ... .capx?dl=0

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic Locked topic
198 18,657
Tom's avatar
Tom Construct Team Founder
Unread hot topic
99 10,137
karshinkoff's avatar
karshinkoff
Unread hot topic
132 10,994
Burvey's avatar
Burvey