Skip to content

Respawning chests script doubt

So, I got back to some scripting, and I have this problem that I just can't understand. My objective is just to respawn chests. I have been successfully respawning chests on my module up to now, but one chest has a different tag from its resref, and I have been using the tag to derivate the resref. So instead, I simply used the GetResRef function. But for some reason, this breaks all respawns, even the ones that have been working.

This is the code of the function I used to recreate an object. using sResRefWorks, it works, but if I try to switch to sResRef it does not work. By work, I mean one chest is respawn, but instead of the current resref, it uses the tag of the chest to decide de resref.

void recreateObject(object oCont, string sTag, int iType, location lLoc)
{
DestroyObject(oCont, 0.0);
string sResRef = GetStringLeft(GetResRef(oCont),16);
string sResRefWorks = GetStringLowerCase(GetStringLeft(sTag, 16));

CreateObject(iType, sResRefWorks, lLoc);
}

I did some debugging with talking chests, and it seems that the strings are equal. So two questions: 1) Why my GetResRef is not working? And 2) is there a way to respawn the chests based on the resref instead of the tag?

Comments

  • squattingmonksquattingmonk Member Posts: 59
    Why not just:
    string sResref = GetResRef(oCont); CreateObject(iType, sResRef, lLoc);
  • fot1fot1 Member Posts: 74

    Why not just:
    string sResref = GetResRef(oCont); CreateObject(iType, sResRef, lLoc);

    Thanks, for some reason this also fails. I can't understand why. The GetStringLeft with 16 was because I tought it was a necessary padding or something. If Nobody answers this up to tomorrow I'll upload a demo module with the problem
  • squattingmonksquattingmonk Member Posts: 59
    GetStringLeft() will not add padding onto the end. If you upload a demo module, I'll take a look.
  • FreshLemonBunFreshLemonBun Member Posts: 909
    You are probably using the wrong instance of an object. The scripts used and the resref have to match the object in the palette to create a perfect loop.

    Check using the view menu option "preview" what the reference value is, and check that the object in your area has the same resref and isn't a copy, a copy will have an altered reference like "001" on the end. Also check that the scripts used in the object in your area are the same scripts used in your chest in the palette.

    If it's the same then it will work.
  • fot1fot1 Member Posts: 74
    So, I was setting up a minimal module to reproduce this bug, and I ended up perceiving the error myself. Let me explain.

    This function `recreateObject` is set up on the object opening, with a delay. If the object is destroyed in the meantime, the GetResRef fails, while the sTag still works because it is being passed as a parameter. To fix it, I passed the resRef as parameter itself.

    Thanks for the help, and sorry for the noise.
  • RutgerMaFerRutgerMaFer Member Posts: 2
    It's been quite a few years since I ran my NWN server. However, I had my own chest respawn script. I didn't use a delay to destroy my chest. I placed invisible objects at every location where I wanted the possibility for a chest to spawn. Each invisible object (Looked like a flag in the toolset) had the resref stored for the proper chest to spawn at that location. This allowed me to use different chests for different zones. It also allowed me to randomly pick from a number of locations to spawn a chest. The other nice thing about using objects for locations is that the chest would spawn facing the direction you desire.

    When the server would load, it would pick a time for the first spawn in order to prevent players from charging all known chest locations immediately upon server startup. Once all chests were spawned, a new time would be selected for all the remaining chests to be destroyed and new locations chosen to spawn chests.
  • FreshLemonBunFreshLemonBun Member Posts: 909
    Either works. The above problem was because object variables are a reference so when the game entity that the variable references is destroyed then the object is returned as OBJECT_INVALID or something, and functions for names or tags will return empty "" instead of a value. It's not really a bug with delaying scripts but obviously it can happen if the object is destroyed during the delay.

    There are lots and lots of ways you can make self spawning and random treasure and monster scripts. I personally think non-designed or machine generated dungeons can be interesting because not even the builder will know what happens where.
Sign In or Register to comment.