Sleeping NPC, part deux

Though I may have going through some NWN Scripting tutorials on YouTube, which I am afraid, may give steps to a simple resolution, they do not impart understanding to be able to see sources of problems.

When this, as recommended by Proleric

{https://neverwintervault.org/comment/32230#comment-32230}, is placed OnSpawn, NOTHING HAPPENS.

void main()
{
effect eLieDown = EffectSleep();
effect eSnore = EffectVisualEffect (VFX_IMP_SLEEP);
effect eSleep = EffectLinkEffects (eLieDown, eSnore);
ApplyEffectToObject (DURATION_TYPE_PERMANENT, eSleep, OBJECT_SELF);
}

Yet, strangely, the NW Script Editor did not find ANYTHING wrong with it.

10-Nov-20 12:40:48: 0 Errors. ‘onspnsleep’ compiled successfully

When the following, modified script, is replaced from HeartBeat to OnSpawn, NOTHING HAPPENS.

///////////
//Drunken fool
/////////// //////////////////////////////////////////
//This script rolls 1d10 and causes a random walking NPC to be have as a drunk.
//Not that I know anything about that :slight_smile:
///////////
//Script by Christopher Bernert
//Date Created: 6/27/02
//////////////////////////////////////////////////////
//object oPC = GetLastPerceived(); This can go in to recognize PCs in the future
//int iRollTen = d10(1);
void main()
{
//object oAle;
//oAle = (NW_IT_THNMISC002);
//DelayCommand(30.0, ActionRandomWalk());
//if(d100(1) > 41) //60% chance of performing a drunken act
{
{
// switch(iRollTen) //Generated Number
{
// case 10:
// ClearAllActions();
ActionDoCommand(ActionPlayAnimation

(ANIMATION_LOOPING_DEAD_FRONT));
effect eVis = EffectVisualEffect(VFX_IMP_SLEEP);
ApplyEffectToObject(DURATION_TYPE_PERMANENT, eVis, OBJECT_SELF);
ActionDoCommand(ActionPlayAnimation

(ANIMATION_LOOPING_DEAD_FRONT));
ApplyEffectToObject(DURATION_TYPE_PERMANENT, eVis, OBJECT_SELF);
ActionDoCommand(ActionPlayAnimation

(ANIMATION_LOOPING_DEAD_FRONT));
//ActionDoCommand(ActionPlayAnimation

(ANIMATION_LOOPING_PAUSE_DRUNK));
// break;
}
}
}
}

=============================================
10-Nov-20 12:45:22: 0 Errors. ‘sleepingnpc’ compiled successfully

Yet again, strangely, the NW Script Editor did not find ANYTHING wrong with it.

Even more strangely, when the above modified drunk to sleep is placed on HeartBeat,
the NPC sleeps, gets up, goes back to sleep, rinse/repeat/ad infinitum.

So exactly where is the problem and why in the above?

In points:

  1. If you need help with your script, you should format it so it is readable on the forum ([code] & [/code] tags)
  2. You did not state your problem - I assume you want an NPC to fall asleep on spawn.
  3. Script correctness at compile time != script correctness at runtime.
  4. In OnSpawn, add only EffectSleep() to OBJECT_SELF with a short DelayCommand.
  5. To add repeating snore VFX, edit nw_g0_sleep (1) and apply VFX_IMP_SLEEP there to OBJECT_SELF with DURATION_TYPE_INSTANT.

(1) nw_g0_sleep (just like other nw_g0_*) are vanilla scripts that replace creature’s OnHeartbeat when it is under conditions that are supposed to take away the control (sleep, fear, etc). These scripts can be freely modified.

1 Like

The code I provided was a snippet, not a script. It needs to be inserted towards the end of the official on spawn script where the User Defined code is called (or in the User Defined event script itself, if enabled). Check that the creature instance is actually calling the modified script.

The same may well apply to the second example, too.

The script editor can only tell you whether your script has syntax errors. It has no knowledge of your intentions, so it can’t tell you whether the script will have the outcome you desire.

wow.
and right on cue, if I may add:
you should have know this, and that, and all of those, and these too!”
as spoken high atop the ivory tower.

thanks.

Nah, they’re only trying to help you understand how scripting works, not trying to make you feel bad. Do some really simple tutorials that are written for Neverwinter Nights itself, and you’ll get more comfortable with how it all goes together. Scripting is a language, and nobody expects you to know how to speak a new language without learning it. But nobody can teach you it if you get mad at each lesson either. Accept their help, and learn from it. Seek out those easier tutorials (hint: check nwnlexicon.com and do those tutorials on that site). Start with Celowin: The Basics, and work your way through.
Good luck!

https://nwnlexicon.com/index.php?title=Category:Beginning_Scripting

2 Likes

For tutorials see this pinned thread. For using these forums see this pinned thread. Finally for a list of scripts and systems that involve minimal scripting by the builder see this pinned thread.

TR

Actually it’s quite easy.

This is a copy of the original On-Spawn script with a few added lines. The creature will fall asleep.
Make a new special on_spawn script for the sleeper.

#include "NW_O2_CONINCLUDE"
#include "NW_I0_GENERIC"

void main()
{
  ApplyEffectToObject(DURATION_TYPE_PERMANENT, EffectSleep(), OBJECT_SELF);

  GenerateNPCTreasure();
  SetListeningPatterns();
  WalkWayPoints();
}

The original script fot the heartbeat “nw_c2_default1” already has some code, concerning sleeping. Make a copy of the script for the sleeper and change the section as follows:

  if (GetHasEffect(EFFECT_TYPE_SLEEP))
    {
      effect eVis = EffectVisualEffect(VFX_IMP_SLEEP); 
      int i = GetLocalInt (OBJECT_SELF, "SleepDelay");
      SetLocalInt (OBJECT_SELF, "SleepDelay", 1-i);
//Snoring will only happen every two heartbeats
      if (i) ApplyEffectToObject(DURATION_TYPE_INSTANT, eVis, OBJECT_SELF);
    }

Thats all.

Tested with 1.69

1 Like