Jump to: navigation, search

Tutorial Spawning Players

Spawning Players

Before we start, you should be able to use the world editor and most importantly the Add Entity feature of it.

You should also be aware that if you are using the Tutorial or cloning another world, "Player Spawn Points" will already exist and you must either move these, or delete and add new spawn points as per the instructions below.

Enter the world editor, and select the "Add Entity" button.. and also click the "Free Cubes" placement mode:


Enter the world editor

Putting the editor in "Free Cubes" placement mode means we can place then entity in mid-air a very important thing to do to prevent players from spawning inside voxels and getting stuck. Where you place the entity is where the players feet will be when they spawn.

Selecting Player Spawn Entity

At this point you need to tell the editor that you want to place a "Player Spawn Point", you do this by using the mouse wheel to spin around the available entity types until you see "Player Spawn Point" - like this:


Placing the spawn point

As you are using "Free Cube" mode you should go ahead and place the entity where you wish in the world, such as:


Testing the spawn point

Now you can press "Run" to see your spawn point in action (you should spawn from it). If you do not spawn from it, then you may have an existing "Player Spawn Point" in the world that Cubiverse is picking, to find it simple respawn and immediately equip the world editor. The spawn point will be directly above where you are standing, so delete it.

Scripting the player spawn point

Spawning a single player at a single point is all and good, but Cubiverse is a multi-user game maker - this means you will have multiple players joining and you may want to affect how these new players are spawned. Whether it's simply selecting a few spawn locations or doing something more complex, you must edit the script that sites behind the "Player Spawn Point" entity to control the spawning event.

Some examples of complex operations may be spawning players further along the world if other players have completed certain parts of your world, or placing new players evenly on one team or another team.

First, select the "Edit Script" button and highlight the "Player Spawn Point Entity" like so (it turns white then selected:


When you click the mouse button you will be presented the script editor.


Understanding the Entity's Script

First some background on the entity - The "Player Spawn Point Entity" is tasked with spawning players WHEN a player joins the server (or a "reset" occurs, for example at the end of a run through the world.

The first line of the script...


What this is doing is creating a new object called "InitialPlayerSpawnEntity1" based on an object that is built into Cubiverse (PlayerSpawnEntity). This built-in object provides all the low-level functionality of spawning a player and allows Cubiverse to know that this object is special. There's a full api section on the PlayerSpawnEntity for those wanting to see all the options for this object.

For the record, you must also call a special API call (that's only available inside "Player Spawn Point Entities") called registerPlayerSpawnEntity(). This allows Cubiverse to link up your object with the entity, meaning when events happen on the entity they are passed through to your object.


onPlayerSpawn Event

To actually control the spawning of a player, you must call the spawnPlayer() API call when a "onPlayerSpawn" event occurs on the object.

For those of you new to Cubiverse, events are just really functions that you add to your object. When the event is "fired" by Cubiverse it calls into that function to notify you about the event.


As you can see from above we are extending our "InitialPlayerSpawnEntity1" object by adding a new function with the name "onPlayerSpawn". You must use this exactly function name as Cubiverse will be looking to call the function.

The interesting part is the parameter that is passed into your function, in this case "playerSpawned".

This is explained in detail in the API, but if you have multiple player spawn points then you want to give each spawn point the chance to spawn your player. Therefore regardless of whether a spawn point actually spawns the player, every spawn point is executed to tell them a player has spawned. The "playerSpawned" parameter (or variable) is set to false UNTIL the player is spawned by one spawn point. After that all other spawn points (who have not been fired yet) will be called with "playerSpawned" set to true.


As you can see from the default entity script, we do a simple check to see if the player has been spawned and then call the spawnPlayer() API call. If you are wondering why the "this." is required, that's because the API call spawnPlayer() exists only in the "PlayerSpawnEntity" object.


If you accidentally call spawnPlayer() more than once you will see errors messages in the server console.