Authoring a Logical Entity/Code: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
 
(source=cpp)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Category:Programming]]
<source lang="cpp">
<pre>
//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ========
//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ========
//
//
Line 12: Line 11:
class CMyLogicalEntity : public CLogicalEntity
class CMyLogicalEntity : public CLogicalEntity
{
{
public:
public:
DECLARE_CLASS( CMyLogicalEntity , CLogicalEntity );
DECLARE_CLASS( CMyLogicalEntity, CLogicalEntity );
DECLARE_DATADESC();
DECLARE_DATADESC();


// Constructor
// Constructor
CMyLogicalEntity ( void ) : m_nCounter( 0 ) {}
CMyLogicalEntity ()
{
m_nCounter = 0;
}


// Input function
// Input function
void InputTick( inputdata_t &inputData );
void InputTick( inputdata_t &inputData );


private:
private:


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


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


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


// Links our member variable to our keyvalue from Hammer
// For save/load
DEFINE_KEYFIELD( m_nThreshold, FIELD_INTEGER, "threshold" ),
DEFINE_FIELD( m_nCounter, FIELD_INTEGER ),


// Links our input name from Hammer to our input member function
// Links our member variable to our keyvalue from Hammer
DEFINE_INPUTFUNC( FIELD_VOID, "Tick", InputTick ),
DEFINE_KEYFIELD( m_nThreshold, FIELD_INTEGER, "threshold" ),


// Links our output member to the output name used by Hammer
// Links our input name from Hammer to our input member function
DEFINE_OUTPUT( m_OnThreshold, "OnThreshold" ),
DEFINE_INPUTFUNC( FIELD_VOID, "Tick", InputTick ),
 
// Links our output member to the output name used by Hammer
DEFINE_OUTPUT( m_OnThreshold, "OnThreshold" ),


END_DATADESC()
END_DATADESC()
Line 52: Line 54:
// Purpose: Handle a tick input from another entity
// Purpose: Handle a tick input from another entity
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CMyLogicalEntity ::InputTick( inputdata_t &inputData )
void CMyLogicalEntity::InputTick( inputdata_t &inputData )
{
{
// Increment our counter
// Increment our counter
Line 67: Line 69:
}
}
}
}
</pre>
</source>
 
== See also ==
 
*[[Authoring a Logical Entity]]
 
[[Category:Programming]]

Latest revision as of 05:09, 17 January 2009

//===== 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 ()
	{
		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;
	}
}

See also