Author Topic: Making an item unequippable under certain conditions  (Read 352 times)

Legacy_Grani

  • Hero Member
  • *****
  • Posts: 1040
  • Karma: +0/-0
Making an item unequippable under certain conditions
« on: November 18, 2013, 08:44:30 pm »


                I wanted torches not to be used in underwater areas and I thought the best way to bite this is make an OnEquip script that unequips the item provided PC is located in a certain area. I also threw in a message for the PC informing them about torches not being usable underwater.

The thing is, the message works, but the unequip itself doesn't and I dunno why.
I even added a delay, thinking a short break will help, but it doesn't work anyway.

What's wrong?


void main(){object oPC = GetPCItemLastEquippedBy();

if (GetLocalInt(GetArea(oPC), "UNDERWATER_AREA")== 1)   {
   FloatingTextStringOnCreature("You can't use torches underwater!", oPC);
   DelayCommand(1.0, AssignCommand(oPC, ActionUnequipItem(GetObjectByTag("nw_it_torch001"))));
   }}

Thanks.
               
               

               


                     Modifié par Grani, 18 novembre 2013 - 08:45 .
                     
                  


            

Legacy_FunkySwerve

  • Hero Member
  • *****
  • Posts: 2325
  • Karma: +0/-0
Making an item unequippable under certain conditions
« Reply #1 on: November 18, 2013, 10:50:10 pm »


               Most likely problem is that you're using GetObjectByTag, which, while fast, will get the first item in the mod with that tag, and not necessarily the one the pc just equipped. Use instead:
object oItem = GetPCItemLastEquipped();
and check to see if oItem shoudl be unequipped however you like (in this case checking tag, for example).

Also, you'll want to use a custom function to force the unequip, or players will be able to circumvent it, both intentionally and by mistake. This is what we use:


void ForceUnequip (object oTarget, object oItem) {
    if (!GetIsObjectValid(oTarget) || GetObjectType(oTarget) != OBJECT_TYPE_CREATURE)
        return;

    if (!GetIsObjectValid(GetArea(oTarget))) {
        DelayCommand(5.0, ForceUnequip(oTarget, oItem));
        return;
    }

    if (GetIsDead(oTarget)) {
        ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectResurrection(), oTarget);
        ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectHeal(GetMaxHitPoints(oTarget)), oTarget);

        if (GetIsPC(oTarget))
            AssignCommand(oTarget, ExecuteScript("fky_deathprocess", oTarget));

        DelayCommand(0.1, ForceUnequip(oTarget, oItem));
    } else {
        AssignCommand(oTarget, ClearAllActions(TRUE));

        SetLocalInt(oItem, "ForceUnequipped", TRUE);
        AssignCommand(oTarget, ActionUnequipItem(oItem));
        AssignCommand(oTarget, ActionDoCommand(SetCommandable(TRUE)));
        AssignCommand(oTarget, SetCommandable(FALSE));
    }
}

You can remove this bit, unless you have another script you like to execute when a player is brought back to life, in which case you can swap it out with "fky_deathprocess":

        if (GetIsPC(oTarget))
            AssignCommand(oTarget, ExecuteScript("fky_deathprocess", oTarget));


Funky
               
               

               
            

Legacy_Grani

  • Hero Member
  • *****
  • Posts: 1040
  • Karma: +0/-0
Making an item unequippable under certain conditions
« Reply #2 on: November 19, 2013, 09:56:57 am »


               Thanks, Funky!
I had to add a 0.2s delay, for it didn't seem to work properly otherwise, but it works fine otherwise!
               
               

               
            

Legacy_FunkySwerve

  • Hero Member
  • *****
  • Posts: 2325
  • Karma: +0/-0
Making an item unequippable under certain conditions
« Reply #3 on: November 19, 2013, 05:16:05 pm »


               It shouldn't need a .2 delay, so long as you're calling it from onequip. Try this script (or add it to your onequip, assuming you already have something in there):


void ForceUnequip (object oTarget, object oItem) {
    if (!GetIsObjectValid(oTarget) || GetObjectType(oTarget) != OBJECT_TYPE_CREATURE)
        return;

    if (!GetIsObjectValid(GetArea(oTarget))) {
        DelayCommand(5.0, ForceUnequip(oTarget, oItem));
        return;
    }

    if (GetIsDead(oTarget)) {
        ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectResurrection(), oTarget);
        ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectHeal(GetMaxHitPoints(oTarget)), oTarget);

        if (GetIsPC(oTarget))
            AssignCommand(oTarget, ExecuteScript("fky_deathprocess", oTarget));

        DelayCommand(0.1, ForceUnequip(oTarget, oItem));
    } else {
        AssignCommand(oTarget, ClearAllActions(TRUE));

        SetLocalInt(oItem, "ForceUnequipped", TRUE);
        AssignCommand(oTarget, ActionUnequipItem(oItem));
        AssignCommand(oTarget, ActionDoCommand(SetCommandable(TRUE)));
        AssignCommand(oTarget, SetCommandable(FALSE));
    }
}

void main() {
    object oPC = GetPCItemLastEquippedBy();

    object oItem = GetPCItemLastEquipped();
    object oArea = GetArea(oPC);

    string sTag = GetTag(oItem);
    if (sTag == "nw_it_torch001" &&
        GetLocalInt(oArea, "UNDERWATER_AREA")== 1) {
        ForceUnequip(oPC, oItem);
    }
}

Funky