Working with copies
Working with copies
Oftentimes while defining your game logic you will need to find and modify a copy from an event of another copy. Catnip has several ways to do that.
Changing groups of copies
To affect all copies of a specific template, you can use a block called "With all the copies of a template". Inside this block, any commands that previously would affect the copy of your event will instead affect copies of the selected template.
A similar block exists to affect all copies in the current room: "For each copy".
Warning
Properties will also read and write values to manipulated copies. If you want to transfer data from main copy to the ones you affect, use variables.
Changing specific copies
To modify a specific copy, you can use several blocks: "With copy", "read property of X" and "write property of X". But firstly, you need a reference to the copy you want to change. References are regular variables and properties that link to complex structures, and copies are one example of such structures. References differ from string, numerical, and Boolean values: when you copy a value from one string variable to another, changing one variable doesn't affect the other. When you are working with references, nothing is duplicated, and you are still working with one object even if you are working with several references to it.
Getting a copy of a specific template
Catnip has a computed block "templates list" which returns an array of all the copies of the template you specify in it. With blocks from the Arrays category, you can get a reference to any copy in this array. This can be used to get a copy of your hero character, or get the list of all the mobs in a room. You can retrieve the values of this array with blocks from the Array category.
Note that getting an element from this array may return nothing if there are no copies of this template in a room or if you select an element in a position which exceeds the size of the array. Because of that, you must always add conditions that check whether the value you got is a valid copy. Also remember that arrays' indices start with 0, not 1.
Example: Find the hero character and move towards it
Changing copies you create programmatically
When you create a copy, you can store a reference to it to a variable using the right-hand slot "store in X". A classic use case is creating a projectile and configuring it.
You can also change the properties of the created copies right in the "Copy a template" block, with the "Advanced" panel at the bottom of it:
Storing a reference is thus more useful when you need to change a copy after some time. For example, in an arcade space shooter game, you may need to destroy any flying turrets that follow your character:
Affecting the impacted copy in collision events
Collision events provide you with a special event variable (put the variable block here) that can be combined with a block "With copy". For example, this can be written in an event of a projectile template when it collides with an enemy:
What's next?
If you have already read other pages in Learning Catnip category, your next step is completing tutorials! They will show how to apply the received knowledge on practice and how to work with the engine. Try these: