for Beginners: From Object Picking to Containers

For questions about using Classic.

Post » Sun Jun 29, 2008 8:25 pm

Look at this.

It does exactly the same as previous .cap. But with easier pick events.
You dont see the difference? OK lets walk you trough it.

Starting in the layout. There is only 1 tank in the layout. 1 base + its turret.

Both are in the same container.
You make a container by adding objects to it under the *groups* properties, and deeper *containers*

Why not name the container, as we name the families ?
Well that would be definably make containers easier to understand, but limit its possibility.

What can a container do for you ? It makes picking contained objects easier. Pick 1 object in a container and all objects in the container are marked as picked.

Lets see this work. Go to the events editor.

See event 2

Creating An object thats part of a container will create all the other objects in the container in an urge to keep the container complete.
As you see there is no action that creates a second turret.
When you create an object, that object will be marked as "picked"
Since the Base that we created is part of a container, and in that container is also the Turret, moving Turret will only move the Right turret, the one thats part of the container we just created.

See event 4
The loop walks trough the instances of object Base, and sets them picked one by one. But since base is part of a container that also contains a turret. The right turret instance will be set as *picked* too.
If we can pick the right turret based on picking the right Base, then only Base needs a identification number in a private variable. Turret does not need one no more.

see event 18
We pick an instance of Base by comparing one of its private variables to a value.
The variable is 'call_me', and this variable is holding the identification number for the instances.
The value is 'selected', a global variable, thats holding the number representing which tank is selected.

So it states: set the 'selected' base picked.
But since Base is part of the container that is holding the Turret, also the right Turret will be automatically set as picked.
So we can do actions for the right Base, and for the right Turret.

As you see, there is no conditions that tries to pick the right Turret. We have no idea anyway which turret is sitting on top of this Base, because we did not even give the turrets an identification number.

But the system knows because it knows in which container the Turret is sitting.

See event 20

We just picked the right Base, now we have the right Turret picked also. And the action will let the right Turret fire a bullet.

See event 22.
We only picked the right Base. Destroying this base will destroy all Turrets in that container.
As you see there is no action to destroy any turret.

Convinced to use containers? : )
Posts: 322
Reputation: 2,119

Post » Sun Jun 29, 2008 10:43 pm

Best tutorial ever, should be stickied!!!

Even if I knew the most of this, this tutorial helped me picking certain istances.

This is one of the most informative tutorials for the newbies, TheIstance! Make more! :D
Posts: 512
Reputation: 2,674

Post » Mon Jun 30, 2008 2:07 am

Hi TheInstance,

No that fine about the sprites, If it helps the other people learning Construct I am all for it.
Posts: 107
Reputation: 4,944

Post » Wed Jul 02, 2008 1:17 pm

tags: picking containers c0nstuct

(just for me to find my *tuts* back)
Posts: 322
Reputation: 2,119

Post » Thu Jul 10, 2008 5:41 am

Wow. Normally I don't bother to rate tutorials or anything, but this one was wonderful. I learned more from it than from the Ghost Shooter tutorial.. and even a little about using sub-events :P
Posts: 26
Reputation: 1,252

Post » Thu Jul 10, 2008 2:30 pm

Guess this story is a never ending story. Yup i have to add something to this topic.

The follow .cap i stole from this topic. (i always misspell threat, so allow me to use 'topic' in stead )


You can download it from there, or pick it from this link.


Fire it up, and take a look at it.
I am only interested in the event numbers 2, 3 and 4.

Back then i saw the error in this. But i had no 'general' solution. With 'general' pointing to something that will work in all situations. I do now.

Lets first analyze the problem.

There are 3 objects in this project. Sprite, Sprite2 and Sprite3. Personal i do not like unnamed objects. But anywayz.

All 3 objects are member of the group 'blue".

So in events 2 .. 4. You see an attempt to set the gravity in the platform behavior for a certain object to zero, when the vectorY in its platform behavior is also zero.

This looks ok j0h! Whats your problem now again !???

Um, well, the syntax is right. And it does exactly what it is programmed to do.
Namely, when the object sprite's VectorY = zero, it will set the gravity in ALL objects that are member of the family Blue to zero.

And it does this in event 2 and 3 and 4, resulting in .. well i guess you can see that.

Do not laughs ! I have done those kinda things myself. I am dead serious ! ( lol well ).

Now lets upgrade this isolated little error to something 'general' to learn from.

The condition in event number 2 is a (what i call) Flow Condition. It runs its Sub Events and Actions when the condition is true.

But what we need here is a Pick Condition. In this case we need to take all the Blue objects, filter out those who's VectorY is not zero, and feed the left overs to the Sub Events and Actions.

Yep, its a Picking Objects situation, why its on its place in this topic.

We do have a small problem here though. The expression as used in the events in this .cap, can not be accessed using a Family.
Thats is not a bug or so, thats rather a very normal thing. Imagine an object having 2 platform behaviors. And at same time being member of a family with 10 other objects, who have only 1 platform behavior attached. Now when accessing the expressions trough the family name, witch VectorY shall the system take ? The one of the first behavior or the one of the second behavior ?

Anyway !
We need a good Pick Condition to make this work.

So lets convert *Flow* to *Pick*.
Let me show you in steps "how to".

Step 1.
Make a private variable in the objects.

Use the "for each object" system loop to walk trough the objects and set the private variable to the Expression. (in this case that would be to the VectorY in the platform behavior)
It does not matter in what order the "for each" walks trough the objects, so do not worry about that.

Now you can use the Pick Condition "Compare Private variable" to pick objects based on the stored expression.

Problem solved ! : )

You can see this work in this .cap


Hope this was a help.
Posts: 322
Reputation: 2,119


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 0 guests