Pensando

From Valve Developer Community
Jump to: navigation, search
English (en)Deutsch (de)Español (es)Português do Brasil (pt-br)Русский (ru)中文 (zh)
... Icon-Important.png
Info content.png
This page is being translated.

You can help by finishing the translation.

Also, please make sure the article tries to comply with the alternate languages guide.

Las funciones de pensar, "think functions", permiten a las entidades programar código que más tarde se ejecutará. Mediante reprogramando "thinks", se pueden hacer bucles automáticos, que pueden hacer que una entidad, sea autónoma, pueda pensar por sí misma.

Scheduling / Programar

SetNextThink() se utiliza para configurar cuándo una entidad debería pensar la siguiente acción. Acepta valores flotantes.

void CMyEntity::Spawn()
{
	BaseClass::Spawn();
	SetNextThink( gpGlobals->curtime ); // Pensar AHORA
}

void CMyEntity::Think()
{
	BaseClass::Think(); // Always do this if you override Think() ... Siempre hacer esto si sobreescribimos "Think()".

	Msg( "Pienso, luego existo.\n" );
	SetNextThink( gpGlobals->curtime + 1 ); // Volver a pensar dentro de un segundo.
}

Date cuenta del uso de gpGlobals->curtime, para hacer que un valor que haya sido enviado, sea relativo al tiempo de ejecución.

Tip.pngTip:SetNextThink(-1) cancelará cualquier futuro "think". Esto es mejor que utilizar SetNextThink(NULL), porque TICK_NEVER_THINK ("«tick» nunca piensa) se significa por "-1".

Nuevas funciones de pensar (think)

Una entidad puede tener cualquier número de funciones de pensar adicionales. Para registrar una nueva:

  1. Asegúrate de que la función es un void.
  2. Añádela a la tabla de descripciones de la entidad, es decir, DATADESC English, con el código DEFINE_THINKFUNC().
  3. Llama a la función SetThink() y envía un pointer a la función. Debajo hay un ejemplo.
  4. Asegúrate de que DECLARE_DATADESC(); está en la clase.
BEGIN_DATADESC( CMiEntidad )
	DEFINE_THINKFUNC( MiPensamiento ), // Register new think function  .... Registra una nueva funcion Think()
END_DATADESC()

void CMiEntidad::Spawn()
{
	BaseClass::Spawn();
	SetThink( &CMiEntidad::MiPensamiento ); // Pass a function pointer ... Envia un pointer a la funcion
	SetNextThink(gpGlobals->curtime);
}

void CMiEntidad::MiPensamiento()
{
	Msg( "Pienso, luego existo.\n" );
	SetNextThink( gpGlobals->curtime + 1 );
}

Dividiendo tu código de pensar en diferentes funciones hace más fácil que una entidad pueda cambiar entre distintos modos de operación.

Tip.pngTip:SetThink() también puede ser llamado dentro de una función de pensar. La siguiente llamada será a la nueva función.

Usar contextos

Es posible programar cualquier número de funciones Think(), simultáneamente, con los contextos de pensar, o en inglés 'think contexts'. Para crear un nuevo contexto:

  1. Llama a RegisterThinkContext(string NombreContexto)
  2. Llama a SetContextThink(void* Function, float NextThinkTime, string NombreContexto)
  3. Para Thinks() subsecuentes llama a SetNextThink(float NextThinkTime, string NombreContexto)
BEGIN_DATADESC( CMiEntidad )
	DEFINE_THINKFUNC( NombreContexto ),
END_DATADESC()

void  CMiEntidad::Spawn()
{
	SetNextThink( gpGlobals->curtime ); // Bucle por defecto de pensar - sin contexto 
	
	RegisterThinkContext( "ContextoPrueba" );
	SetContextThink( & CMiEntidad::NombreContexto , gpGlobals->curtime, "ContextoPrueba" );
}

void  CMiEntidad::Think()
{
	BaseClass::Think();

	Msg( "Pensar\n" );
	SetNextThink( gpGlobals->curtime + .1 );
}

void  CMiEntidad::NombreContexto()
{
	Msg( "Pensar contexto\n" );
	SetNextThink(gpGlobals->curtime + .2, "ContextoPrueba" );
}

Esto crea dos bucles de pensar simultáneos, los dos escriben un mensaje en la consola, a una velocidad distinta.

Tip.pngTip:Crear un nuevo contexto es una buena forma de retrasar llamadas a funciones al futuro, sin romper los bucles Think() que se estén ejecutando.

Utilidades

Esto se explica por sí solo:

float	GetLastThink()
float	GetNextThink()
int	GetLastThinkTick()
int	GetNextThinkTick()
Barnacle.jpg

The GetLast functions are useful for controlling the rate at which something occurs. This think code from npc_barnacle English modulates the speed of tongue movement, even if the frequency of thinking changes:

float dt = gpGlobals->curtime - GetLastThink(); // dt is "delta time"
SetAltitude( m_flAltitude + m_flBarnaclePullSpeed * dt ); // Change tongue altitude

For its non-skeletal animation to be smooth this code would need to be executed every frame. This is exactly what happens, until the barnacle is no longer in the player's PVS and the rate is slowed down – thus requiring the above code.

ClientThink()

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

void C_MyEntity::ClientThink()
{
	Msg( "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 (done on the client as well as server to avoid prediction errors)
  • Striders’ legs snapping ropes (disabled by default)

SetNextClientThink() is used to schedule ClientThink(). There are two special values it accepts:

CLIENT_THINK_ALWAYS
Think on the client once every frame. Use with caution!
Tip.pngTip:Use gpGlobals->frametime to regulate speed.
CLIENT_THINK_NEVER
Pause all automated client thinking.