Scale NPC Level to PC Level

I’m looking to have any spawned NPC to be set to the same level as the PC. Not sure how best to do this. It would be per NPC spawn or area wide. I was thinking this may go into the onpercieve, but if I’m at far enough range the mob is not going to match the pc level. So Onspawn makes sense. Looking for suggestions and examples. I’ve searched around but haven’t found quite what I’m looking for.

If you’re spawning the NPC via the official encounter system, or on some event, OnSpawn is fine. Bear in mind though that placed NPCs will spawn when the module first loads, which may be inappropriate if the PC will encounter them much later on, having gained levels.

So, for placed NPCs, the OnEnter event of an area or trigger is better. You can either do that on first entry (setting a flag to prevent repetition) or every time, depending on whether you are OK with the PC returning a few levels later if they can’t beat the NPC first time.

There are further complications in multiplayer, but I’m no expert on that.

Whatever event you decide to use, here’s a levelling up function. As the Lexicon explains, levelling up can fail unless you make the creature template at level 1 with the Creature Wizard, and don’t tamper with its package (though you can freely change properties that don’t level up). For some classes, the default packages suck, so you may have to do some tweaking after calling the function.

// Level up encounter
// sNPC    - level up all creatures with this tag (unless oTarget is set explicitly).
// nDelta  - level up to the PC level, plus or minus this amount.
// oTarget - if set, only level up this creature.
void   bhLevelUp(string sNPC, int nDelta = 0, object oTarget = OBJECT_INVALID)
  object oPC = GetFirstPC();
  int nLevel = GetHitDice(oPC) + nDelta;

  int n = -1;
  object oNPC = GetObjectByTag(sNPC, ++n);

  if (GetIsObjectValid(oTarget)) oNPC = oTarget;

  while (GetIsObjectValid(oNPC))

      while (nLevel > GetLevelByClass(GetClassByPosition(1, oNPC), oNPC))

          if (!LevelUpHenchman(oNPC, GetClassByPosition(1, oNPC), TRUE))
              SendMessageToPC(oPC, StringToRGBString(sNPC + " level up failed.", STRING_COLOR_ROSE));

      oNPC = GetObjectByTag(sNPC, ++n);

      if (GetIsObjectValid(oTarget)) oNPC = OBJECT_INVALID;
1 Like

Awesome. Thank you. I’ll work with this.

Mobs are spawned in when a pc enters an area.So this should work perfectly.

If I throw this into a script and add it to execute from my current onspawn, should it work? I’m not getting the error message so I don’t think it’s triggering. Should I have anything included?

Since post is tagged nwnx, probably better off using that level up function. Don’t think anyone has time to explain LevelUpHenchman and differences between patch versions and package files and…

Yes. As with all functions, you either need a function prototype or the entire function before the main section of the script. Also, you’ll need to include StringToRGBString via

#include "x3_inc_string"

if you don’t already have it in your script.

Ensure that the script compiles without errors.

In game, the script itself will only produce a message if level up fails, but for test purposes you can add a message reporting the new level, or, less scientifically, just check the CR that results.

Unless I’m missing something, there’s nothing about nwnx in the OP.

There is only one forum for Toolset, Scripting, NWNX, so that’s not a tag, either.

As I understand it, nwnx is not available to SP.

For beginners (and beyond) LevelUpHenchman works fine, though experts in package finesse could no doubt write volumes on the subject.

1 Like

The one thing you need is to setup proper packages in the npcs to level up.

Beware of Class prerequisites and restrictions

I’m not having much luck in testing.

I went so far as to just create a copy of the default goblin. Ensured it at level 1. Changed class to fighter. Set onspawn script.

Is this what might be holding it back from leveling? Testing with a Scout (21) Fighter (2). I use PRC/CEP. So I would expect the mob to be leveled to 23?

How do I setup class packages as mentioned above when I need to leave the mobs untouched in terms of level, abilities, skills?

Tested with other normal classes like Wiz and Sorc in case it was the PRC class causing the issue. Just doesn’t seem to be running the script. I took my normal onspawn script out and just set what Proleric provided for the test goblin, and still no messages to PC upon entering which triggers sparky spawner to spawn in the mob via resref. Still showing effortless so I don’t believe it’s working yet.

Think you’d be ok with the goblin caster…the one that doesn’t have Humanoid as its first class (no package file). Proleric’s snippet looks fine…I’d simplify script a lot more and just use the in game script editor to play with in dm mode, get a feel for what works or not much faster.

Note: Was able to level up stock lantern archon with no class specified for example.

Ha there’s a in-game script editor via the DM client. TIL. I’ve hardly touched the DM client. I’ll give that a go and see what I get.

There’s your problem. If you change the class, the toolset doesn’t change the package details, so level up fails because the starting package isn’t what it expects.

Use the Creature Wizard to make a Fighter, changing the appearance to Goblin.

In theory, you can edit the creature template retrospectively with GFF Editor so that every detail matches what level up expects, but Creature Wizard gets it right first time.