Door Package - a short tutorial using the official package

Door Package (Using the Official Package)

Description

A short tutorial using the official Door Package from crayta. A good intro for basic and beginning craytors on a simple door.



Placing the door in the world

The package comes with a template and a script:

  • Game Round Door (template)
  • doorScript
Game Round Door doorScript

Using the Template

Place the template in the world. It will have a new name something like gameRoundDoor1. As a template it will have doorScript already attached. Note in the doorScript the startState is closed.

Preview the game and the door is closed. If you click on the door with [E] keyboard or controller, the door does not open. No logic has been setup for OnInteract etc.

Game Round Door door in world

On Interact

The doorScript has several functions to which can be called OnInteract. These can also be called OnCollision or OnTriggerEnter when using a trigger; not used in this example but should give an idea what can be expanded upon in the future.

This example will use On Interact to toggle the door from closing to opening and vice versa.

  • Under On Interact, add an event and either drag and drop the entity aka door into the *Entity field.
  • the Script field should auto populate as there is only one script on the door.
  • the Event field can be any function in the doorScript, for this example use Toggle

Preview the game and test it with Toggle. Notice how the door will now transition from opening to closing, etc.



Other Notes

note: SetOpen or SetOpening will have similar effects to open the door but no way to close it.

There are other official tutorials on opening doors with triggers or pads such as:

Andy’s has video series on scripting with with opening door logic. It’s a very good introduction for beginer’s to scripting.



Advanced Door Script

this script for the door adds audio and animation controls.

local DoorScript = {}

-- Script properties are defined here
DoorScript.Properties = {
	{ name = "startState", type = "string", default = "closed", options = { "open", "opening", "closing", "closed", }, },
	{ name = "animationTime", type = "number", min = 0, default = .5, tooltip = "time in seconds", },
	{ name = "openSound", type = "soundasset", },
	{ name = "closeSound", type = "soundasset", },
	{ name = "soundVolume", type = "number", min = 0, default = 1, },
	{ name = "fadeInOut", type = "number", min = 1, default =1, },

}

function DoorScript:Init()
	self:SetState(self.properties.startState)
end

function DoorScript:SetState(state)
	self.state = state
	self:GetEntity():PlayAnimation(self.state, {
		looping = false, playbackTime = self.properties.animationTime, positionFactor = 0.5 
	})
	self:DoorSound(self.state)
end

function DoorScript:SetOpen()
	self:SetState("open")
end

function DoorScript:SetOpening()
	self:SetState("opening")
end

function DoorScript:SetClosed()
	self:SetState("closed")
end

function DoorScript:SetClosing()
	self:SetState("closing")
end

function DoorScript:Toggle()
	if self.state == "open" or self.state == "opening" then
		self:SetState("closing")
	else
		self:SetState("opening")
	end
end

function DoorScript:DoorSound(state)
	self:Schedule(function()
		if state == "opening" and self.properties.openSound ~= nil then
			local soundHandle = self:GetEntity():PlaySound(self.properties.openSound, self.properties.fadeInOut)
			soundHandle:SetVolume(self.properties.soundVolume)
			Wait(self.properties.animationTime)
			soundHandle:Stop()
		elseif state == "closing" and self.properties.closeSound ~= nil then
			local soundHandle = self:GetEntity():PlaySound(self.properties.closeSound, self.properties.fadeInOut)
			soundHandle:SetVolume(self.properties.soundVolume)
			Wait(self.properties.animationTime)
			soundHandle:Stop()
		end
	end)
end

return DoorScript