How to "upgrade" an object to a family

16

Stats

4,675 visits, 5,638 views

Tools

Translations

This tutorial hasn't been translated.

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 4 Oct, 2017. Last updated 14 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.

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 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 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!

  • 4 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • This should be added as a base behavior in construct, extract family

  • How do I do this if there are several instance variables in each object type that I want to keep in the object instead of the family? I have hundreds of places across multiple event pages where each of the objects I want to combine into a family are used in different ways, but I want to combine their like features, and each object type has a large number of instance variables.

  • I guess it is just good practice to create instances of an object through families so that it can be late accessed easily

  • Great tutorial, I've used it a few times already. Thanks!