January 18, 2017

Dynamically Creating Sibling Objects

An object's icon halo gives you the option to duplicate the object or make a sibling copy. Although duplicates are an exact copy of the original at the time, they are separate object instances. Changing the original has no further effect. Sibling copies on the other hand share variables and scripts. Add a variable or script in one and they all update. Siblings can have individual values set for properties and user created variables but they all have identical versions of scripts including script status and tick rate. Scripts can be called on to run in specific siblings or all of them. Both duplicates and siblings share the same name as the original with numbers appended in order of creation and can be renamed if you like. Copies created through scripting are always siblings which means you wind up working with siblings a lot. This is a powerful feature of the scripting system and it pays to understand how they work.

For this example I'm going to create copies of a Star object. Scripts and variables are created using the viewer for the original Star object.

Steps

 

Create Original Object:

Star Object Image
Drag out a Star object from the Supplies Flap and right click on it. By default it's rather small so resize it holding down the shift key to preserve it's aspect ratio. Open it's viewer with the blue eye icon.






Create Reset Scripts:

Whenever I work with sibling copies, I like to set up a couple of scripts to remove them and start over if I need too. Believe me this saves a lot of grief.

Drag out an "empty script" tile from the scripts category and name it reset.

Get another "empty script" and call it delete.

Star delete script Image
Go to the miscellaneous category, drag out the "Star erase" tile and drop it in the delete script.

Star reset script Image
Now select the scripting category. Drag the "Star tell all siblings" tile to the reset script and select delete in the drop down box. Whenever the reset script is run all the siblings will run their delete script and remove themselves leaving the original. They will not go to the trash but will be gone forever. Objects only go to the trash when manually deleted with the halo icon so beware any tile or menu option that says erase or destroy. Be careful not to confuse the "tell all siblings" tile with the "send to all" tile because "send to all" will remove the original as well. After checking my scripts have the correct tiles, I usually send delete back to the viewer so I don't accidentally run it.

Create copy spawn scripts:

spawn script Setup Image
Inside Star's viewer we need to create a new player type variable. Click the light brown down arrow icon on the very top bar of the viewer. I am going to call it newStar. Remember to select type player. If you forget you can change it by clicking on the menu icon to the left of the variable in the variables category. This variable will hold a reference to the last copy created. The default value is the Dot object. Next drag the "newStar" variable out by the assignment symbol from the variables categorry and name the resulting new script. I'll call it spawn.

spawn script Image
Now select the miscellaneous category and drag out the "Star's copy" tile and replace the variable value tile with it. The tile should read "Star's newStar <- Star's copy".



That's all there is to making sibling copies. Every time we run the spawn script a new star is created. Because they are copies of Star, they are all the same color and pile up on top of each other. They'll be easier to distinguish if we make them different colors and spread them out in a row.


Set the copy's unique values:

Get an "empty script" tile and call it randColor.

Set Up randColor script Image
Go to the color category in Star's viewer, drag out the "red", "green" and "blue" tiles by the assignment icon and drop them on the randColor script.



randColor script Final Image
Open the gold box on the script editor menu and you will see a tile for creating random numbers in a range from 1 to value. The default is 5. Replace the value tiles in all the statement tiles with the "random number" tile and set each one to 100. This will set the rgb values to a random number between 1 and 100 and combine to form a new random color.


spawn script Updated with randColor Image
Go to the script category and drag the "randColor" script tile to the spawn script. We want this script to run in the new Star not the original. When the spawn script runs player variable newStar is assigned the copy. So drag the variable name tile "Star's newStar" to the script and replace the tile "Star's" with it. Now the statement should read "Star's newStar randColor". The script will run in whatever player object is assigned to the newStar variable.


Now we want to move the copy to the right along the x-axis. 140 pixels should be enough. Each copy we make will be set to Star's values so we will need to increment the x location by 140 every time  we make a copy or the stars will start to pile up on each other again. Make a new number variable in the Star viewer called xIncr and set it to 140.


reset script xIncr Variable Added Image
We are going to want to reset that every time we start over so drag it by the assignment symbol over to the reset script.


spawn script Final image
Next go the basic category and drag out "Star's x" tile by the assignment symbol and drop it on the spawn script. Click on the "x" or the arrows to the left of it and select increase by. Replace the current "x" value with the "xIncr" variable tile. Replace "Star's" with the "newStar" variable tile so only the copy's x value increases by the value in xIncr. The statement should now read "Star's newStar x increase by xIncr". Now grab the "xIncr" statement by the assignment symbol, drop it in the spawn script and set it to increase by 140. It should now read "Star's xIncr increase by 140". Now each time the spawn script is run, a new star should appear 140 pixels to the right of the last one and should have a different color.


Run the script once and open Star's viewer. An image of the copy should be visible in the newStar variables value. xIncr should now be set to 280. Right click on the new star. It should be named Star1. Open it's viewer and you'll notice that the newStar variable and the xIncr variable are set to whatever Star's values were when it was copied. Drag out the "randColor" script and look at the statements. They all now automatically belong to Star1. So when the spawn script ran Star1 was first created and then assigned to variable newStar. The statements in the rest of the script are translated to "Star1 randColor" etc.  Create another star and you should see the new copy in Star's newStar variable. Open the viewer for the new Star and look at it's values. When you run out of room for more stars you can run reset and start over and perhaps figure out how to start a new row in the spawn script. On a side note, you'll notice a star category added to Star's viewer. Playing with the options there changes the Star object in interesting ways.


There is one tricky thing to know about assigning values to player type variables inside objects and making copies. If you create a player type variable in the original object and assign the player object itself to the variable, the copy will automatically assign itself to the same variable. If you need the value for the variable to stay constant, that is not the behaviour you want. In that case, define and assign the variable outside of the object in the World object. That way you only need to assign it once. I'll go through that in the next post Creating a Linked List with Sibling Objects which is one example of what can be achieved with sibling objects.



Last Updated.