Notifications (cliff notes) - using the official Notifications Package

Notifications (cliff notes) - using the official Notifications Package

Install

Notifications Package

  • search for notifi
  • check Crayta Packages
  • click Install
Notifications Package

Configure

User template

  • drag and drop User Notifications template onto User template
  • (optional) rename to Notifications
User Template

Overview

Notifications defaults:

  • shout
    • only visible to the user
    • displayed top center of screen by default
  • chat
    • visible to the user specified in the function call
    • displayed right center of the screen by default

Script examples

Notifications functions:

  • Shout(msg, time, values)
  • AddNotification(msg, values)
  • OnChatMessage(user, message)

Shout

Example based Adventure Land: Stream Blue Print

Objective: When a player enters an area, display the area name on screen to the user.

  • use a locator to control the position of the trigger
  • use a trigger to determine the size of the area to be covered
  • optional
    • make a template for repeated use and placement of notifications
    • not covered in this tutorial

notification locator creation checklist

  • drop a mesh “vending machine” on world
    • (named shop1 in example)
    • used for preview
  • drop a locator on world just in front of “vending machine”
  • rename locator to notification
  • add script
    • sendNotificationShoutScript
    • see code below for script text
  • configure script properties on locator
    • Msg Type = shout
      • notification is AddNotification function
      • see screenshot below for example output
    • Message = Now entering the box
    • Display Time = 3
  • add trigger under notification
  • configure trigger
    • Position to 0, 0, 150
    • Size to 400, 400, 300
    • On Trigger Enter
      notification / sendNotificationShoutScript / SendToUser
  • Preview
    • walk toward the vending machine (shop1)
    • notice the notification appear entering the trigger box
locator initial loctor named notification

sendNotificationShoutScript

local SendNotificationShoutScript = {}

-- Script properties are defined here
SendNotificationShoutScript.Properties = {
	{name = "msgType", type = "string", 
        options = { "shout", "notification" }, },
	{name = "message", type = "text", },
	{name = "displayTime", type = "number", default = 3, },
}

function SendNotificationShoutScript:SendToUser(player, optionalValues)
    if self.properties.message ~= "" then
        if self.properties.msgType == "shout" then
            player:GetUser():SendToScripts("Shout", 
                self.properties.message, 
                self.properties.displayTime, 
                optionalValues)
        else
            player:GetUser():SendToScripts("AddNotification", 
                self.properties.message, 
                optionalValues)
        end
    end
end

return SendNotificationShoutScript
sendNotificationShoutScript on notification locator
trigger position trigger size and On Trigger Enter
locator position trigger box
Preview Shout
Preview AddNotification

note: Adventure Land has larger trigger boxes for entering the base, the farm, the forest, the cave, etc. for the messages.

Chat

Advanced use: See Team Chat Package

Objective: When a player enters an area, display message to all users who entered the trigger.

  • use the locator and trigger from above example
  • script can be on the locator or trigger

Notes

  • this is a simple script
  • uses OnTriggerEnter function
    • only works on scripts attached to triggers
    • On Trigger Enter need only if script on another entity
  • message is hard coded
    • best practice is to use a property
    • easier to change as a property
  • trigger
    • used for ease of demonstration
    • triggers maybe better for shout

Example

  • create a new script
    • sendUserMessageScript
    • see code below for script text
  • attach to trigger above
  • Preview:
    • on trigger enter
    • see the chat window appear with the username and message

sendUserMessageScript

local SendUserMessageScript = {}

-- Script properties are defined here
SendUserMessageScript.Properties = {
	-- Example property
	--{name = "health", type = "number", tooltip = "Current health", default = 100},
}

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

function SendUserMessageScript:OnTriggerEnter(entity)
	-- entity is entity entering the trigger
	-- check if is a player
	if entity:IsA(Character) then
		-- show username of player
		print(entity:GetUser():GetUsername())
		-- set user to players user using a local variable
		local userEntered = entity:GetUser()
		local msg = "This is a test"
		-- send a message to all users who entered trigger
		local users = GetWorld():GetUsers()
		for _, user in pairs(users) do
			user:SendToScripts("OnChatMessage", userEntered, msg)
		end
	end
end	

return SendUserMessageScript

sendUserMessageScript
Preview Chat Message