##Spawn cubes dynamically and manipulate them using Skookum
Remember that you can download the tutorial project here. This tutorial uses map Level2.
For this tutorial, we’re going to create the same cube but in a separate BP so we can use the Skookum
Mind class to spawn it and manipulate it. This write-up assumes that you’ve already gone through the previous tutorial.
Start a new project (or feel free to use any suitable test project you may have). Create a cube BP (just like we did in the previous tutorial) but name this one
BP_Cube2. The reason for a new cube BP is that we want to have a different setup and scripts inside its Skookum class.
Remember to turn off
EnableGravityand apply some default material to its
StaticMeshif you wish.
That’s all for Unreal for the time being! Hit the button and let’s move on to the SkookumIDE.
This time, we’re not going to add a constructor to our
BP_Cube2class as we’re going to spawn dynamic instances and manipulate them through the
Mindclass. We’re simply going to add a
_rotatecoroutine. Select the
BP_Cube2class in the Classes widget, then open the New Class or Member pane and add
_rotate. This will be our code inside:
() [ //create some random speeds for each axis !yaw : MathLib.random_float * 100.0 !pitch : MathLib.random_float * 100.0 !roll : MathLib.random_float * 100.0 loop [ !d : GameLib.world_delta_seconds !r : RotationAngles!yaw_pitch_roll(yaw * d, pitch * d, roll * d) add_actor_world_rotation(r) _wait //always good to wait for at least a frame in loops otherwise our game might slow to a crawl ] ]
Save and compile. We’re done with
BP_Cube2scripts. Now we’re going to take a look at the
Mindclass. You can find it quickly by filtering for the term “Mind” in the filter in the Classes widget.
You’ll notice that the
Mindclass has a
Master Mindis the 1st Skookum class to get constructed at launch. You can check that out by just running the current project that you’re creating and seeing the print call in its constructor:
Member: Master.!() - Mind instance constructor called.
So we could spawn and manipulate our cubes inside the
Master, but we can do something even better. We can create our own
Mindobjects that we can add to different levels and have them manage our coroutines and updates depending on what level we’re currently in.
Since this is the 2nd tutorial, let’s create a
Level2subclass. Ensure the
Mindclass in the Classes widget is selected and open the New Class or Member pane and type in
Level2. Ensure the the newly created subclass is selected, open the New Class or Member pane again and create a default constructor
Let’s add a
() [ println("Level2 Mind has started!") ]
Save and compile. Let’s move back to Unreal for a bit. We’re going to add our
Mindinto our current level. Go to your
Modestab and find
Basic. Drag one out into the level anywhere (placement is not important here, this is a purely logical object; a container for our
Rename it to
Level2 Mindso we never forget why we added this. Then under the object’s
+AddComponent, search for
Skookum Script Mind, and add it. Highlight it in the component list and look for the
Script Class Nameunder
Script. This is where we need to provide the name of our
Mindclass; so type in
Level2and hit Enter. Save the project and run it and you’ll see your print call that we added in step 9.
Now we have our custom
Mindobject running in the game. We can use this class to spawn some cubes and manipulate them! Let’s go back to the SkookumIDE, select the
Level2class, and open the New Class or Member pane to add
spawn_cubeas a method. This will be our code for it:
(Real height : 0) [ !xform : player_pawn.transform xform.@translation += [player_pawn.actor_forward_vector *= 500] //move xform forward relative to player xform.@translation.@z += height //specify how high we want our cube to spawn BP_Cube2.spawn_at_xform(xform) ]
The main difference here being the parameter
heightthat we’re setting to 0 by default. We can use that parameter to offset the cube on the z axis.
One last thing we’re going to add to this class is a coroutine called
_run_craziness. This will be our code for it (check the comments for an explanation on what it does):
() [ _wait //wait a frame to make sure the player has spawned //spawn 3 cubes at varying heights (1st call uses the default parameter specified in the function) spawn_cube spawn_cube(2.0 * Real.@@meter) spawn_cube(4.0 * Real.@@meter) 4._do //do the following block of code 4 times [ BP_Cube2.instances._do //do the following block of code on all instances of BP_Cube2 objects in our level [ race //race the _rotate call with the _wait call; essentially rotating for 5 secs [ item._rotate //item refers to an instance of BP_Cube2 _wait(5) ] ] ] ]
In short, the code above will spawn 3 cubes then rotate each one for 5 seconds and loop over all instances 4 times. All that’s left is to call our crazy coroutine from our
() [ println("Level2 Mind has started!") branch [_run_craziness] ]
Save and compile then head on back to Unreal and run the game. You’ll see 3 cubes spawn on top of each other right in front of you with one rotating for 5 secs before the coroutine moves on to the next one! Make sure to go back to
_run_crazinessto experiment with the different values in there!
How would you modify the script(s) to have all 3 cubes rotating together in parallel?!