ARAvatarTrigger

From Blue Mars Developer Guidebook

Jump to: navigation, search
There are security restrictions on this article

Contents

Overview

The ARAvatarTrigger responds when an avatar enters or leaves the bounding box of the trigger, or a Shape area linked to the trigger. Example usage is shown here.

Image:ARAvatarTrigger_entity.png

In the CityEditor, drag the ARAvatarTrigger entity into the level. Use the FlowGraph setup to trigger events like Enter and Leave.


Properties

Image:ARAvatarTrigger_props.png

DimX,Y,Z
X, Y, Z dimensions
OnlyLocalAvatar
flag determines whether the trigger responds only to the local avatar (a user's avatar)
TriggerOnce
flag determines if the trigger should be limited to one enter event (requires OnlyLocalAvatar to be set to true).
StopOnEnter
flag determines whether the avatar should stop walking and start an idle animation upon entering the trigger
ScriptCommand
executed upon entering the trigger
ScriptCommandLeave
executed upon leaving the trigger


FlowGraph setup in CityEditor

Image:ARAvatarTrigger_fg.png

FlowGraph Input

Disable
sends to Inactive state, where Enter event will have no effect
Enable
sends to Empty (enabled) state
Enter
triggers Enter event
Leave
triggers Leave event

FlowGraph Output

Enter
triggered when avatar enters bounding box area (or by Enter input)
Leave
triggered when avatar leaves bounding box area (or by Leave input)
InactiveUponEnter
triggered when avatar enters bounding box area (or by Enter input), and the entity state is Inactive
Disable, Enable
not used


Script

ARAvatarTrigger = {
  type = "Trigger",

  Properties = {
    DimX = 5,
    DimY = 5,
    DimZ = 5,
    bEnabled=1,
    bOnlyLocalAvatar = 1,
    bOnlyAvatar = 1,
    bStopOnEnter=0,
    bTriggerOnce = 0,
    ScriptCommand = "",
    ScriptCommandLeave = "",
  },
  States = {"Empty", "Inactive"},

  Editor={
    Model="Editor/Objects/T.cgf",
    Icon="Trigger.bmp",
    ShowBounds = 1,
  },
  
  trigger = true,
  bTriggered = false,
}

function ARAvatarTrigger:OnPropertyChange()
  self:OnReset();
end

function ARAvatarTrigger:OnInit()
  self:SetUpdatePolicy( ENTITY_UPDATE_PHYSICS );
  self:OnReset();
end

function ARAvatarTrigger:OnReset()
  self:KillTimer(0);
  self.Who = nil;
  self.Entered = 0;
  self.bLocked = 0;
  self.bTriggered = 0;
  self.EnterCount =0;
  self.UpdateCounter = 0;
  self.bUseOrderEnabled = true;
  
  local Min = { x=-self.Properties.DimX/2, y=-self.Properties.DimY/2, z=-self.Properties.DimZ/2 };
  local Max = { x=self.Properties.DimX/2, y=self.Properties.DimY/2, z=self.Properties.DimZ/2 };
  self:SetTriggerBBox( Min, Max );
  
  if (self.Properties.bEnabled==1) then
    self:GotoState( "Empty" );
  else
    self:GotoState( "Inactive" );
  end

  System.LogAlways ("ARAvatarTrigger:OnReset")
end

function ARAvatarTrigger:IsValidInput (sender)
  local prop = self.Properties
  if sender == nil then
    return false
  elseif prop.bOnlyLocalAvatar and prop.bOnlyLocalAvatar ~= 0 then
    return ARVirtualWorld.IsLocalAvatar (sender)
  elseif prop.bOnlyAvatar and prop.bOnlyAvatar ~= 0 then
    return sender.class == "Avatar"
  else
    return true
  end
end

function ARAvatarTrigger:Event_Enter( sender )
  if (self:GetState( ) == "Inactive") then
    self:ActivateOutput("InactiveUponEnter", true);
    return 
  end 
  
  if self:IsValidInput (sender) then
    if (self.Properties.bTriggerOnce == 1 and self.bTriggered == 1 and self.Properties.bOnlyLocalAvatar == 1) then
      return
    end
    self.bTriggered = 1;
    if (self.Properties.bStopOnEnter==1) then
      sender.ARAvatarAction:StartIdle();
    end
    
    if (self.Properties.ScriptCommand and self.Properties.ScriptCommand~="") then
      local f = loadstring(self.Properties.ScriptCommand);
      if (f~=nil) then
        f();
      end
    end
    self:ActivateOutput("Enter", sender.id);
  end
end

function ARAvatarTrigger:Event_Leave( sender )
  if self:IsValidInput (sender) then
    System.LogAlways ("ARAvatarTrigger:Event_Leave: sender = " .. tostring(sender.id))
    if (self.Properties.ScriptCommandLeave and self.Properties.ScriptCommandLeave~="") then
      local f = loadstring(self.Properties.ScriptCommandLeave);
      if (f~=nil) then
        f();
      end
    end
    self:ActivateOutput("Leave", sender.id);
    if (self.Properties.bTriggerOnce == 1 and self.Properties.bOnlyLocalAvatar == 1) then
      self:GotoState("Inactive");
    end
  end
end

function ARAvatarTrigger:Event_Enable( sender )
   bEnabled = true;   
   self:GotoState("Empty");
end

function ARAvatarTrigger:Event_Disable( sender )
   bEnabled = false;
   
   self:GotoState( "Inactive" );
end

function ARAvatarTrigger:Log( msg )
  System.Log( msg );
end

-- Check if source entity is valid for triggering.
function ARAvatarTrigger:IsValidSource( entity )
  return true;
end



-------------------------------------------------------------------------------
-- Inactive State -------------------------------------------------------------
-------------------------------------------------------------------------------
ARAvatarTrigger.Inactive =
{
  OnBeginState = function( self )
    System.Log("ARAvatarTrigger.Inactive");
  end,
}

-------------------------------------------------------------------------------
-- Empty State ----------------------------------------------------------------
-------------------------------------------------------------------------------
ARAvatarTrigger.Empty =
{
  -------------------------------------------------------------------------------
  OnBeginState = function( self )
    System.LogAlways ("Empty!")
    self.Who = nil;
    self.UpdateCounter = 0;
    self.Entered = 0;
    if (self.Properties.aianchorAIAction~="") then
    end
  end,

  -------------------------------------------------------------------------------
  OnEnterArea = function( self,entity,areaId )
    self:Event_Enter(entity);
  end,

  ---------------------------------------------
  OnLeaveArea = function( self,entity,areaId )
    self:Event_Leave( entity );
  end,
}

ARAvatarTrigger.FlowEvents =
{
  Inputs =
  {
    Disable = { ARAvatarTrigger.Event_Disable, "bool" },
    Enable = { ARAvatarTrigger.Event_Enable, "bool" },
    Enter = { ARAvatarTrigger.Event_Enter, "entity" },
    Leave = { ARAvatarTrigger.Event_Leave, "entity" },
  },
  Outputs =
  {
    Disable = "bool",
    Enable = "bool",
    Enter = "entity",
    InactiveUponEnter = "bool",
    Leave = "entity",
  },
}
Problems with this wiki page? Contact us either by: Support Email or Support Ticket System

Blue Mars Guidebook Privacy Policy
Blue Mars Guidebook Community Guidelines

Personal tools