Authoring a Logical Entity/Code: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
 
(colours)
Line 1: Line 1:
[[Category:Programming]]
[[Category:Programming]]
<pre>
<span style="color:green;">//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ========
//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ========
//
//
// Purpose: Simple logical entity that counts up to a threshold value, then
// Purpose: Simple logical entity that counts up to a threshold value, then
// fires an output when reached.
// fires an output when reached.
//
//
//=============================================================================</span>
//=============================================================================
 
<span style="color:blue;">#include</span> <span style="color:brown;">"cbase.h"</span>
#include "cbase.h"
 
<span style="color:blue;">class</span> CMyLogicalEntity : <span style="color:blue;">public</span> CLogicalEntity
class CMyLogicalEntity : public CLogicalEntity
{
{
<span style="color:blue;">public</span>:
public:
DECLARE_CLASS( CMyLogicalEntity, CLogicalEntity );
DECLARE_CLASS( CMyLogicalEntity , CLogicalEntity );
DECLARE_DATADESC();
DECLARE_DATADESC();
 
<span style="color:green;">// Constructor</span>
// Constructor
CMyLogicalEntity ( <span style="color:blue;">void</span> ) : m_nCounter( 0 ) {}
CMyLogicalEntity ( void ) : m_nCounter( 0 ) {}
 
 
<span style="color:green;">// Input function</span>
// Input function
<span style="color:blue;">void</span> InputTick( inputdata_t &inputData );
void InputTick( inputdata_t &inputData );
 
<span style="color:blue;">private</span>:
private:
 
<span style="color:blue;">int</span> m_nThreshold; <span style="color:green;">// Count at which to fire our output</span>
int m_nThreshold; // Count at which to fire our output
<span style="color:blue;">int</span> m_nCounter; <span style="color:green;">// Internal counter</span>
int m_nCounter; // Internal counter
 
COutputEvent m_OnThreshold; <span style="color:green;">// Output event when the counter reaches the threshold</span>
COutputEvent m_OnThreshold; // Output even when the counter reaches the threshold
};
};
 
LINK_ENTITY_TO_CLASS( my_logical_entity, CMyLogicalEntity  );
LINK_ENTITY_TO_CLASS( my_logical_entity, CMyLogicalEntity  );
 
<span style="color:green;">// Start of our data description for the class</span>
// Start of our data description for the class
BEGIN_DATADESC( CMyLogicalEntity  )
BEGIN_DATADESC( CMyLogicalEntity  )
<span style="color:green;">// For save/load</span>
// For save/load
DEFINE_FIELD( m_nCounter, FIELD_INTEGER ),
DEFINE_FIELD( m_nCounter, FIELD_INTEGER ),
 
<span style="color:green;">// Links our member variable to our keyvalue from Hammer</span>
// Links our member variable to our keyvalue from Hammer
DEFINE_KEYFIELD( m_nThreshold, FIELD_INTEGER, <span style="color:brown;">"threshold"</span> ),
DEFINE_KEYFIELD( m_nThreshold, FIELD_INTEGER, "threshold" ),
 
<span style="color:green;">// Links our input name from Hammer to our input member function</span>
// Links our input name from Hammer to our input member function
DEFINE_INPUTFUNC( FIELD_VOID, <span style="color:brown;">"Tick"</span>, InputTick ),
DEFINE_INPUTFUNC( FIELD_VOID, "Tick", InputTick ),
 
<span style="color:green;">// Links our output member to the output name used by Hammer</span>
// Links our output member to the output name used by Hammer
DEFINE_OUTPUT( m_OnThreshold, <span style="color:brown;">"OnThreshold"</span> ),
DEFINE_OUTPUT( m_OnThreshold, "OnThreshold" ),
 
END_DATADESC()
END_DATADESC()
 
<span style="color:green;">//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: Handle a tick input from another entity
// Purpose: Handle a tick input from another entity
//-----------------------------------------------------------------------------</span>
//-----------------------------------------------------------------------------
<span style="color:blue;">void</span> CMyLogicalEntity::InputTick( inputdata_t &inputData )
void CMyLogicalEntity ::InputTick( inputdata_t &inputData )
{
{
<span style="color:green;">// Increment our counter</span>
// Increment our counter
m_nCounter++;
m_nCounter++;
 
<span style="color:green;">// See if we've met or crossed our threshold value</span>
// See if we've met or crossed our threshold value
<span style="color:blue;">if</span> ( m_nCounter >= m_nThreshold )
if ( m_nCounter >= m_nThreshold )
{
{
<span style="color:green;">// Fire an output event</span>
// Fire an output event
m_OnThreshold.FireOutput( inputData.pActivator, <span style="color:blue;">this</span> );
m_OnThreshold.FireOutput( inputData.pActivator, this );
<span style="color:green;">// Reset our counter</span>
// Reset our counter
m_nCounter = 0;
m_nCounter = 0;
}
}
}
}
</pre>

Revision as of 03:35, 24 February 2008

//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ========
//
// Purpose: Simple logical entity that counts up to a threshold value, then
//			fires an output when reached.
//
//=============================================================================

#include "cbase.h"

class CMyLogicalEntity : public CLogicalEntity
{
public:
	DECLARE_CLASS( CMyLogicalEntity, CLogicalEntity );
	DECLARE_DATADESC();

	// Constructor
	CMyLogicalEntity ( void ) : m_nCounter( 0 ) {}
 
	// Input function
	void InputTick( inputdata_t &inputData );

private:

	int	m_nThreshold;	// Count at which to fire our output
	int	m_nCounter;	// Internal counter

	COutputEvent	m_OnThreshold;	// Output event when the counter reaches the threshold
};

LINK_ENTITY_TO_CLASS( my_logical_entity, CMyLogicalEntity  );

// Start of our data description for the class
BEGIN_DATADESC( CMyLogicalEntity  )
	
	// For save/load
	DEFINE_FIELD( m_nCounter, FIELD_INTEGER ),

	// Links our member variable to our keyvalue from Hammer
	DEFINE_KEYFIELD( m_nThreshold, FIELD_INTEGER, "threshold" ),

	// Links our input name from Hammer to our input member function
	DEFINE_INPUTFUNC( FIELD_VOID, "Tick", InputTick ),

	// Links our output member to the output name used by Hammer
	DEFINE_OUTPUT( m_OnThreshold, "OnThreshold" ),

END_DATADESC()

//-----------------------------------------------------------------------------
// Purpose: Handle a tick input from another entity
//-----------------------------------------------------------------------------
void CMyLogicalEntity::InputTick( inputdata_t &inputData )
{
	// Increment our counter
	m_nCounter++;

	// See if we've met or crossed our threshold value
	if ( m_nCounter >= m_nThreshold )
	{
		// Fire an output event
		m_OnThreshold.FireOutput( inputData.pActivator, this );
		
		// Reset our counter
		m_nCounter = 0;
	}
}