The first puzzle room will continue to practice using vectors. Especially using the size of an offset vector to determine the distance between two objects.
Puzzle Overview
The first puzzle riddle is as follows:
Karl will only chase Kate if other suitors are nearby
This means that the player must stand near the Kate statue in order for the Karl statue to move over to Kate. Once they are close enough, the first puzzle will be complete and open the door to the second puzzle room.
Scripting Overview
Each puzzle room will have its own server script to run its logic and determine if certain conditions are met. There will be common logic required by all the puzzles so it makes sense to use an API script. This will allow sharing functions across multiple scripts and avoid unnecessary repetition of code.
What is an API?
API stands for application programming interface. In Core, it is a special script that can be loaded into other scripts using a custom property. This is useful because multiple scripts can load the API and access shared properties, functions, and other code.
API Example
Here’s a quick example of how an API works.
The API script saves its properties and functions to a table and returns it at the bottom.
The API script is in the Project Content window, but NOT in the Hierarchy.
A second script (in the Hierarchy) has the API as a custom property.
The second script uses the require function to access the API table of properties and functions.
Create an API Script
In the Project Content window, right click and add a new script. Name the script PuzzleAPI.
Create a Server Script
In the Hierarchy, add a new script named Puzzle1Logic to the Server Context within the Scripts folder.
Add Custom Properties
The Puzzle1Logic script will need several properties to detect if the player is in the room, move the statues, and use the API script.
Expand the Escape The Room Template and find the Puzzle 1 group. This will contain the objects required by the script. Then open the Properties window with the Puzzle1Logic script selected.
Add the following custom properties.
From the Project Content window, drag and drop the PuzzleAPI script as a custom property.
Drag and drop the Statue 1 object as a custom property.
Drag and drop the Statue 2 object as a custom property.
Drag and drop the Trigger object as a custom property.
Add a new custom property of type float, set the name to SolutionDistance, and value to 300.
Add a new custom property of type float, set the name to MoveSpeed, and value to 500.
Solution Distance
The SolutionDistance custom property is the minimum amount of distance of the two statues required for the puzzle to be completed.
It is also the minimum distance of the player and Statue 2 in order for Statue 1 to begin moving.
Starting Code
To start, all the custom properties will need to be assigned to variables. The Trigger will detect when a player enters and exits the room. There will also be a Tick function that will constantly call another function named MoveStatue if there is a player in the room. This function will later be filled in with the puzzle logic.
Double click the Puzzle1Logic script to open the Script Editor window. Add the following code:
local API = require(script:GetCustomProperty("PuzzleAPI"))
local STATUE_1 = script:GetCustomProperty("Statue1"):WaitForObject()
local STATUE_2 = script:GetCustomProperty("Statue2"):WaitForObject()
local TRIGGER = script:GetCustomProperty("Trigger"):WaitForObject()
local SOLUTION_DISTANCE = script:GetCustomProperty("SolutionDistance")
local MOVE_SPEED = script:GetCustomProperty("MoveSpeed")
local player = nillocalfunctionMoveStatue(player)print(player.name .. " is in Puzzle Room 1.")
endfunctionTick()if player then
MoveStatue(player)
endendlocalfunctionOnBeginOverlap(trigger, other)if other:IsA("Player") then
player = other
endendlocalfunctionOnEndOverlap(trigger, other)if other:IsA("Player") then
player = nilendend
TRIGGER.beginOverlapEvent:Connect(OnBeginOverlap)
TRIGGER.endOverlapEvent:Connect(OnEndOverlap)Code language:Lua(lua)
Preview the Project
Save the script and preview the project. When the player enters the first room, the Event Log window should print a message repeatedly until the player exits.
Missing Code
The MoveStatue function will need to check the distance of the player and statues. It will also need to calculate the velocity vector to move Statue 1 towards Statue 2.
This will be the pseudocode for the Puzzle 1 logic:
API Functions
Before implementing the puzzle logic, let’s add some helpful functions to the PuzzleAPI script. The puzzle will need to know the distance between two objects and the velocity vector from one object to another.
In the Project Content window, double click the PuzzleAPI script to open the Script Editor window. Add the following code:
In the GetOffsetVector function, the two position vectors being subtracted are being converted to a Vector2. This is because the distance does not need to consider the Z axis. It is as if the vectors are being calculated on a 2D plane using the top-down view of the 3D project.
In the GetTargetVelocty function, the Vector2 offset vector is being converted back to a Vector3 because the move function (will be added in future steps) is expecting a Vector3 even if the Z component is 0.
MoveStatue Function
With the pseudocode planned and API functions created, the puzzle logic is now ready to be implemented. The statue will be moved and stopped using CoreObject functions.
Open the Puzzle1Logic script. Add the following code inside the MoveStatue function.
localfunctionMoveStatue(player)local playerDistance = API.GetDistance(player, STATUE_2)
local statueDistance = API.GetDistance(STATUE_1, STATUE_2)
local velocity = API.GetTargetVelocity(STATUE_1, STATUE_2, MOVE_SPEED)
if statueDistance <= SOLUTION_DISTANCE then
STATUE_1:StopMove()
Events.Broadcast("OpenPuzzleDoor", "1")
elseif playerDistance <= SOLUTION_DISTANCE then
STATUE_1:MoveContinuous(velocity)
else
STATUE_1:StopMove()
endendCode language:Lua(lua)
Preview the Project
Save the scripts and preview the project. The first statue should move towards the second statue if the player is nearby. Eventually, the puzzle door should open once the statues get close enough.
This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Strictly Necessary Cookies
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.
3rd Party Cookies
This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.
Keeping this cookie enabled helps us to improve our website.
Please enable Strictly Necessary Cookies first so that we can save your preferences!