Checkpoint Homepoint Package

Checkpoint Homepoint Package

- Description -

Despawn/Spawn Players with effects. An easy to deploy checkpoint system for adventure or open world style games. Optional checkpoints for OnDeathPlaneTrigger and Homepoints for death. Homepoint save (optional).


- Features -

  • Checkpoints
  • Homepoints
  • Transporter (bonus)
Example: Game play with full options result
player spawns and falls off or dies respawn on where spawned in
player runs over homepoint and falls off respawns on last homepoint
player runs over checkpoint and falls off respawns on last checkpoint
player dies before getting to new homepoint respawns on last homepoint
player logs off and on respawns on last homepoint
transport to and from does not save checkpoint or homepoint

note: the difference between dying and falling off


- Install Packages -

  • Auto-Repawn (official package)
  • Checkpoint Homepoint
  • (optional) Auto-Save (official package)
  • (optional) Health (official package)
Auto-Respawn Checkpoint Homepoint

- Configure -

Player Template

  • add template Player Respawn to Player template
  • (optional) rename to Respawn
Player Template Respawn Folder

User Template

  • add template User Spawn to User template
  • (optional) rename to Spawn

Spawn Script Folder - spawnUserScript

  • Spawn Point - set startlocation
  • Player Template - (not needed if only 1 player template)
  • Respawn Delay Time - adjust time as desired
  • Spawn on Login - check for true
    • not needed if using (optional) spawnSaveScript
    • spawnSaveScript will spawn player

Spawn Script Folder - spawnPlayerScript

  • (optional) add spawnPlayerScript
  • uses DespawnPlayerWithEffect
  • default is no effect OnPlayerDied if this is not added

Spawn Script Folder - spawnSaveScript

  • (optional) add spawnSaveScript
  • on user login, spawn player at last saved homepoint
  • requires AutoSave (see below)
User Template Spawn Folder

- Auto-Save -

  • (optional)
  • create Script folder AutoSave under User Template
  • add autoSaveScript to folder
  • On Load add
    Spawn / spawnSaveScript / OnLoad
  • On Save not used as SetSpawn is used; not scheduled
  • Save Version required - set to 1 initially
  • required autoLoadScript on locator in world
User AutoSave Folder AutoSave Scripts
autoLoad in World Tree autoLoadScript

- Checkpoint Homepoint Locators -

Terminology:

  • checkpoints are a respawn spot for OnDeathPlaneTrigger
  • homepoints are returned to upon death or onlogin

Usage:

  • add checkpointScript to anylocator
  • requires an entity for: (see screenshots)
    • OnCollision
    • OnInteract
    • OnTriggerEnter
  • Save Homepoint
    • check to save on death or onlogin (spawnSaveScript)
  • Default checkpoints are set to be homepoints
    • uncheck for OnDeathPlaneTrigger checkpoints
    • (optional) spawnPlayerScript needed for effects
  • Health Package
    • players will fully heal by default
    • (optional) - healthScript is changed (see below)
      • to retain hp after falling
      • may still count as playerDeath

- spawnUserScript -

(optional) SpawnPlayerWithEffect

  • by default players do not resapwn with effects
  • search and replace
    • SpawnPlayer
    • SpawnPlayerWithEffect
  • lines 126 and 129 (manual method)

- healthScript -

(opitonal) do not reset HP OnDeathPlaneTrigger

  • change code Init
  • add On Died event OnPlayerDied spawnPlayerScript on User
  • no heal script provided - too many ways/options
    blue
-- called when the player entity is created (the player is spawned)
function HealthScript:Init()
	Print("Health:Init")
	--self.properties.hp = self.properties.maxHp
	if self:GetEntity():GetUser().spawnPlayerScript then
		self.properties.hp = self:GetEntity():GetUser().spawnPlayerScript.hp
	end
	if self.properties.hp <= 0 then
		self.properties.hp = self.properties.maxHp
	end
	self.startPos = self:GetEntity():GetPosition()
end
Player Template
Health Script Folder
heatlhScript
On Died

- transportPlayerScript -

bonus - not needed or required for main package

  • add transportPlayerScript to locator
  • add event to entity - example is trigger OnInteract
  • not shown is the interact prompt script
  • no time delay added as this was OnInteract

- Scripts -

provided for reference 
CheckpointScript
local CheckpointScript = {}

-- Script properties are defined here
CheckpointScript.Properties = {
	{ name = "saveHomePoint", type = "boolean", default = true, 
		tooltip = [[
		save checkpoint for all spawning 
		uncheck for OnDeathPlaneTrigger spawning
		]], },
}

function CheckpointScript:SaveCheckpoint(player)

	-- sends data to spawnUserScript on User to set spawnPoint
	-- verifies user is alive vice dead on the pad or trigger or interact
	if player and player:IsAlive() then
		if self.properties.saveHomePoint then
			player:GetUser():SendToScripts("SetSpawn", self:GetEntity())
		end
		-- used for OnDeathPlaneTrigger to respawn on that checkpoint
		player.respawnPlayerScript.properties.checkpoint = self:GetEntity()
	end
	
end

return CheckpointScript
RespawnPlayerScript
local RespawnPlayerScript = {}

-- Script properties are defined here
RespawnPlayerScript.Properties = {
	{ name = "checkpoint", type = "entity", editable = false, },
}

function RespawnPlayerScript:OnDeathPlaneTrigger()

	-- if this script exists, run custom function, else default method
	if self:GetEntity():GetUser().spawnPlayerScript then
		-- script named function to DespawnPlayerWithEffect and SpawnInternal
		self:GetEntity():GetUser().spawnPlayerScript:DespawnSpawnPlayer(self.properties.checkpoint)
	else
		-- if the above script is not found, respawn the player this way without effect
		self:GetEntity():GetUser():SendToScripts("OnPlayerDied", self:GetEntity(), nil)
	end
end

return RespawnPlayerScript
SpawnPlayerScript
local SpawnPlayerScript = {}

-- Script properties are defined here
SpawnPlayerScript.Properties = {
}

function SpawnPlayerScript:DespawnSpawnPlayer(checkpoint)
	self:Schedule(function()
		-- switch to action camera - orbit cam bug	
		self:GetEntity():GetPlayer().cameraType = 1
		
		-- save original locator
		local spawnPoint = self:GetEntity().spawnUserScript:GetSpawn()
		
		-- save hp if healthScript exists
		if self:GetEntity():GetPlayer().healthScript then
			self.hp = self:GetEntity():GetPlayer().healthScript.properties.hp
		end
		-- despawn player with effect
		self:GetEntity():DespawnPlayerWithEffect()
		Wait(2)
		
		-- if checkpoint has no value, spawnUserScript will use default spawnPoint
		if checkpoint then 
			print("Transport Player to: ", checkpoint:GetName())
			self:GetEntity().spawnUserScript:SetSpawn(checkpoint)
		end
		-- spawn player -- spawnUserScript can be changed to SpawnPlayerWithEffect
		self:GetEntity().spawnUserScript:SpawnInternal()
		Wait(1)
		
		-- reset spawnpoint back (needed for transporter)
		self:GetEntity().spawnUserScript:SetSpawn(spawnPoint)
		
	end)
end

function SpawnPlayerScript:OnPlayerDied()
	self.hp = 0
end

return SpawnPlayerScript
SpawnSaveScript
local SpawnSaveScript = {}

-- Script properties are defined here
SpawnSaveScript.Properties = {
}

--This function is called on the server when this entity is created
function SpawnSaveScript:Init()
end

function SpawnSaveScript:OnLoad(callback, saveVersion)
	self:GetSaveData(
		function(data)
			print("dataver", data.saveVersion)
			if data and data.saveVersion == saveVersion then
				print("GetSaveData for SpawnSaveScript:", "ver = " .. data.saveVersion, "spawnPoint = " .. data.checkpoint)
				callback(data)
			end
		end
	)
end


function SpawnSaveScript:SetSpawn(spawnPoint)
	self:SetSaveData(
		{
			checkpoint = spawnPoint:GetName(),
			saveVersion = self:GetEntity().autoSaveScript.properties.saveVersion
		}
	)
end

return SpawnSaveScript
AutoLoadScript
local AutoLoadScript = {}

AutoLoadScript.Properties = {
}

-- called AdventureScript in the Adventure Starter Game
-- this script added support for the Auto-Respawn Package
-- placed a locator in the world tree called Adventure or AutoLoad or whatnot

-- simple OnUserLogin callback, sent by the game
-- when a user logs in. This one calls Load on autoSaveScript
-- and then spawns a player for the user when everything loaded...
function AutoLoadScript:OnUserLogin(user)
    
    user.autoSaveScript:Load(
        function(data)
        	-- if Auto-Respawn Package is not used, spawnplayer as normal
        	if user.spawnUserScript then
	        	-- find checkpoint in world
				local spawnPoint = GetWorld():Find(data.checkpoint)
				
				-- set spawnPoint via script name
				user.spawnUserScript:SetSpawn(spawnPoint)
				
	            Print("Adventure: Spawning")
	            user.spawnUserScript:SpawnInternal()
	        else
	        	user:SpawnPlayer()
	        end
        end    
    )
    
end

-- when a user leaves make sure they are saved...
function AutoLoadScript:OnUserLogout(user)
    user.autoSaveScript:Unload()
end

return AutoLoadScript
TransportPlayerScript
local TransportPlayerScript = {}

-- Script properties are defined here
TransportPlayerScript.Properties = {
	{ name = "destination", type = "entity", is = locator, },
}

--This function is called on the server when this entity is created
function TransportPlayerScript:Init()
end

function TransportPlayerScript:Transport(player)
	player:GetUser().spawnPlayerScript:DespawnSpawnPlayer(self.properties.destination)
end

return TransportPlayerScript