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.
1) 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.
2) Remember to turn off
EnableGravity and apply some default material to its
StaticMesh if you wish.
3) That's all for Unreal for the time being! Hit the button and let's move on to the
4) This time, we're not going to add a constructor to our
BP_Cube2 class as we're going to spawn dynamic instances and manipulate them through the Mind class. We're simply going to add a
_rotate coroutine. Open the
Add a Class/Member dialog 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
!d : GameLib.world_delta_seconds
!r : RotationAngles!yaw_pitch_roll(yaw * d, pitch * d, roll * d)
_wait //always good to wait for at least a frame in loops otherwise our game might slow to a crawl
5) Save and compile. We're done with
BP_Cube2 scripts. Now we're going to take a look at the
Mind class. You can find it quickly by filtering for the term "Mind" in the filter in the Classes browser.
6) You'll notice that the
Mind class has a
Master subclass. The
Master Mind is 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
Mind objects that we can add to different levels and have them manage our coroutines and updates depending on what level we're currently in.
More info on the
Mind class here.
7) Since this is the 2nd tutorial, let's create a
Level2 subclass. Make sure
Mind is highlighted and hit the + button and type in
Level2. Highlight the newly created subclass and hit + again and create a constructor
8) Let's add a
print call here:
println("Level2 Mind has started!")
9) 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
10) Rename it to
Level2 Mind so we never forget why we added this. Then under the object's
Details panel, hit
+AddComponent, search for
Skookum Script Mind, and add it. Highlight it in the component list and look for the
Script Class Name under
Script. This is where we need to provide the name of our
Mind class; so type in
Level2 and hit Enter. Save the project and run it and you'll see your print call that we added in step 9.
11) Now we have our custom
Mind object running in the game. We can use this class to spawn some cubes and manipulate them! Let's go back to the
SkookumIDE, highlight the
Level2 class, and open the
Add a Class/Member dialog to add
spawn_cube as 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
12) The main difference here being the parameter
height that we're setting to 0 by default. We can use that parameter to offset the cube on the z axis.
13) 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(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
More info on branch, sync, and race commands here.
14) 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!")
15) 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_craziness to experiment with the different values in there!
How would you modify the script(s) to have all 3 cubes rotating together in parallel?!