- when trigger is entered by player
- respawn player
- Spawn Point
- Trigger Zone
- Script Gizmo
This is the most basic script you will ever write in Horizon Worlds and one of the most important too. Every world you created will contain a variation of this script. We will take this time to explain some basic concepts as well.
A Script Gizmo by itself does nothing. It will not run or execute any code until it is attached to an object. To attach a script to any object, open the property window for that object and click the dropdown at the very bottom of the window and select the script you wish to attach. As long as your world is running, your script will now run too. Also, keep in mind that a script can be attached to multiple objects. This will create multiple instances of the same script, so no need to make copies of scripts that do the same function.
For our first lesson, we need to attach our script gizmo to a Trigger Zone gizmo. So go ahead and pull out a Trigger Zone gizmo and a Script Gizmo if you haven't already. Open the property window for your Trigger Zone gizmo and attach your script.
Now open the property window for your Script Gizmo and you will see the scripting panel. First, let us name of script something like "RespawnScript" at the top left corner of the scripting panel. The right side of the scripting panel contains all of the events, actions, operators, and variables we will use to create our scripts.
You should be looking at a list of events by default and you want to find one that says when trigger is entered by player under the Player Events category. Drag that to the left side of your script and drop it anywhere in the black space. If the script already contains an event called when world is started, you may delete that event by clicking and holding it while pressing down on your right joystick.
Now that we have our event in place, we need to add an action under it. In the Motion tab(2nd tab) on the right side of the scripting panel, you see an icon with 4 arrows pointing in each direction. This tab contains our Motion codeblock. Under the Player Motion section, you should find one called respawn player. Click and drop this directly under our when trigger is entered by player event.
Now we're nearly done, but we need to take a moment and talk about variables here. The event we pulled out earlier, when trigger is entered by player, comes with a Player variable that tells us who entered the trigger. This type of variable that is given along with an event can only be used within the scope of this event. So you cannot use this Player variable anywhere else in the script outside of the event it was given. This is known as a private variable.
We can also create our own variables in the last tab on the right side of the scripting panel. Variables created here can be used anywhere in this script. This is called a public variable. Private variables can be assigned to a public variable, but we won't be doing that in this script.
Now going back to the respawn player action that we pulled out and placed under our event earlier is missing some information. It needs to know the player we want to respawn, and it wants to know which Spawn Point gizmo we want to respawn the player to. Satisfying the missing player variable is easy. We can drag the player variable from the when trigger is entered by player event down to the first empty space in our respawn player actions. We know this the player who entered the trigger is also the player who needs to be respawned, but now we need to tell our script where to respawn them to.
You can only respawn a player to an existing Spawn Point gizmo. you should already have an existing Spawn Point gizmo in your world by default. You can also pull out a new one or a second one to use instead.
Let us create a public variable that we will use to reference this Spawn Point gizmo. On the right side of your scripting panel, go to the last tab and click on New Variable. A dialog box will appear, give this variable a name like "mySpawnPoint" and choose the type "Object". All gizmos are considered Object variables by the script. Once created we can now drag the mySpawnPoint object variable over to the missing field in our respawn player action and you're script is ready, but we need to do one last thing.
Back to the property window for your Trigger Zone gizmo, if our newly created variable does not appear at the bottom of the property window then close it and reopen it. Now you should see the variable with the words "Empty". Open the property panel for the Spawn Point gizmo you want your player to respawn to and drag the blue pill found near the bottom of the property window to the Empty field in your Trigger Zone gizmo. Now they should be linked and you are ready to test your script.