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

Get help using Construct 2

Post » Fri May 19, 2017 8:13 pm

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

Image

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:

Image

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?
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Fri May 19, 2017 8:33 pm

Have a look at https://www.scirra.com/manual/150/containers first. There are other ways.
ImageImageImage
B
71
S
22
G
241
Posts: 3,734
Reputation: 134,494

Post » Fri May 19, 2017 8:55 pm

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?
Like my work? Fallow me on social media, and see my games on the arcade.
B
7
S
5
G
9
Posts: 298
Reputation: 5,367

Post » Fri May 19, 2017 9:00 pm

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
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Fri May 19, 2017 11:53 pm

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.
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Sat May 20, 2017 9:45 am

An old example.

99Instances2Go wrote:Same with spawning in the other order.

https://www.dropbox.com/s/xupduervio26y ... .capx?dl=0
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950


Return to How do I....?

Who is online

Users browsing this forum: Armench, brunopalermo, Kin, worm1 and 16 guests