Spawn object based on player location

Hi guys,

Unreal engine noob here so I’m still figuring out how each block works in the blueprint editor.

I have an object moving across a platform at a specific speed (even tick) and it can move to the left/right only by a specific amount. I want a platform to spawn ahead of the object in either of the 3 positions seen in the image below. Once the object is on the next platform, the same pattern of spawning should repeat. There should also be a limit on the extreme ends since I would like the object to be within a boundary.

Also, how do I get these platforms to spawn randomly in combination with the above action.

Please help me with the blueprints

Note: The object and platform are two different blueprint classes.

First thing to do is check if the player has reached the goal number of platforms yet. If they have, do your win condition (whatever that may be):

After this, do another check. This time, we check whether the player is within an arbitrary distance. You should change this based on your behaviour. In my example, a new platform will spawn when the player is within 10 units of the current platform. Note that the incoming execution node is from the false node of the first branch in image 1:

Once we’ve decided we want to spawn a platform we call an event dispatcher. The reason we do this is that it allows us to break down the way we spawn each platform into a subroutine.

For example, let’s say you have enemies in your level and in the harder levels you want to spawn platforms further ahead with enemies on them. It would be annoying and inefficient to redo all of our blueprints so instead we just “call” (or rather, execute) that subroutine:

What this does is as follows:

First, we roll 3 random integers of 0, 1 or 2. We set a vector variable, PlatformRelativeOffset, based on the result (a better implementation would be to have a constant value which is subtracted or added based on location for X and a constant value of Y). We then add this vector to the current platform vector to give the new location. Note that in the screenshot above you should change your “Class” node to your platform.

Once we’ve set the new location we need to do some basic admin. We set the platform that we’ve just created as the current platform so that the next calculations are on the next platform, the one after that and so on. We then increment the value of “PlatformCount” to do the comparison check that you see in the first image (CONST_PLATFORM_LIMIT == PlatformCount) to decide whether the player has reached the end of the level.

If I’ve solved this for you, be sure to click the tick to the left. If you have any questions, please let me know.

  1. My goal was to have players compete based on a high score rather than reaching a final platform, so I guess there would be no win condition. Does the blueprint in the first and second image change with respect to that? Or is it unrealistic to have an infinite loop and just set a value for the final platform to be really high? (I hope this makes sense)

  2. The first and second image would fall under the class of the object and the third would fall under the platform, if I’m not wrong?

If there is no win condition, simply remove the branch where the condition is CONST_PLATFORM_LIMIT == PlatFormCount. However, if the game is infinite then there will be memory issues if we constantly just add the platforms and don’t clean them up. Therefore, we need to remove some of the platforms that won’t be used anymore.

Optimally you would use a First-in First-out structure such as std::queue to remove the platform that was placed into the container FIRST (i.e. the oldest platform is removed when a limit is reached).

However, if we were to just implement this with blueprints we’d do it like this:

251150-251034-ue4editor-2018-08-20-12-02-29.png

Next we’d check to see if that array is too big. Let’s say if the array is more than 30 objects in size, we’ll cull the 31st object:

251035-ue4editor-2018-08-20-12-04-00.png

We’ll make another subroutine for this as you may want to change the way it behaves. It’d look like this:

This:

Checks to see if the number of platforms is above the threshold. If it isn’t, nothing happens. If it is:

  • Get the first item of the array (the oldest, furthest away)
  • Delete the actor from the level
  • Remove the actor from the array

This check is also made in the array to make sure that the loop breaks when the number of platforms is below the required threshold. Without it, we would delete the entire array every frame.

The array should dynamically resize itself when remove is called (from documentation I’ve found)

In regards to the second question none of these blueprints would go inside of your object class - they should all go in a global blueprint such as a custom gamemode or the level blueprint.

Hope this helps.

When I was using a custom game mode, I couldn’t seem to call/Get ‘Current Platform’ like you did so I switched everything over to the level blueprint. I placed the object and the platform in the level which makes it possible to ‘Get’ it but I can’t seem to use the ‘Set’ function for ‘Current Platform’. When I play the level, the platform doesn’t seem to be spawning. Any idea where I’m going wrong?

Could you post a screenshot?

`][2]][1]

251150-251034-ue4editor-2018-08-20-12-02-29.png

In the first image, ‘Ball’ and ‘Floor’ (Platform) are classes and have blueprints in them which make them behave a certain way. I dragged both into the scene so that when I select either of them and open the level blueprint, I can add a reference to them. As you can see in the second and third image, where it’s required to ‘Get’ or ‘Set’ the Floor/Platform, I just get a ‘Cast to’ option.

But you have a ‘Current Platform’ which you are able to ‘Get’ (4th image) and ‘Set’ (5th image). So how did you make that possible? I think that seems to be the only reason the blueprints aren’t working. Or if you spot anything additional, please let me know.

You need to make your platform a class. Then you need to use “Spawn Actor From Class” and change the class to your platform.

On BeginPlay, you want to set your current platform as the one you’ve placed in the level already.

You need to untick “Context Sensitive” in order to set other options - it doesn’t always show up valid options. Others are available but are hidden.

Thanks a lot!