I don't need to test my programs. I have an error-correcting modem.

User:Wormbo/ClientMaterialTrigger

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search
UE2 Object >> Actor >> Triggers >> MaterialTrigger >> ClientMaterialTrigger (custom)

The ClientMaterialTrigger class triggers Materials just like a MaterialTrigger. The important difference is: The ClientMaterialTrigger also works on network clients. (See Replication.)

Note: If you're working with Jailbreak 2004 you should use the identical JBMaterialTrigger class from the JBToolbox package instead.

Properties

Property group 'MaterialTrigger'

MaterialsToTrigger

Type: array<Material>

Inherited from MaterialTrigger. A list of materials that should be triggered or reset.

TriggerAction

Type: ETriggerAction

What to do with the materials when the ClientMaterialTrigger is triggered.

UntriggerAction

Type: EUntriggerAction

What to do with the materials when the ClientMaterialTrigger is untriggered.

Enums

ETriggerAction

TriggerTriggers 
Triggering the ClientMaterialTrigger triggers the materials. (default)
TriggerResets 
Triggering the ClientMaterialTrigger resets the materials.
TriggerDoesNothing 
Triggering the ClientMaterialTrigger has no effect.

EUntriggerAction

UntriggerDoesNothing 
Untriggering the ClientMaterialTrigger has no effect. (default)
UntriggerTriggers 
Untriggering the ClientMaterialTrigger triggers the materials.
UntriggerResets 
Untriggering the ClientMaterialTrigger resets the materials.

Code

This custom class actually consists of two classes, ClientMaterialTrigger and MaterialTriggerReplicationInfo. Both are required for compiling.

ClientMaterialTrigger

Create a subclass of MaterialTrigger in the package MyLevel and name it ClientMaterialTrigger. Paste the following code replacing the lines UnrealEd automatically added to the class:

//=============================================================================
// ClientMaterialTrigger
// Copyright 2003 by Wormbo
//
// A MaterialTrigger that works in network games.
//=============================================================================
 
 
class ClientMaterialTrigger extends MaterialTrigger;
 
 
//=============================================================================
// Variables
//=============================================================================
 
// whether triggering resets or triggers the materials
var(MaterialTrigger) enum ETriggerAction {
  TriggerTriggers,
  TriggerResets,
  TriggerDoesNothing
} TriggerAction;
 
// whether untriggering resets or triggers the materials
var(MaterialTrigger) enum EUnriggerAction {
  UntriggerDoesNothing,
  UntriggerTriggers,
  UntriggerResets
} UntriggerAction;
 
// array holding the ReplicationInfos for clientside triggering
var array<MaterialTriggerReplicationInfo> ReplicatedMaterialTriggers;
 
 
//=============================================================================
// PostBeginPlay
//
// Spawns a MaterialTriggerReplicationInfo for each triggered material.
//=============================================================================
 
function PostBeginPlay()
{
  local int i;
 
  ReplicatedMaterialTriggers.Length = MaterialsToTrigger.Length;
 
  for (i = 0; i < MaterialsToTrigger.Length; i++) {
    ReplicatedMaterialTriggers[i] = Spawn(class'MaterialTriggerReplicationInfo', Self);
    ReplicatedMaterialTriggers[i].SetMaterialToTrigger(string(MaterialsToTrigger[i]));
  }
}
 
 
//=============================================================================
// Trigger
//
// Tells the MTRIs about the Instigators and triggering actors and tells them
// to trigger the material.
//=============================================================================
 
function Trigger(Actor Other, Pawn EventInstigator)
{
  local int i;
 
  if ( Other == None )
    Other = Self;
 
  if ( TriggerAction == TriggerTriggers ) {
    for (i = 0; i < ReplicatedMaterialTriggers.Length; i++)
      if ( ReplicatedMaterialTriggers[i] != None )
        ReplicatedMaterialTriggers[i].TriggerMaterial(Other, EventInstigator);
  }
  else if ( TriggerAction == TriggerResets ) {
    for (i = 0; i < ReplicatedMaterialTriggers.Length; i++)
      if ( ReplicatedMaterialTriggers[i] != None )
        ReplicatedMaterialTriggers[i].ResetMaterial();
  }
}
 
 
//=============================================================================
// Untrigger
//
// Triggers or resets the materials depending on the UntriggerAction property.
//=============================================================================
 
function Untrigger(Actor Other, Pawn EventInstigator)
{
  local int i;
 
  if ( Other == None )
    Other = Self;
 
  if ( UntriggerAction == UntriggerTriggers ) {
    for (i = 0; i < ReplicatedMaterialTriggers.Length; i++)
      if ( ReplicatedMaterialTriggers[i] != None )
        ReplicatedMaterialTriggers[i].TriggerMaterial(Other, EventInstigator);
  }
  else if ( UntriggerAction == UntriggerResets ) {
    for (i = 0; i < ReplicatedMaterialTriggers.Length; i++)
      if ( ReplicatedMaterialTriggers[i] != None )
        ReplicatedMaterialTriggers[i].ResetMaterial();
  }
}

MaterialTriggerReplicationInfo

UE2 Object >> Actor >> Info >> ReplicationInfo >> MaterialTriggerReplicationInfo (custom)

The MaterialTriggerReplicationInfo is a non-placeable helper class to replicate trigger events to network clients. It will be spawned by the ClientMaterialTrigger at runtime.

Create a subclass of ReplicationInfo in MyLevel and name it MaterialTriggerReplicationInfo. (You need to uncheck the "Placeable classes Only" checkbox to see ReplicationInfo in the Actor Classes Browser.) Paste the following code replacing the lines UnrealEd automatically added to the class:

//=============================================================================
// MaterialTriggerReplicationInfo
// Copyright 2003 by Wormbo
//
// Used by ClientMaterialTrigger to replicate material trigger events to
// network clients.
//=============================================================================
 
 
class MaterialTriggerReplicationInfo extends ReplicationInfo;
 
 
//=============================================================================
// Variables
//=============================================================================
 
var Material MaterialToTrigger;
var string MaterialName;
var int ClientTriggerCount;
var int ClientUnTriggerCount;
var int TriggerCount;
var struct TTriggerData {
  var Actor Triggerer;
  var Pawn EventInstigator;
  var byte TriggerAction; // 0 = nothing/idle, 1 = trigger, 2 = reset
} LastTriggerings[10];
 
 
//=============================================================================
// Replication
//=============================================================================
 
replication
{
  reliable if ( Role == ROLE_Authority )
    TriggerCount, LastTriggerings, MaterialName;
}
 
 
simulated function PostBeginPlay()
{
  SetTimer(0.1, True);
}
 
 
function SetMaterialToTrigger(string newMaterial)
{
  MaterialName = newMaterial;
}
 
 
function TriggerMaterial(Actor Other, Pawn EventInstigator)
{
  TriggerCount++;
  LastTriggerings[TriggerCount % 10].Triggerer = Other;
  LastTriggerings[TriggerCount % 10].EventInstigator = EventInstigator;
  LastTriggerings[TriggerCount % 10].TriggerAction = 1;
}
 
 
function ResetMaterial()
{
  TriggerCount++;
  LastTriggerings[TriggerCount % 10].TriggerAction = 2;
}
 
 
simulated function Timer()
{
  if ( MaterialName != "" && MaterialToTrigger == None ) {
    MaterialToTrigger = Material(DynamicLoadObject(MaterialName, class'Material'));
    if ( MaterialToTrigger != None )
      MaterialToTrigger.Reset();
  }
 
  if ( MaterialToTrigger != None && TriggerCount > ClientTriggerCount
      && LastTriggerings[(ClientTriggerCount + 1) % 10].TriggerAction != 0 ) {
    ClientTriggerCount++;
    if ( LastTriggerings[ClientTriggerCount % 10].TriggerAction == 2 )
      MaterialToTrigger.Reset();
    else
      MaterialToTrigger.Trigger(LastTriggerings[ClientTriggerCount % 10].Triggerer,
          LastTriggerings[ClientTriggerCount % 10].EventInstigator);
  }
}