Es/Thinking: Difference between revisions
m (Nesciuse moved page Thinking:es to Es/Thinking over redirect: -Language prefixes) |
m (Using L template for links) |
||
Line 4: | Line 4: | ||
== Scheduling / Programar == | == Scheduling / Programar == | ||
<code>SetNextThink()</code> se utiliza para configurar cuándo una entidad debería pensar la siguiente acción. Acepta valores | <code>SetNextThink()</code> se utiliza para configurar cuándo una entidad debería pensar la siguiente acción. Acepta valores {{L|Float|flotantes}}. | ||
<source lang=cpp> | <source lang=cpp> | ||
Line 58: | Line 58: | ||
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: | 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: | ||
# Llama a <code>RegisterThinkContext( | # Llama a <code>RegisterThinkContext({{L|string}} NombreContexto)</code> | ||
# Llama a <code>SetContextThink( | # Llama a <code>SetContextThink({{L|void}}* Function, float NextThinkTime, string NombreContexto)</code> | ||
# Para Thinks() subsecuentes llama a <code>SetNextThink(float NextThinkTime, string NombreContexto)</code> | # Para Thinks() subsecuentes llama a <code>SetNextThink(float NextThinkTime, string NombreContexto)</code> | ||
Line 113: | Line 113: | ||
</source> | </source> | ||
For its non- | For its non-{{L|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 {{L|PVS}} and the rate is slowed down – thus requiring the above code. | ||
== ClientThink() == | == ClientThink() == | ||
Line 121: | Line 121: | ||
<span style="color:blue;">void</span> C_MyEntity::ClientThink() | <span style="color:blue;">void</span> C_MyEntity::ClientThink() | ||
{ | { | ||
Msg( <span style="color:brown;">"Don't put anything | Msg( <span style="color:brown;">"Don't put anything {{L|expensive}} in this function!\n"</span> ); | ||
SetNextClientThink( CLIENT_THINK_ALWAYS ); <span style="color:green;">// Think every frame</span> | SetNextClientThink( CLIENT_THINK_ALWAYS ); <span style="color:green;">// Think every frame</span> | ||
} | } | ||
Line 129: | Line 129: | ||
* Visual effects / particles | * Visual effects / particles | ||
* VGUI screen interaction | * VGUI screen interaction | ||
* Modifying player speed (done on the client as well as server to avoid | * Modifying player speed (done on the client as well as server to avoid {{L|prediction}} errors) | ||
* Striders’ legs snapping ropes (disabled by default) | * Striders’ legs snapping ropes (disabled by default) | ||
Line 135: | Line 135: | ||
; CLIENT_THINK_ALWAYS | ; CLIENT_THINK_ALWAYS | ||
: Think on the client once every frame. Use with caution! {{tip|Use <code> | : Think on the client once every frame. Use with caution! {{tip|Use <code>{{L|gpGlobals}}->frametime</code> to regulate speed.}} | ||
; CLIENT_THINK_NEVER | ; CLIENT_THINK_NEVER | ||
: Pause all automated client thinking. | : Pause all automated client thinking. |
Revision as of 07:09, 11 July 2024

You can help by finishing the translation.
If this page cannot be translated for some reason, or is left untranslated for an extended period of time after this notice is posted, the page should be requested to be deleted.
Also, please make sure the article complies 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.

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:
- Asegúrate de que la función es un void.
- Añádela a la tabla de descripciones de la entidad, es decir, DATADESC
, con el código DEFINE_THINKFUNC().
- Llama a la función
SetThink()
y envía un pointer a la función. Debajo hay un ejemplo. - 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.

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:
- Llama a
RegisterThinkContext(string NombreContexto)
- Llama a
SetContextThink(void * Function, float NextThinkTime, string NombreContexto)
- 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.

Utilidades
Esto se explica por sí solo:
float GetLastThink()
float GetNextThink()
int GetLastThinkTick()
int GetNextThinkTick()
The GetLast
functions are useful for controlling the rate at which something occurs.
This think code from npc_barnacle 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!
Consejo:Use
gpGlobals ->frametime
to regulate speed. - CLIENT_THINK_NEVER
- Pause all automated client thinking.