StaticMesh, the hard way, just to do some learning.

In unreal script there’s the defaultproperties where you can use Begin Object and then Components.Add(someObject). What is that doing exactly?

lets do some stuff the hard way just to see how we can make our objects more dynamic.

In a previous tutorial I created a MyActor.uc file with you, and in there we had Begin Object in there that created a mesh object and added it to the actor. How else can we do the same thing? lets see using the “new” command.

class MyActor extends actor
	placeable;

var StaticMeshComponent SMeshComponent;

event PostBeginPlay()
{
	super.PostBeginPlay();

	SMeshComponent = new(self) class'StaticMeshComponent';
	SMeshComponent.SetStaticMesh(StaticMesh'MyObjects.Meshes.MyStaticMesh');
	AttachComponent(SMeshComponent);
}

defaultproperties
{
}

Notice that there’s nothing in the default properties, this is because we’ve created the mesh in the PostBeginPlay() function. We started by adding a var to the class caled SMeshComponent, which is a StaticMeshComponent type object. To make use of this we fill in the variable in memory with a “new(self) class’StaticMeshComponent’” this is cool in a number of ways. First, it’s a new object for “self” the “self” keyword is a reference to the class that created it. Important because when this class is destroyed the new mesh will know that its parent is the class that created it. Second we now have control over the contents of that variable, and we can manipulate it in a few basic ways.

Comething Creative

Lets add two new variable types, a bool, and a couple of StaticMeshes, and a float for a timer.
our vars should now look like this:

var StaticMeshComponent SMeshComponent;
var StaticMesh Mesh1, Mesh2;
var float SwitchDelay;
var bool bMeshSwitch;

We’ll use the SwitchDelay to setup a place to record some WorldInfo.TimeSeconds, and then we’ll make a bMeshSwitch to keep track of which mesh we’re going to show. And for good measure we’ll keep a place in memory for each mesh.

Here’s some of the code for the timer and the mesh switch logic:

event Tick(float DeltaTime)
{
	super.Tick(DeltaTime);
	SwitchMesh();
}

function SwitchMesh()
{
	if( WorldInfo.TimeSeconds > SwitchDelay )
	{
		if( bMeshSwitch )
		{
			SMeshComponent.SetStaticMesh(Mesh1);
			bMeshSwitch = !bMeshSwitch;
		}
		else
		{
			SMeshComponent.SetStaticMesh(Mesh2);
			bMeshSwitch = !bMeshSwitch;
		}
		SwitchDelay = WorldInfo.TimeSeconds + 3;
	}
}

So in the tick we’ve got a call to a SwitchMesh() function. Right under tick we write our function. the first if statement controls how often our switch happens. When the if condition is met it will update the timer with SwitchDelay = WorldInfo.TimeSeconds + 3, so the +3 sets the next time the if statement should execute.
To switch on and off the second mesh, we use bMeshSwitch, and then when the if{}else{} statement is executed the statements will toggle the boolean from one to the other by using bMeshSwitch = !bMeshSwitch, read that as “bMeshSwitch is Not bMeshSwitch” also read that as “set bMeshSwitch to what it is not”

Now with the previous scene we’ll get updated with a ball that goes from smooth to spiky every three seconds.

Leave a Reply

You must be logged in to post a comment.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes