Back

Custom Portal

This is a custom portal that allows the user to teleport to any Entity location and be able to set hidden portals. It will also fade and with different ease styles and play sound if a SoundComponent is added to the Entity.


Properties:


How to use:


The package includes a Model named "Model_Portal" that have the basic components added to it. (Custom Portal and InteractionComponent)


On the Custom Portal component, set the UILoadingGroup and EntityRef.


You will need to add "MapManager" component to the World's common.


Add a SpriteGUIRendererComponent to the loading UI group and set it's color to black (or any color you want)



Video for setup:

v1.0

Download Package

structCustomPortal
Script
Plain Text

--@ BeginProperty
--@ SyncDirection=None
Entity UILoadingGroup = "nil"
--@ EndProperty

--@ BeginProperty
--@ SyncDirection=None
EntityRef EntityRef = "nil"
--@ EndProperty

--@ BeginProperty
--@ SyncDirection=None
Vector3 Offset = "Vector3(0,0,0)"
--@ EndProperty

--@ BeginProperty
--@ SyncDirection=None
number FadeInDuration = "1"
--@ EndProperty

--@ BeginProperty
--@ SyncDirection=None
string EaseType = """"
--@ EndProperty

--@ BeginProperty
--@ SyncDirection=None
boolean HiddenPortal = "false"
--@ EndProperty

--@ BeginMethod
--@ MethodExecSpace=ClientOnly
void PlaySound()
{
local sound = self.Entity.SoundComponent
if isvalid(sound) then
	sound:Play()
end
}
--@ EndMethod

--@ BeginMethod
--@ MethodExecSpace=ClientOnly
void UIFadeIn(string userID)
{
if _UserService.LocalPlayer.OwnerId ~= userID then return end

if not isvalid(self.UILoadingGroup) or not isvalid(self.UILoadingGroup.CanvasGroupComponent) then
	self:PrepareTeleport(userID)
	return
end

self.UILoadingGroup.Enable = true
local canvasGroupAlpha = self.UILoadingGroup.CanvasGroupComponent.GroupAlpha

local tween
local tweenAlpha = function(tweenValue)
	self.UILoadingGroup.CanvasGroupComponent.GroupAlpha = tweenValue
end

local tweenType = self:EaseTypeFromString(self.EaseType)

tween = _TweenLogic:MakeTween(0, 1, self.FadeInDuration, tweenType, tweenAlpha)
tween.AutoDestroy = true
tween:SetOnEndCallback(function() 
	self:PrepareTeleport(userID)
end)
tween:Play()

_TimerService:SetTimerOnce(function()
	
	for _index, object in ipairs(self.UILoadingGroup.Children) do
		object.Enable = true
	end
	
end, self.FadeInDuration)
}
--@ EndMethod

--@ BeginMethod
--@ MethodExecSpace=ClientOnly
void UIFadeOut(Entity player)
{
if not isvalid(self.UILoadingGroup) then return end
if not isvalid(self.UILoadingGroup.CanvasGroupComponent) then return end

local tween
local tweenAlpha = function(tweenValue)
	self.UILoadingGroup.CanvasGroupComponent.GroupAlpha = tweenValue
end

local tweenType = self:EaseTypeFromString(self.EaseType)

tween = _TweenLogic:MakeTween(1, 0, 1, tweenType, tweenAlpha)
tween.AutoDestroy = true	
tween:Play()

for _index, object in ipairs(self.UILoadingGroup.Children) do
	object.Enable = false
end

self:DisablePlayer(player, false)
}
--@ EndMethod

--@ BeginMethod
--@ MethodExecSpace=Server
void PrepareTeleport(string userID)
{
local data = PortalData()
local player = self.Entity.CurrentMap:GetChildByName(userID)

if isvalid(self.EntityRef) then
	local position = _EntityService:GetEntity( self.EntityRef.EntityId ).TransformComponent.Position
	position = position + self.Offset
	local path = self.EntityRef.Path:match("/maps/([^/]+)/")
	self:TeleportPlayer(player, position, path)
else
	self:TeleportFailed(player)
end
}
--@ EndMethod

--@ BeginMethod
--@ MethodExecSpace=ServerOnly
void TeleportPlayer(Entity player,Vector3 position,string path)
{
_TeleportService:TeleportToMapPosition(player, position, path)
}
--@ EndMethod

--@ BeginMethod
--@ MethodExecSpace=Client
void TeleportFailed(Entity player)
{
log_warning("Teleport EntityRef Missing!")
self:UIFadeOut(player)
}
--@ EndMethod

--@ BeginMethod
--@ MethodExecSpace=ClientOnly
any EaseTypeFromString(string ease)
{
return EaseType[ease] or EaseType.Linear
}
--@ EndMethod

--@ BeginMethod
--@ MethodExecSpace=ClientOnly
void DisablePlayer(Entity player,boolean disable)
{
if not isvalid(player) then return end

local playerHit = player.PlayerHit
if isvalid(playerHit) then
	playerHit.Enable = not disable
end

local playerControllerComponent = player.PlayerControllerComponent
if isvalid(playerControllerComponent) then
	playerControllerComponent.Enable = not disable
end
}
--@ EndMethod

--@ BeginMethod
--@ MethodExecSpace=ClientOnly
void OnBeginPlay()
{
if self.HiddenPortal then
	if not isvalid(self.Entity.SpriteRendererComponent) then return end
	self.Entity.SpriteRendererComponent.Color.a = 0
end
}
--@ EndMethod

--@ BeginEntityEventHandler
--@ Scope=Client
--@ Target=self
--@ EventName=InteractionEvent
HandleInteractionEvent
{
local player = event.InteractionEntity
local userID = player.PlayerComponent.UserId

if _UserService.LocalPlayer.OwnerId ~= userID then return end

self:PlaySound()

local currentMap = self.EntityRef
if isvalid(currentMap) then
	self:PrepareTeleport(userID)
	return
end

self:UIFadeIn(userID)
self:DisablePlayer(player, true)
}
--@ EndEntityEventHandler

--@ BeginEntityEventHandler
--@ Scope=Client
--@ Target=self
--@ EventName=InteractionEnterEvent
HandleInteractionEnterEvent
{
if not self.HiddenPortal then return end
if _UserService.LocalPlayer ~= event.InteractionEntity then return end
if not isvalid(self.Entity.SpriteRendererComponent) then return end

local tween
local tweenAlpha = function(tweenValue)
	self.Entity.SpriteRendererComponent.Color.a = tweenValue
end

tween = _TweenLogic:MakeTween(0, 1, 0.2, EaseType.Linear, tweenAlpha)
tween.AutoDestroy = true
tween:Play()
}
--@ EndEntityEventHandler

--@ BeginEntityEventHandler
--@ Scope=Client
--@ Target=self
--@ EventName=InteractionLeaveEvent
HandleInteractionLeaveEvent
{
if not self.HiddenPortal then return end
if _UserService.LocalPlayer ~= event.InteractionEntity then return end
if not isvalid(self.Entity.SpriteRendererComponent) then return end

local tween
local tweenAlpha = function(tweenValue)
	self.Entity.SpriteRendererComponent.Color.a = tweenValue
end

tween = _TweenLogic:MakeTween(1, 0, 1, EaseType.Linear, tweenAlpha)
tween.AutoDestroy = true
tween:Play()
}
--@ EndEntityEventHandler
            
structMapManager
Script
Plain Text

--@ BeginProperty
--@ SyncDirection=None
Entity UILoadingGroup = "nil"
--@ EndProperty

--@ BeginProperty
--@ SyncDirection=None
number WorldLoadingWaitDelay = "1"
--@ EndProperty

--@ BeginProperty
--@ SyncDirection=None
number FadeOutDuration = "1"
--@ EndProperty

--@ BeginProperty
--@ SyncDirection=None
string EaseType = """"
--@ EndProperty

--@ BeginMethod
--@ MethodExecSpace=ClientOnly
void UIFadeOut()
{
local tween
local tweenAlpha = function(tweenValue)
	self.UILoadingGroup.CanvasGroupComponent.GroupAlpha = tweenValue
end

local tweenType = self:EaseTypeFromString(self.EaseType)

tween = _TweenLogic:MakeTween(1, 0, self.FadeOutDuration, tweenType, tweenAlpha)
tween.AutoDestroy = true
tween:SetOnEndCallback(function() 
	self.UILoadingGroup.Enable = false
end)
	
tween:Play()
	
for _index, object in ipairs(self.UILoadingGroup.Children) do
	object.Enable = false
end

self:DisablePlayer(false)
}
--@ EndMethod

--@ BeginMethod
--@ MethodExecSpace=ClientOnly
any EaseTypeFromString(string ease)
{
return EaseType[ease] or EaseType.Linear
}
--@ EndMethod

--@ BeginMethod
--@ MethodExecSpace=ClientOnly
void DisablePlayer(boolean disable)
{
local player = _UserService.LocalPlayer
if not isvalid(player) then return end

local playerHit = player.PlayerHit
if isvalid(playerHit) then
	playerHit.Enable = not disable
end

local playerControllerComponent = player.PlayerControllerComponent
if isvalid(playerControllerComponent) then
	playerControllerComponent.Enable = not disable
end
}
--@ EndMethod

--@ BeginEntityEventHandler
--@ Scope=Client
--@ Target=localPlayer
--@ EventName=EntityMapChangedEvent
HandleEntityMapChangedEvent
{
if not isvalid(self.UILoadingGroup) then return end
if not isvalid(self.UILoadingGroup.CanvasGroupComponent) then return end

_TimerService:SetTimerOnce(function()
	self:UIFadeOut()
end, self.WorldLoadingWaitDelay)
}
--@ EndEntityEventHandler
            
avatar