Adventure Game Opening Chests
Objective
- Open a chest and observe the chest opening.
- Display the contents.
- Hide the chest after x time.
- Pick up the contents.
Starter: Adventure Game
- this starter game has the basics required for this tutorial.
Additional Packages
- Door Package (Official Crayta Package)
Introduction
This tutorial will use the pickupSpawnerScript on a locator to control the chest visibility using show and hide functions. The doorScript will be used to control the opening and closing of the chest. The eventScheduleScript will be used to control the above scripts and timing.
Building the Chest and Apple
- place a locator in the world
- place a chest on the locator
- place the resourceSpawner template aka Apple on the chest
Adding Scripts
For ease of visibility, script folders will be used to identify script placement and access. Add the following script folders to each entity, and associated scripts with settings:
Locator
-
.locator pickupSpawner
-
pickupSpawnerScript
- respawning = false
- useSelfAsTemplate = false
- showOnInit = true
-
pickupSpawnerScript
note:
- use of
.locator
name to show it at the top. - easier to see with child objects.
Chest
-
largeChest door
-
doorScript
- defaults i.e. closed
-
doorScript
This is an Event Container or Array of Events with a delay or timer beteen sets.
- largeChest eventSchedule
-
eventScheduleScript
-
eventSet 1
- largeChest door / doorScript / SetOpening
- resourceSpawner1 / pickupSpawnerScript / ShowPickup
-
eventSet 2
- largeChest door / doorScript / SetClosed
- .locator pickupSpawner / pickupSpawnerScript / HidePickup
-
WaitTime = 1
- wait 1 seconds between events
- adjust as desired
-
eventSet 1
Event Schedule Script
local EventScheduleScript = {}
EventScheduleScript.Properties = {
{ name = "eventSet", type = "event", container = "array", tooltip = "do set 1 and then wait, etc.", },
{ name = "waitTime", type = "number", min = 0, tooltip = "time to wait between events", },
}
function EventScheduleScript:Init()
end
function EventScheduleScript:DoEvents(player)
local events = self:ReturnTable(self.properties.eventSet)
self:Schedule(function()
for _, event in pairs (events) do
event:Send(player)
Wait(self.properties.waitTime)
end
end)
end
function EventScheduleScript:ReturnTable(arrayName)
local tempTable = {}
for i=1, #arrayName do
table.insert(tempTable, arrayName[i])
end
return tempTable
end
return EventScheduleScript
-
largeChest interact
-
interactMsgScript
- InteractMsg = Open Chest
-
interactMsgScript
interactMsgScript
local InteractMsgScript = {}
InteractMsgScript.Properties = {
{ name = "interactMsg", type = "text", },
}
function InteractMsgScript:Init()
end
function InteractMsgScript:GetInteractPrompt(prompts)
prompts.interact = self.properties.interactMsg
end
return InteractMsgScript
Resouce Spawner
-
pickupSpawnerScript
- respawning = false
- showOnInit = false
- respawning = false
Setting Things in Motion
With the above logic in place, preview the game. The chest does not open when interacting with it. One additional piece is required; setting up OnInteract on the chest itself. Drag the script folder largeChest eventSchedule onto the OnInteract Entity Field, and select the function DoEvents.
Chest
-
On Interact
- largeChest eventSchedule / eventScheduleScript / DoEvents
Supporting Images
pickupSpawnerScript | largeChest OnInteract |
---|---|
|
Preview
- the chest should appear in the game
- click on the chest to see it open
- the apple is in the chest
- the chest will hide in 1 seconds
- the apple will remain
Respawning chests
- not shown in this tutorial
There are multiple ways of setting things in motion with an event schedule.
This game is published and remixable for the curious and/or brave.