Once I get that upgrade to 36-hour days, I will tackle that. – Mychaeel

Legacy:ReviveBarrel

From Unreal Wiki, The Unreal Engine Documentation Site
Revision as of 10:51, 18 November 2007 by Sweavo (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Extending[edit]

A respawning barrel.

Remaining problems:

  • no effect of any kind when the barrel reappears. You might want some sort of swishy wizz or something
  • you can be standing where the barrel really is when it reappears, and then you're sort of inside it

To use this script in your map: Create A Subclass.

//=============================================================================
// ReviveBarrel.
//=============================================================================
class ReviveBarrel extends Barrel;
 
var() float TimeToRespawn;
var int InitialHealth;
 
function PreBeginPlay()
{
  super.PreBeginPlay();
  InitialHealth = Health;
}
 
function Vanish(bool bVanish)
{
  bHidden = bVanish;
  bBlockActors = !bVanish;
  bBlockPlayers = !bVanish;
  bProjTarget = !bVanish;
}
 
simulated function Frag(class<fragment> FragType, vector Momentum, float DSize, int NumFrags) 
{
  local int i;
  local actor A, Toucher;
  local Fragment s;
 
  if ( bOnlyTriggerable )
    return; 
  if (Event!='')
    foreach AllActors( class 'Actor', A, Event )
      A.Trigger( Toucher, pawn(Toucher) );
  if ( Region.Zone.bDestructive )
  {
    Destroy();
    return;
  }
  for (i=0 ; i<NumFrags ; i++) 
  {
    s = Spawn( FragType, Owner);
    s.CalcVelocity(Momentum,0);
    s.DrawScale = DSize*0.5+0.7*DSize*FRand();
  }
 
  GotoState('Absent');
}
 
state Absent 
{
Begin:
  Health = InitialHealth; // restore health
  Vanish(true);
 
  Sleep( TimeToRespawn );
 
  Vanish(false);
 
  GotoState('Animate');
}

Wormbo: You should toggle bHidden instead of changing the DrawType.

Tarquin: Thanks :) Had to sort out blocking as well. Mych will not like me calling that function Vanish() but I can't think of a better name :(

Wormbo: What about "SetVisibility" or "SetHidden"?

Tarquin: Great! Thanks :)

Foxpaw: It might not hurt to have both, sort of. It's a little bit of overhead at the interpreter level, but not that big of a deal. In my GUI classes I use a SetHidden function, then a Show and Hide function that are just one-line functions that call SetHidden( false ) and SetHidden( true ) respectively. Effectively it's the same, but I guess it kind of helps the readability. If you're into that sort of thing. :D

Tarquin: I was thinking this could make a tutorial about subclassing and states, but it's not very neat what with inheriting one state from Barrel and a load of code from Decoration. So I guess for SetHidden it's a case of what's clearest for people to read. Your idea of Show() just being a call to setHidden is neat, but I don't know how easy to grasp it would be.

Dirk Fist: I call that a wrapper function, and it can greatly increase the readability of complex code. Although its impact here is minimal. If you put the definition of Hide() and Show() immediately above SetHidden() the relationship is clear to anyone that looks.

xX)(Xx: Doesnt appear to work for some reason

MythOpus: I do believe this script can be used only for Unreal 1 and MAYBE Unreal Tournament. Anything higher than those games wouldn't have the class 'Barrel'.

Bullet: And what about replication here? Don't we need to replicate the two new variables?

Wormbo: No, damage and respawning is handled on the serverside.

LionsPhil: Pedantically, unless UT's interpreter does automatic inlining of functions (and maybe it does – it's got to be fairly optimised, and possibly optimising), the Hide()/Show() wrappers will add an extra stack frame when calling and cause a tiny performance hit. This is probably not measurable, however.

Foxpaw: It doesn't automatically make functions inline - doing so would prevent polymorphism from working properly. An eye toward polymorphism is possibly why it was implemented that way, plus it looks a bit cleaner, I guess. Technically if you did put those functions inline you could replace the variable lookups with constant values and then eliminate the inversion operations too. However, in this instance we are looking at something a function that will get called at most once in a few seconds, and the runtime of it as-is is in the nanoseconds.

Personally I would probrably optimize it, but for this page, I think that readability to novice coders is more important than the speed gain.

AlexKotsch I made a respawning barrel and it doesn't have the problem of being inside the barrel

//-----------------------------------------------------------
//
//-----------------------------------------------------------
class DECO_Barrel extends DECO_Smashable;
 
#exec OBJ LOAD FILE=..\StaticMeshes\ONS-BPJW1.usx
 
//=============================================================================
// defaultproperties
//=============================================================================
 
var   float    Damage;
var	  float	   DamageRadius;
var   float	   MomentumTransfer;
var   class<DamageType>	   MyDamageType;
var int Ted;
var int MaxTed;
var float TedRate;
 
  function BreakApart(vector HitLocation, vector momentum)
{
	// If we are single player or on a listen server, just spawn the actor, otherwise
	// bHidden will trigger the effect
 
	if (Level.NetMode == NM_ListenServer || Level.NetMode == NM_StandAlone)
	{
		if ( (EffectWhenDestroyed!=None ) && EffectIsRelevant(location,false) )
			Spawn( EffectWhenDestroyed, Owner,, Location );
	}
 
	gotostate('Exploding');
}
 
state Exploding
{
	function BeginState()
	{
		super.BeginState();
		PlaySound(sound'WeaponSounds.BExplosion3',,2.5*TransientSoundVolume);
		Ted = 1;
		   SetTimer(TedRate, true);
                   Timer();
                }
 
                	}
 
 simulated function Timer()
	{
		Ted = Ted++;
        	if (Ted == MaxTed)
	        {
        		SetTimer(0.0, false);
                         	BlowUp(Location);
                }
                  }
 
function BlowUp(vector Location)
{
	HurtRadius(Damage, DamageRadius, MyDamageType, MomentumTransfer, Location );
		gotostate('Broken');
}
 
defaultproperties
{
     MaxTed=15
     TedRate=.025
     bImperviusToPlayer=False
     EffectWhenDestroyed=Class'UT2k4Assault.FX_ExplodingBarrel'
     Health=50
     DrawType=DT_StaticMesh
     StaticMesh=StaticMesh'AS_Decos.ExplodingBarrel'
     DamageRadius=90.000000
     Damage=90.000000
     MomentumTransfer=50000.000000
     MyDamageType=Class'XWeapons.DamTypeRocket'
     PrePivot=(Z=40.000000)
     AmbientGlow=48
     CollisionRadius=25.000000
     CollisionHeight=32.000000
}