Combining Everything I’ve Learned So Far

Welcome back, I have spent a little over a month with unreal engine 4 and I have made a small demo to combine everything I have learned. I have actually done a lot since the last update but I will not go super into detail for it all, I will try to condense it so it makes sense.

We will mainly be focusing on how I got the UI elements to update today

To start off, I created 2 widgets, one for showing how many notes are collected and one for how many cubes are destroyed. The making of the widget is very easy, just drag the text element where ever you want and make sure you tick the “variable” box on the right next to the name.

To actually start counting the notes that we collect and the boxes we destroy, we need a way to actually know when one of those events has ben done. In the FirstPersonCharacter blueprint, there are 2 custom events.

Figure 1 Collected Note and Cube Destroyed customer events

We have a variable called notes collected and cubes destroyed. When the event is triggered, it simply adds an increment of 1 to that variable. We also need to program when this even should be triggered. This is relatively easy. In the note blueprint, whenever the note is collected, the Collected Note event is called. We have to make sure to give the event trigger a reference to the first person character. The Cubes Destroyed event is triggered in a similar way in the cube blueprint.

To get our widget and events here to communicate with each other, we need to talk about event dispatchers. Events can be bound to an event dispatcher, this event dispatcher will have events tied to it, when it is called, those events will execute. In our widget blueprint, we need to execute this event dispatcher, and take the variable that is “Number Of Cubes Destroyed” and use that value to update the variable text we wrote previously.

Figure 2 Notes Collected widget blueprint

As you can imagine, we do something very similar for the cubes destroyed event. From the level blueprint, we can create this widget and add it to the viewport.

I have also added a system that spawns cubes at random positions within a given area, at slightly different times and with different colours. This was actually not very easy to do but I am happy with the final result.

Figure 3 Cube Spawn Loop

To make sure the cubes spawn at random positions, I went into the level blue print and created this loop as you see above. I had to create somewhat of a custom node which is the For Loop With Delay Node. This involved going into a macro library and configuring things from there. As you can see, there is a bounding box with a set of coordinates and that is connected into the spawn actor cube. This ensures that the cubes will spawn at a random point within that box. The boxes also spawn with random colours which is easy. In the construction script of the cube, we need to create a dynamic material instance and set the parameter value for based on random float values. In the material editor, we have made the base colour a 3 value parameter. There is a node called random float which makes this process rather easy.

Figure 4 Cube construction script to spawn with random colours

That is the bulk of it, I will leave you with a video showcasing what the demo looks like.

Thanks for reading.
Saarim