Author Topic: If/Else statements with a foggy memory  (Read 312 times)

Legacy_Indigo

  • Newbie
  • *
  • Posts: 44
  • Karma: +0/-0
If/Else statements with a foggy memory
« on: January 09, 2015, 08:45:33 am »


               

A little brief background, I've been revamping this old Local Vault of mine and I decided to have a few widgets for players that spawn and despawn objects. I've done it before, but unfortunately that was for a different server that I was helping out with for a short time and needless to say all these years later I have no idea where my .erfs ran off to. So I'm trying to do this to the best of my foggy brain's ability (even though NWscript is much much prettier than Skyrim's Papyrus, because Papyrus Fragments are jerks). It's been a while and I'm slowly getting there though!


 


 


Now what I'd like to achieve is basically using the same misc. item to spawn and destroy it's placeable without creating duplicated placeables. So far the script below does it, but I've been able to somewhat abuse it by waiting until the delay value goes up. And it also deletes cauldrons from other players. 


 


 


I know I could make the placeable stay there for a certain amount of time (until it's destroyed) and use delay values to fix that part, but then I can't use the same script idea for items to summon chairs and other placeable items. I also thought about using include scripts (for extra functions/effects), but I don't know if that'd help me or not. I'm hoping that the script below isn't too far off from what I'm trying/wanting to accomplish (which if you can let me know if I was close or not, I'd be very grateful).


 


Any help would be greatly appreciated! 



//Portable Player Cauldron

void main()
{
    effect eVFX;
    object oItem = GetItemActivated();
    object oPC = GetItemActivator();
    string sItem = GetTag(oItem);
    object oActTarget = GetItemActivatedTarget();
    location llocation = GetLocation(oPC);
    int cauldron = GetLocalInt(oPC, "HAS_CAULDRON");

    if(sItem == "playercauldron")
    {
        int cauldron = GetLocalInt(oPC, "HAS_CAULDRON");

        if(cauldron == 0)
        {       //Checks to see if there's a cauldron. If there's not, make one with visual effects.
            ApplyEffectAtLocation(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_SUMMON_MONSTER_1), llocation);
            object oNew = CreateObject(OBJECT_TYPE_PLACEABLE, "playercauldron", llocation);
            SetLocalInt(oPC, "HAS_CAULDRON", 1);
                 //String message to tell player how to destroy placeable cauldron.
            FloatingTextStringOnCreature("Make sure to select the cauldron to destroy it with this widget!", oPC);
        }
       else if (cauldron == 1)
        {       //Checks to see if there's a cauldron or not by using HAS_Cauldron and a delay.
                //If cauldron is selected, it's destroyed with visual effects.
            eVFX = EffectVisualEffect(VFX_FNF_DISPEL);
            ApplyEffectToObject(DURATION_TYPE_INSTANT, eVFX, oActTarget);
            DestroyObject(oActTarget, 1.0);
            DelayCommand(20.0, SetLocalInt(oPC, "HAS_CAULDRON", 0));//Delays cauldron respawn to prevent abuse after cauldron is destroyed.
            FloatingTextStringOnCreature("You have to wait a while to use this item again.", oPC);//Sends a string if used again within 10 seconds.
        }
    }
}



               
               

               
            

Legacy_Proleric

  • Hero Member
  • *****
  • Posts: 1750
  • Karma: +0/-0
If/Else statements with a foggy memory
« Reply #1 on: January 09, 2015, 11:41:12 am »


               You could use two locals on the player - an object (the cauldron) and a cool down flag.

if cool down flag is set, do nothing.

else if the cauldron object is invalid, create it, and store it on the player.

else if the target matches the local object on the player, destroy it, clear the local object, set cool down flag, clear the cool down flag after a delay.

That would prevent interference with other players, and prevent destruction of some other object if the item is used on it.

I think that will do it, unless there's an issue with the delay that I've misunderstood?
               
               

               
            

Legacy_Indigo

  • Newbie
  • *
  • Posts: 44
  • Karma: +0/-0
If/Else statements with a foggy memory
« Reply #2 on: January 09, 2015, 05:17:56 pm »


               

I don't think I've ever tied in two different locals on a player using the same before. Is there any way I can get an example on how to do that with the cool down flag? I'm in the Toolset now, so I'm tinkering a little, but something tells me my brain's definitely gonna be straining to remember that. But that definitely sounds like what I'm wanting/trying to do with the script.


 


The main thing I want it to detect would be the actual recent placeable/ last spawned placeable from the widget , Should I be using GetLocalObject for that?


 


And I don't think you misunderstood what I was talking about with the delays! The delay I figure is just that extra added padded cushioning in trying to prevent someone spamming the widget. 



               
               

               
            

Legacy_Squatting Monk

  • Hero Member
  • *****
  • Posts: 776
  • Karma: +0/-0
If/Else statements with a foggy memory
« Reply #3 on: January 10, 2015, 12:48:33 am »


               

The main thing I want it to detect would be the actual recent placeable/ last spawned placeable from the widget , Should I be using GetLocalObject for that?




Yes, that's right. CreateObject() returns an object, which you can then set as a local variable on the PC. When destroying the cauldron, just use GetLocalObject() to return the PC's cauldron and delete that one. Here's how I'd do it:



void main()
{
    object oPC   = GetItemActivator();
    object oItem = GetItemActivated();
    string sTag  = GetTag(oItem);
   
    if (sTag == "playercauldron")
    {
        // See if the PC has a cauldron.
        object oCauldron = GetLocalObject(oPC, "PLAYER_CAULDRON");
        if (GetIsObjectValid(oCauldron))
        {
            // We do have a cauldron, so destroy it and put the item on cooldown.
            effect eVFX = EffectVisualEffect(VFX_FNF_DISPEL);
            ApplyEffectToObject(DURATION_TYPE_INSTANT, eVFX, oCauldron);
            DestroyObject(oCauldron, 1.0);
            SetLocalInt(oPC, "CAULDRON_ON_COOLDOWN", 1);
            DelayCommand(20.0, DeleteLocalInt(oPC, "CAULDRON_ON_COOLDOWN"));
            FloatingTextStringOnCreature("You have to wait a while to use this item again.", oPC);
        }
        else if (GetLocalInt(oPC, "CAULDRON_ON_COOLDOWN"))
        {
            // The PC has destroyed the cauldron, but the item is still on cooldown.
            // Tell the PC so he knows what's up.
            FloatingTextStringOnCreature("You have to wait a while to use this item again.", oPC);
        }
        else
        {
            // We do not have a cauldron and the item is not on cooldown.
            // Make a new cauldron where the item was activated.
            location lLoc = GetItemActivatedTargetLocation();
            effect   eVFX = EffectVisualEffect(VFX_FNF_SUMMON_MONSTER_1);
            ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eVFX, lLoc);
            oCauldron = CreateObject(OBJECT_TYPE_PLACEABLE, "playercauldron", lLoc);
            SetLocalObject(oPC, "PLAYER_CAULDRON", oCauldron);
            FloatingTextStringOnCreature("Make sure to destroy the cauldron by using this widget again!", oPC);
        }
    }
}


               
               

               
            

Legacy_Indigo

  • Newbie
  • *
  • Posts: 44
  • Karma: +0/-0
If/Else statements with a foggy memory
« Reply #4 on: January 10, 2015, 04:13:37 am »


               


 


Yes, that's right. CreateObject() returns an object, which you can then set as a local variable on the PC. When destroying the cauldron, just use GetLocalObject() to return the PC's cauldron and delete that one. Here's how I'd do it:




 


 


Oooooh my god, I'm kicking myself right now. I can't believe I COMPLETELY butchered GetIsObjectValid! I totally facepalmed when I read it. I KNEW it was something like that, but after 20 or so compile errors and nothing coming back in the Lexicon I was like "I'm doing something wrong." I was searching for GetIsValidObject instead of GetIsObjectValid. That'd PROBABLY be why I couldn't find it or get it to compile. When I read "if (GetIsObjectValid(oCauldron))", I was dumbfounded. I still am, but that definitely looks better than the script that I re-wrote a couple dozen times (hence why my original script was really cluttered there at the top).


 


This definitely helped, SquattingMonk. If I could like both your post and Proleric's a bajillion times, I totally would! Thank you both for putting up with my bad memory. xD


 


Edit: Also random and slightly related question, is there a Notepad ++ extension/plugin for NWscript? I found threads here that linked to the old Vault with it, but I'm not seeing it. D:



               
               

               
            

Legacy_Squatting Monk

  • Hero Member
  • *****
  • Posts: 776
  • Karma: +0/-0
If/Else statements with a foggy memory
« Reply #5 on: January 10, 2015, 04:34:59 am »


               

Oooooh my god, I'm kicking myself right now. I can't believe I COMPLETELY butchered GetIsObjectValid! I totally facepalmed when I read it. I KNEW it was something like that, but after 20 or so compile errors and nothing coming back in the Lexicon I was like "I'm doing something wrong." I was searching for GetIsValidObject instead of GetIsObjectValid. That'd PROBABLY be why I couldn't find it or get it to compile.




Don't feel too bad. I do that all the time, but usually write GetIsValid() instead.

 



Edit: Also random and slightly related question, is there a Notepad ++ extension/plugin for NWscript? I found threads here that linked to the old Vault with it, but I'm not seeing it. D:




Indeed there is, and that's what I wrote this on. '<img'>



               
               

               
            

Legacy_Indigo

  • Newbie
  • *
  • Posts: 44
  • Karma: +0/-0
If/Else statements with a foggy memory
« Reply #6 on: January 10, 2015, 07:00:47 am »


               

OH MY GOD! You are indeed a lifesaver! Thanks for this! I really just need to take a whole day and look over the new vault. I'm sure there's loads of new fun builder toys I'm missing out on.