Found out about GetLastThink(). Again - any mistakes?


     GetLastThink()

A utility function that returns the time of the entity’s last think as a
floating point value.

float dt = gpGlobals->curtime - GetLastThink();
SetAltitude( m_flAltitude + m_flBarnaclePullSpeed * dt );

This real-world code for npc_barnacle modulates the speed of tongue
movement, even if the frequency at which the code driving it is called
changes. |dt| is short for “delta time”.

For smooth animation this think code would need to be executed every
frame. This is exactly what happens, until the barnacle is no longer in
the PVS and the rate is slowed down – thus requiring the above modulation.

Tom Edwards wrote:
[ Converted text/html to text/plain ]

I'm in the process of writing some programming documentation for VDC (it's a
fantastic way to learn). Before I make any changes, I'd like to hear some
feedback. :-)

Some concerns of mine:

I don't explicitly mention that you can call the functions from anywhere
within a class, not just a think function. How obvious is it that you can do
that?
I don't mention DATADESC. I don't understand it, can't make it work, and all
of my experiments worked fine without it anyway. Is it important?
I don't mention AI thinking. Should I?
Is it actually any good?

Here's the copy. I hope you can view HTML e-mails:

------------------------------------------------------------------------------

Thinking

An entity's Think() function is the "root" gateway used to run all internal
logic. It is called once on spawn, with any subsequent calls decided by the
programmer – happening either on a regular basis, or in response to external
events.

Use ClientThink()[1] to have an entity think every frame.

SetNextThink()

Defines when the entity next thinks.
Accepts a floating point[2] value.
If more than one call is made in a single execution, the new value overrides
the old.

void CMyEntity::Think()
{
        Msg( "I think, therefore I am.\n" );
        SetNextThink( gpGlobals->curtime + 1.0f ); // Think again in 1.0 seconds
}

This code causes the entity to print a message to the console once per second.
Note the use of gpGlobals->curtime, which returns the time at execution, and f,
which tells the C++ compiler that we are submitting a floating-point value and
not an integer[3].

SetThink()

Changes the active think function
Accepts a function pointer[4]: add ‘&’ before the name and omit its
closing parentheses.
If more than one call is made in a single execution, the new value overrides
the old.

void CMyEntity::Think()
{
        Msg( "I think, therefore I am.\n" );
        SetThink( &CMyEntity::Think2 ); // Think with this procedure next
        SetNextThink( gpGlobals->curtime + 1.0f );
}

void CMyEntity::Think2()
{
        Msg( "Variety is the spice of life.\n" );
        SetThink( &CMyEntity::Think ); // Think with this procedure next
        SetNextThink( gpGlobals->curtime + 1.0f );
}

This code switches thinking between two functions. A real-world application is
to change an entity between various life stages: consider a buildable gun
turret[5]. One think function would run while it waits to be unpackaged,
another while it is being built, another while it is active, and a fourth when
it dies. Creating think functions for each discrete stage increases code
stability and aids debugging.

ClientThink()

Thinking can also occur on the client, but its effects are limited.
Additionally, only one think function is supported for each entity.

void C_MyEntity::ClientThink()
{
        DevMsg( "Don't put anything expensive in this function!\n" );
        SetNextClientThink( CLIENT_THINK_ALWAYS ); // Think every frame
}

Some examples of client-side thinking are:

Visual effects / particles
VGUI screen interaction
Modifying player speed (calculated on the client as well as server to avoid
lag)
Striders’ legs snapping ropes (disabled by default)

SetNextClientThink()

Used to re-fire ClientThink(). In addition to normal float values, it accepts:

CLIENT_THINK_ALWAYS
Think on the client once every frame. Use with caution!
Replaces Simulate().
CLIENT_THINK_NEVER
Stop all client-side thinking without killing the entity.

===References:===
  1. 
http://developer.valvesoftware.com/w/index.php?title=Generalities_On_Entities&action=submit#ClientThink.28.29
  2. http://en.wikipedia.org/wiki/Floating_point
  3. http://en.wikipedia.org/wiki/Integer_%28computer_science%29
  4. http://en.wikipedia.org/wiki/Pointer_%28computing%29
  5. http://forums.gamedesign.net/viewtopic.php?t=3702

_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives, please 
visit:
http://list.valvesoftware.com/mailman/listinfo/hlcoders





_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives, please 
visit:
http://list.valvesoftware.com/mailman/listinfo/hlcoders

Reply via email to