Save and restore spell book slots problem

Hi everyone !

I’m trying to create an item that allows you to save the current spellbook slots and restore them.

For that I’m using nwnx2 and nwnx_funcs.dll from Nwnx funcs for windows v0.941 | The Neverwinter Vault. I’m on a windows server.

I encounter several issues :

-For a wizard the NWNXFuncs_SetMemorizedSpellSlot function crashes the server
-For a cleric, I can restore some slots but it’s not consistent, it puts a spell of say a level 0 to a slot of level 3, and only one for each level.

I feel like there is a problem with the function NWNXFuncs_SetMemorizedSpellSlot but I’m not sure.

Here are the 2 main functions of my code :

void storeSpellSlotsForClass(object oPC, object oItem, int iClass){


    int iSpellLevel=0;

    for(iSpellLevel; iSpellLevel<=9; iSpellLevel++){

        int iNBSlots = NWNXFuncs_GetMaxSpellSlots(oPC, iClass, iSpellLevel);
        int j=iNBSlots;
        for(j; j>=0 ;j--){
            struct MemorizedSpellSlot slot = NWNXFuncs_GetMemorizedSpellSlot(oPC, iClass, iSpellLevel, j);
            SetLocalInt(oItem, IntToString(iClass)+"_"+IntToString(iSpellLevel)+"_"+IntToString(j)+"_id", slot.id);
            SetLocalInt(oItem, IntToString(iClass)+"_"+IntToString(iSpellLevel)+"_"+IntToString(j)+"_meta", slot.meta);
            SetLocalInt(oItem, IntToString(iClass)+"_"+IntToString(iSpellLevel)+"_"+IntToString(j)+"_ready", slot.ready);
        }
    }
}
void restoreSpellSlotsForClass(object oPC, object oItem, int iClass){
    int iSpellLevel=0;
    for(iSpellLevel; iSpellLevel<=9; iSpellLevel++){

        int iNBSlots = NWNXFuncs_GetMaxSpellSlots(oPC, iClass, iSpellLevel);

        int j=0;
        for(j; j<=iNBSlots ;j++){

            struct MemorizedSpellSlot slot;
            slot.id = GetLocalInt(oItem, IntToString(iClass)+"_"+IntToString(iSpellLevel)+"_"+IntToString(j)+"_id");

            if(-1 != slot.id){
                slot.meta = GetLocalInt(oItem, IntToString(iClass)+"_"+IntToString(iSpellLevel)+"_"+IntToString(j)+"_meta");
                slot.ready = GetLocalInt(oItem, IntToString(iClass)+"_"+IntToString(iSpellLevel)+"_"+IntToString(j)+"_ready");

                NWNXFuncs_SetMemorizedSpellSlot(oPC, iClass, iSpellLevel, j, slot);
            }
        }
    }
}

Here is a link to the module : spellbook

Can you help me find out what’s wrong please ?

Thank you !