wiki.sine.space | sinespace

Difference between revisions of "Scripting/SAvatar"

From wiki.sine.space
Jump to: navigation, search
m
Line 332: Line 332:
  
 
OnClick = function()
 
OnClick = function()
StartCoroutine(TripleSpeedCoroutine)
+
Space.Host.StartCoroutine(TripleSpeedCoroutine)
 
end
 
end
  

Revision as of 14:54, 12 August 2021

The SAvatar class refers a single player avatar active within the scene. This only contains active players with a valid network connection, and does not include NPCs. ((PlayerAvatar being used below is just an example, as it returns an SAvatar.))

Members

Detach

void Detach ();

Detaches the avatar from whatever it may be attached to (only works for the player avatar), including chairs, vehicles and so forth.

Space.Scene.PlayerAvatar.Detach()


AttachTo

void AttachTo (SGameObject target);

Attaches the player to the target game object, keeping the avatar fixed in its current position/rotation relative to the target object (e.g. for use in Vehicles)

Space.Scene.PlayerAvatar.AttachTo(targetObject)


StartFly

void StartFly ();

Puts the avatar into Fly Mode.


 
--the below script teleports the player to a sky location after he clicks
--but then immediately puts the character in fly mode so they don't fall back down
--(Example: Sky exhibit)

thisGameObject = Space.Host.ExecutingObject

OnClick = function()
Space.Scene.PlayerAvatar.Teleport(Vector.New(0,10,0))
Space.Scene.PlayerAvatar.StartFly()
end

thisGameObject.AddClickable()
thisGameObject.Clickable.Tooltip = "Click me to view our sky exhibit!"
thisGameObject.Clickable.OnClick(OnClick) 

EndFly

void EndFly ();

Takes the avatar out of Fly Mode.

Space.Scene.PlayerAvatar.EndFly()


--the below script a player flying in the sky back to the ground 
--but then immediately end's the character fly mode so they are standing
--(Example: Going back down to ground from Sky exhibit)

thisGameObject = Space.Host.ExecutingObject

OnClick = function()
Space.Scene.PlayerAvatar.Teleport(Vector.New(0,0,0))
Space.Scene.PlayerAvatar.EndFly()
end

thisGameObject.AddClickable()
thisGameObject.Clickable.Tooltip = "Click me to go back to the ground!"
thisGameObject.Clickable.OnClick(OnClick) 

FindBone

SGameObject FindBone (string bone);

Retrieves the specified bone.

-- Best used with non humanoid models

local bone = Space.Scene.PlayerAvatar.FindBone("Hips/Spine/Chest/LeftShoulder"); -- Gets the bone by path
Space.Log(bone.Name); -- Prints the retrieved bones name, which will match the last name in the path specified.

or

-- Best used with humanoid models
local bone = Space.Scene.PlayerAvatar.FindBone("LeftShoulder"); -- Gets bone by UnityEngine.HumanTrait.BoneName[]
Space.Log(bone.Name); -- Prints the bone name of the corresponding search parameter, which when using a default avatar, it will print "LeftShoulder"

--[[
UnityEngine.HumanTrait.BoneName[]

Body: Hips, Spine, Chest, UpperChest
Head: Neck, Jaw, Head, LeftEye, RightEye

Left Arm: LeftShoulder, LeftUpperArm, LeftLowerArm, LeftHand
Left Leg: LeftUpperLeg, LeftLowerLeg, LeftFoot, LeftToes

Right Arm: RightShoulder, RightUpperArm, RightLowerArm, RightHand
Right Leg: RightUpperLeg, RightLowerLeg, RightFoot, RightToes

Left Hand (Thumb): LeftThumbProximal, LeftThumbIntermediate, LeftThumbDistal
Left Hand (Index): LeftIndexProximal, LeftIndexIntermediate, LeftIndexDistal
Left Hand (Middle): LeftMiddleProximal, LeftMiddleIntermediate, LeftMiddleDistal
Left Hand (Ring): LeftRingProximal, LeftRingIntermediate, LeftRingDistal
Left Hand (Little): LeftLittleProximal, LeftLittleIntermediate, LeftLittleDistal

Right Hand (Thumb): RightThumbProximal, RightThumbIntermediate, RightThumbDistal
Right Hand (Index): RightIndexProximal, RightIndexIntermediate, RightIndexDistal
Right Hand (Middle): RightMiddleProximal, RightMiddleIntermediate, RightMiddleDistal
Right Hand (Ring): RightRingProximal, RightRingIntermediate, RightRingDistal
Right Hand (Little): RightLittleProximal, RightLittleIntermediate, RightLittleDistal

]]


 --this script will set your avatar's "right hand" as parent of this object
--this way this object will be a child of your right hand and therefore move with it
--(Example: clicking on a gun to equip it)


thisGameObject = Space.Host.ExecutingObject

OnClick = function()

RightHand = Space.Scene.PlayerAvatar.FindBone("RightHand")
thisGameObject.SetParent(RightHand)
thisGameObject.LocalPosition = Vector.New(0,0,0) -- resetting position to match hand

end

thisGameObject.AddClickable()
thisGameObject.Clickable.Tooltip="Click to Equip "
thisGameObject.Clickable.OnClick(OnClick) 


ResetOutfit

void ResetOutfit ();

Reset current outfit.

local avatar=Space.Scene.PlayerAvatar
avatar.ResetOutfit()


Register

void Register (string username, string password, string email);

Allow a guest to register.

local avatar=Space.Scene.PlayerAvatar
avatar.Register("MyName","MyPassword","MyEmail",false,function ()
    Space.Log("Success!")
end,function ()
    Space.Log("OnError!!")
end)


LoadOutfit

void LoadOutfit (int outfitID);

Only works on a white-label deployment due to abuse concerns. Restrictions: The outfit ID needs to be one of the grid template outfits, with all the restrictions that apply to those (i.e. must use either SW content, or content uploaded by a grid admin account).

local avatar=Space.Scene.PlayerAvatar
avatar.LoadOutfit(10001)


 --the below script changes the player's outfit into 
--a pre-defined outfit if they click the object containing the script
--(Example: Player clicks on a wardrobe and their outfit changes)
--[Only works on white-label deployment]

thisGameObject = Space.Host.ExecutingObject

OnClick = function()
Space.Scene.PlayerAvatar.LoadOutfit(2662469)
end

thisGameObject.AddClickable()
thisGameObject.Clickable.Tooltip = "Put on your favourite suit"
thisGameObject.Clickable.OnClick(OnClick)

ResetOutfitToTemplate

void ResetOutfitToTemplate (int outfitID);

Reset current outfit to template one.

local avatar=Space.Scene.PlayerAvatar
avatar.ResetOutfitToTemplate(10001)


Teleport

void Teleport (string landmarkName);

Teleport the avatar to the landmark.

local avatar=Space.Scene.PlayerAvatar
avatar.Teleport("MyLandMark")


Teleport

void Teleport (SVector position);

Teleport the avatar to certain position.

local avatar=Space.Scene.PlayerAvatar
avatar.Teleport(Vector.Zero)


--the below script teleports the player to a sky location after he clicks
--but then immediately puts the character in fly mode so they don't fall back down
--(Example: Sky exhibit)

thisGameObject = Space.Host.ExecutingObject

OnClick = function()
Space.Scene.PlayerAvatar.Teleport(Vector.New(0,10,0))
Space.Scene.PlayerAvatar.StartFly()
end

thisGameObject.AddClickable()
thisGameObject.Clickable.Tooltip = "Click me to view our sky exhibit!"
thisGameObject.Clickable.OnClick(OnClick) 

Teleport

void Teleport (SVector position, SQuaternion rotation);

Teleport the avatar to certain position and rotation.

local avatar=Space.Scene.PlayerAvatar
avatar.Teleport(Vector.Zero,Quaternion.Identity)


Teleport

void Teleport (int region, SVector position, SQuaternion rotation);

Teleport the avatar to a certain region with certain position and rotation.

local avatar=Space.Scene.PlayerAvatar
avatar.Teleport(2342341,Vector.Zero,Quaternion.Identity)


Teleport

void Teleport (int region);

Teleport the avatar to a certain region.

local avatar=Space.Scene.PlayerAvatar
avatar.Teleport(2342341)


--the below script teleports player to another region
--if they click the object containing it
--(Example: A region promotional/advertisement sign)

thisGameObject = Space.Host.ExecutingObject

OnClick = function()
Space.Scene.PlayerAvatar.Teleport(151931)
end

thisGameObject.AddClickable()
thisGameObject.Clickable.Tooltip = "Click me to go to our region!"
thisGameObject.Clickable.OnClick(OnClick)

SetIKGoal

void SetIKGoal (string goal, bool positionGoal, float positionStrength, SVector positionTarget, bool rotationGoal, float rotationStrength, SQuaternion rotationTarget );

Set the IK goal to avatar.

local avatar=Space.Scene.PlayerAvatar
local pos=avatar.FindBone("Chest").WorldPosition
avatar.SetIKGoal("Chest",false,0,pos,true,20,Quaternion.Euler(0,60,0))


ClearIKGoal

void ClearIKGoal (string goal);

Clear the IK Goal.

local avatar=Space.Scene.PlayerAvatar
local pos=avatar.FindBone("Chest").WorldPosition
avatar.SetIKGoal("Chest",false,0,pos,true,20,Quaternion.Euler(0,60,0))
Space.Host.InvokeDelayed(function ()
    avatar.ClearIKGoal("Chest")
end,5)
'' --will clear after 5 seconds.''


PlayCustomAnimation

void PlayCustomAnimation (SResource animationClip);

Make the avatar plays custom animation.

local avatar=Space.Scene.PlayerAvatar
avatar.PlayCustomAnimation(Space.Resources[1])
'' --the avatar will play the custom animation if the animation clip was set correctly.''


StopCustomAnimation

void StopCustomAnimation ();

Make the avatar stops playing custom animation.

local avatar=Space.Scene.PlayerAvatar
avatar.PlayCustomAnimation(Space.Resources[1])
avatar.StopCustomAnimation()


LockObject

SGameObject LockObject ();

Lock the avatar.

local avatar=Space.Scene.PlayerAvatar
local obj=avatar.LockObject.Name
if obj~=nil then
    Space.Log(obj.Name)
end


OnAvatarReload

void OnAvatarReload (Closure o);

This event will be called when the avatar gets loaded.

local avatar=Space.Scene.PlayerAvatar
avatar.OnAvatarReload(function ()
    Space.Log(avatar.OutfitID)
end)


SynchroniseState

void SynchroniseState ();

Synchronise avatar's state to network.

local avatar=Space.Scene.PlayerAvatar
avatar.SynchroniseState()



OnAvatarSkeletonReload

bool OnAvatarSkeletonReload (Closure o);

This event will be called when the avatar skeleton gets loaded.

local avatar=Space.Scene.PlayerAvatar
Space.Log(avatar.BlockJump)
avatar.OnAvatarSkeletonReload(function ()
    Space.Log(avatar.OutfitID)
end)


Properties

Username

string Username { get; }

Returns the avatar's current username (note: this can be changed by players for a small fee)

playerUsername = Space.Scene.PlayerAvatar.Username


 --this script, once clicked, will get all avatars in the scene
--and print their Username(including self)
--(Example: scoreboards/radars/scanners/game machines)

thisGameObject = Space.Host.ExecutingObject

OnClick = function()
allAvatars = Space.Scene.AllAvatars

        for var=1, #allAvatars do
    local Username = allAvatars[var].Username
     Space.Log("Username: " .. Username)
        end

end

 
thisGameObject.AddClickable() 
thisGameObject.Clickable.Tooltip = "Click to print all avatars' Username"
thisGameObject.Clickable.OnClick(OnClick) 

Title

string Title { get; }

Returns the avatar's current title

playerTitle = Space.Scene.PlayerAvatar.Title


ID

long ID { get; }

Returns the avatar's user ID, please note if you store these, this is a 'long' value not a 'int'.

playerID = Space.Scene.PlayerAvatar.ID


--this script, once clicked, will get all avatars in the scene
--and print their ID(including self)
--(Example: scoreboards/radars/scanners/game machines)

thisGameObject = Space.Host.ExecutingObject

OnClick = function()
allAvatars = Space.Scene.AllAvatars

        for var=1, #allAvatars do
    local ID = allAvatars[var].ID
     Space.Log("ID: " .. ID)
        end

end

 
thisGameObject.AddClickable() 
thisGameObject.Clickable.Tooltip = "Click to print all avatars' ID"
thisGameObject.Clickable.OnClick(OnClick) 

GameObject

SGameObject GameObject { get; }

Returns a reference to avatar's GameObject

playerObject = Space.Scene.PlayerAvatar.GameObject


BlockMovement

bool BlockMovement { get;set; }

If set to True, avatar's movement will be blocked

Space.Scene.PlayerAvatar.BlockMovement = true


 --the below scipt makes this object a boundary area where any user entering it
--will be blocked from movement for 10 seconds, then the object will release controls and
--destry itself after that
--(Example: freezing trap)
--[This object's collider needs to be set as IsTrigger (Please read about OnTriggerStart for troubleshooting)]

thisGameObject = Space.Host.ExecutingObject
FrozenTime = 0.0

OnTriggerStart = function(TriggerStarter)
  if TriggerStarter.Root.Avatar ~= nil then
    if TriggerStarter.Root.Avatar.Username == Space.Scene.PlayerAvatar.Username then
      Space.Scene.PlayerAvatar.BlockMovement = true
    end
  end
end

OnTriggerStay = function(TriggerStayer)
  if TriggerStayer.Root.Avatar ~= nil then
    if TriggerStayer.Root.Avatar.Username == Space.Scene.PlayerAvatar.Username then
      FrozenTime = FrozenTime + Space.DeltaTime
        if FrozenTime > 20 then
        Space.Scene.PlayerAvatar.BlockMovement = false
        thisGameObject.Destroy()
        end
    end
  end
end

thisGameObject.SubscribeToEvents()
thisGameObject.OnTriggerStart(OnTriggerStart)
thisGameObject.OnTriggerStay(OnTriggerStay)
 

Guest

bool Guest { get; }

Returns True if avatar is guest

Space.Log(Space.Scene.PlayerAvatar.Guest)


Skeleton

SGameObject Skeleton { get; }

Returns a reference to avatar's skeleton GameObject

avSkeleton = Space.Scene.PlayerAvatar.Skeleton


IsAttached

bool IsAttached { get; }

Returns True if avatar is Attached

Space.Log(Space.Scene.PlayerAvatar.isAttached)


IsGrounded

bool IsGrounded { get; }

Returns True if avatar is Grounded

Space.Log(Space.Scene.PlayerAvatar.isGrounded)


JumpHeight

float JumpHeight { get;set; }

How high the avatar's jump is

Space.Scene.PlayerAvatar.JumpHeight = 20


Loaded

bool Loaded { get; }

Returns true if avatar is Loaded

Space.Log(Space.Scene.PlayerAvatar.Loaded)


MovementSpeed

float MovementSpeed { get;set; }

How fast the avatar's movement is

Space.Scene.PlayerAvatar = 20


 --the below scipt makes the object give the player triple movement speed
--once it's clicked, and then returns them to original speed after 10 seconds
--(Example: Triple Speed Powerup (vatar running race))

thisGameObject = Space.Host.ExecutingObject

TripleSpeedCoroutine = function()
local originalSpeed = Space.Scene.PlayerAvatar.MovementSpeed
Space.Scene.PlayerAvatar.MovementSpeed = originalSpeed * 3
coroutine.yield(10)
Space.Scene.PlayerAvatar.MovementSpeed = originalSpeed
  
end


OnClick = function()
Space.Host.StartCoroutine(TripleSpeedCoroutine)
end

thisGameObject.AddClickable()
thisGameObject.Clickable.Tooltip = "Triple Speed Powerup!"
thisGameObject.Clickable.OnClick(OnClick)  

OutfitID

long OutfitID { get; }

Returns the current outfit ID of the avatar

avOutfit = Space.Scene.PlayerAvatar.OutfitID


BlockRun

bool BlockRun { get;set; }

Returns true if the avatar could run.

local avatar=Space.Scene.PlayerAvatar
Space.Log(avatar.BlockRun)
avatar.BlockRun=true
'' --Now the avatar cannot run anymore.''


BlockFly

bool BlockFly { get;set; }

Return true if the avatar could fly.

local avatar=Space.Scene.PlayerAvatar
Space.Log(avatar.BlockFly)
avatar.BlockFly=true
'' --Now the avatar cannot fly anymore.''


 --the below scipt makes this object a boundary area where flying gets blocked
--while player is in it and flying is allowed once player leaves it
--(Example: No fly zone)
--[This object's collider needs to be set as IsTrigger (Please read about OnTriggerStart for troubleshooting)]

thisGameObject = Space.Host.ExecutingObject

OnTriggerStart = function(TriggerStarter)
  if TriggerStarter.Root.Avatar ~= nil then
    if TriggerStarter.Root.Avatar.Username == Space.Scene.PlayerAvatar.Username then
      Space.Scene.PlayerAvatar.BlockFly = true
    end
  end
end


OnTriggerExit = function(TriggerExiter)
  if TriggerExiter.Root.Avatar ~= nil then
    if TriggerExiter.Root.Avatar.Username == Space.Scene.PlayerAvatar.Username then
      Space.Scene.PlayerAvatar.BlockFly = false
    end
  end
end

thisGameObject.SubscribeToEvents()
thisGameObject.OnTriggerStart(OnTriggerStart)
thisGameObject.OnTriggerExit(OnTriggerExit) 

BlockCrouch

bool BlockCrouch { get;set; }

Return true if the avatar could crouch.

local avatar=Space.Scene.PlayerAvatar
Space.Log(avatar.BlockCrouch)
avatar.BlockCrouch=true
'' --Now the avatar cannot crouch anymore.''


BlockJump

bool BlockJump { get;set; }

Return true if the avatar could jump.

local avatar=Space.Scene.PlayerAvatar
Space.Log(avatar.BlockJump)
avatar.BlockJump=true
'' --Now the avatar cannot jump anymore.''