How to "upgrade" an object to a family

11

Stats

6,464 visits, 11,925 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 23 Apr, 2013. Last updated 19 Feb, 2019

It's always good to plan ahead when making a project, and determine when you'll need a family in advance. However it's easy to forget and end up with a large number of events using a specific object type, when you'd rather all those events were for a family instead. This tutorial will show you how you can convert all those events to refer to a family, preserving any references to instance variables or behaviors, without having to re-do all the events by hand.

An example

Suppose we have an object Monster which we want to replace with a family called Enemies so we can easily add different kinds of enemies... but we already have 100 events for Monster! Here's how to "upgrade" it to a family.

Back-up

This tutorial involves changing large parts of your project. If you make a mistake, you could end up with quite a mess. So be sure to make a backup of your project first. If you haven't already, you should also turn on Construct 2's automatic backup.

The process

First of all, we need to create the family to replace with. We can't create an empty family, so in fact first we need to create an empty object which will only be used temporarily. In this case, create a new Sprite and add it to the layout (no need to worry about its image, we'll delete it later). If you're replacing a different kind of object, like a Text object or Tiled Background, create one of them instead.

Next, create the family. In this case we'll call it Enemies. When prompted to add an object to the family (since it can't be empty), add the empty object you just created and press OK.

Now we need to add all the same instance variables, behaviors and effects that the object has to the family. Objects can only be replaced with other objects or families with matching instance variables, behaviors and effects, so we need to add matching ones to the family. They must all have identical names. For example, if Monster has the number instance variables health and speed, and a bullet behavior named Bullet, we must add health and speed number instance variables to the Enemies family, as well as a Bullet behavior.

Now the events can be replaced with a reference to the family! In your event sheet, select all (Ctrl+A) then press R (for Replace Object). First choose the object you want to replace (in this case the Monster), then you can choose to replace it with the new family. If the family does not appear, or a message appears saying there are no objects that it can be replaced with, double-check the family has exactly the same instance variables, behaviors and effects, all with the same names and types. If you have multiple event sheets with events referring to the object, you'll also need to repeat this process for each event sheet, selecting all the events then replacing the object with the family.

Your events now refer to the family! But the original object - our Monster - is not in the family. We can't add it yet either, since its own instance variables and behaviors conflict with the family's.

So now delete all the instance variables, behaviors and effects from the original object. Try not to feel too nervous - by now there should be no events referring to the object at all, since we replaced them all with the family. (And you made a backup earlier... right?)

In the project bar, edit the new family and add the object to the family. In this case we'd add the Monster object to the Enemies family. If you get an error, make sure you deleted all the instance variables, behaviors and effects from the object. We also don't need the temporary object any more, so you can remove the temporary object from the family, then delete it from the project completely from the project bar.

End result

Now all your events refer to the family instead of the object, and the object inherits all the same instance variables, behaviors and effects from the family. So it works exactly how it used to, but you can now add other objects to the family, taking advantage of applying the family events to different object types. Hopefully this will come in handy at some point!

  • 0 Comments

Want to leave a comment? Login or Register an account!