Is OnUserDefinedEvent scripts works different in NWN2?

#1

I’m trying to learn advanced NWNscript from Celowin’s tutorials. Basically they were written for NWN1 although in most cases absolutely actual for NWN2.
But it looks like User Defined Events works differently in NWN1 & NWN2, nw_c2_default9 scripts is totally different, and there isn’t SetSpawnInCondition function.

Can you guys help me to understand how to make these scripts work in NWN2?

0 Likes

#2

i believe they do work the same, but there’s likely to be a few “gotchas” along the way,

//:: X0_I0_SPAWNCOND
/*
  This library separates out the spawn-in conditions from
  nw_i0_generic for improved clarity. This cannot be
  dual-#included with nw_i0_generic.
 */

// Sets the specified spawn-in condition on the caller as directed.
void SetSpawnInCondition(int nCondition, int bValid = TRUE);

ie,

  1. set the spawn-in condition in the creature’s OnSpawn script
  2. make sure the creature’s respective ai-script signals that event
  3. handle it in the creature’s user-defined script
0 Likes

#3

Can you post an example of simple OnUserDefinedEvent & OnSpawn scripts for NWN2 please?

0 Likes

#4
// 'sp'
/*
    creature OnSpawn script
*/

#include "x0_i0_spawncond"

void main()
{
    SetSpawnInCondition(NW_FLAG_HEARTBEAT_EVENT);
}
// 'hb'
/*
    creature OnHeartbeat script
*/

#include "x0_i0_spawncond"

void main()
{
    if (GetSpawnInCondition(NW_FLAG_HEARTBEAT_EVENT))
    {
        SignalEvent(OBJECT_SELF, EventUserDefined(EVENT_HEARTBEAT));
    }
}
// 'ud'
/*
    creature OnUserDefined script
*/

void main()
{
    switch (GetUserDefinedEventNumber())
    {
        case EVENT_HEARTBEAT:
            SendMessageToPC(GetFirstPC(FALSE), "hi userdefined event");
            break;
    }
}
1 Like

#5

Thx for quick respond! Script works but as I understand from Celowin’s tutorial, the point of User Defined Events is to simplify scripting and intended not affect event slots so we can keep creature default OnHeathbeat script…

0 Likes

#6

the code in my example hb.nss is already in the stock OnHeartbeat script ( nw_c2_default1 ), at the very bottom

I pasted it above simply to show the idea …

 
as to whether a user-defined script actually lessens scripting or makes things easier is seldom the case in my experience,

usually if I want a custom behavior, i’ll copy an ai-script, rename it, assign it to a creature’s script-slot, modify it … this way only 1 script needs to change instead of two.

but it strongly depends on the “module environment” and how you’d like to do stuff there,

2 Likes

#7

Thank you very much, I get it.

1 Like