Skip to content

[Help] Script StartingConditional, if TRUE then..

KrazenpoxKrazenpox Member Posts: 12
edited March 2018 in Builders - Scripting
Hello, I need some help with a script, during a dialog I'd like to check if the player has an item in his inventory, if TRUE then destroy it and reward the player. With the script wizard I have created the simple conditional script:
#include "nw_i0_tool"

int StartingConditional()
{

    // Make sure the PC speaker has these items in their inventory
    if(!HasItem(GetPCSpeaker(), "teteproprio"))

        return FALSE;



    return TRUE;

}
I don't know how to add the fact that if the player has the item, it will destroy it and give him reward. Is it possible? Thanks for your help :)


PS: Is there an active community or forum about scripting help for newbies like me?

Comments

  • Sylvus_MoonbowSylvus_Moonbow Member Posts: 1,085
    edited March 2018
    When you are in a conversation use the red scripting hat (scripts wizard) that appears under the actions taken portions of the convo. Check give rewards and take from the player together. Go through the sets involved for giving a reward (gold, xp, item) and then the next option will be what you want to take. Then save the script so it does both of these things for you.

    It looks like you only checked take from the player to make that script.
  • KrazenpoxKrazenpox Member Posts: 12
    edited March 2018
    The solution was so simple! I was thick headed with that script I didn't thank there was other solutions, thank you Sylvus_Moonbow :)
    (Sorry for my approximative english :blush: ).
  • squattingmonksquattingmonk Member Posts: 59
    Krazenpox said:

    PS: Is there an active community or forum about scripting help for newbies like me?

    Certainly! For a script reference, see the NWN Lexicon. And for the most community help, see the Neverwinter Vault.

  • BalkothBalkoth Member Posts: 161
    If you want to learn how to get that result without using the script wizard, let us know. I know I at a minimum would be happy to help explain how it works.
  • KrazenpoxKrazenpox Member Posts: 12
    Yes, I'd like to know how to have the same result with one script, what have I to add in the script to get something like: if the condition is TRUE then reward and destroy object, if FALSE nothing happen..? Thanks.
  • BalkothBalkoth Member Posts: 161
    edited March 2018
    I'm going to give the simple version and then a more complicated one. Also, please don't get all "YOU'RE BEING INEFFICIENT!" on me. I know this can be done more cleanly with less code -- but I'm trying to be very explicit for someone new at this.

    Simple Version

    Your goal is: if the condition is true then reward and destroy object, if false then nothing happens. For this we'll use the actions taken condition (aka, this happens when you trigger this part of the conversation).

    First, our "pseudocode"...

    if PC has item then
    reward PC
    destroy item

    else
    nothing

    Now, our real code...
    void main()
    {
        // Who are we checking?
        object oPC = GetPCSpeaker();
    
        // What are we checking?
        object oItem = GetItemPossessedBy(oPC, "teteproprio");
    
        // If the item is valid, then the PC must have it
        if (oItem != OBJECT_INVALID)
        {
            // What are we doing if the PC has the thing?
    
            // First we'll reward
            // This gives the PC 10,000 gold
            GiveGoldToCreature(oPC, 10000);
            // This gives the PC 500 XP
            GiveXPToCreature(oPC, 500);
            // This needs a valid blueprint reference to work but then creates a
            // reward item on the PC
            CreateItemOnObject("awesomereward", oPC);
    
            // Now we'll destroy the item
            DestroyObject(oItem);
        }
    
        // Since we don't need to do anything special if the PC lacks the item,
        // that's the end of our code -- nothing will happen at this point
    }
    }
    Now let's get fancier.

    Complicated Version

    What if we don't want the reward script to fire UNLESS the PC has the item? We're going to use the "Text Appears When" tab for this and make a new script there:
    int StartingConditional()
    {
        // Who are we checking?
        object oPC = GetPCSpeaker();
    
        // Get a value of true (1) or false (0) for whether the PC has the item
        int nHasItem = GetIsObjectValid(GetItemPossessedBy(oPC, "teteproprio"));
    
        // return our value -- if it's false, the PC can't ever claim to have the item
        return nHasItem;
    }
    We put this script on a line (in the Text Appears When section) the PC gets like "Hey, I found that thing for you" and it won't appear as an option unless the PC has actually done so. Then put the first script on the Actions Taken to strip the item from the PC and reward the PC appropriately.

    If you'd like a clearer example, I can whip up a simple module showing a basic conversation like this if you want to poke around in it (though probably not tonight, busy rewriting the Aielund Saga's weapon enchanted scripts to be cleaner and work with up to four henchmen).
    Post edited by Balkoth on
  • DazDaz Member Posts: 127
    int nHasItem = GetItemPossessedBy(oPC, "teteproprio");

    should be

    int nHasItem = GetIsObjectValid(GetItemPossessedBy(oPC, "teteproprio"));
  • BalkothBalkoth Member Posts: 161
    That's what I get for typing that up in a rush without testing it in the toolset. Had another error or two in the main script as well. Was busy rewriting the enchanting scripts for Aielund, in my defense >.>
  • squattingmonksquattingmonk Member Posts: 59
    Balkoth said:

    We put this script on a line (in the Text Appears When section) the PC gets like "Hey, I found that thing for you" and it won't appear as an option unless the PC has actually done so. Then put the first script on the Actions Taken to strip the item from the PC and reward the PC appropriately.

    This leaves you open to an exploit, though: the PC can have the item and get the prompt, then drop the item while still in conversation, then select the conversation option and obtain the reward. Then pick the item back up and do it all over again.

    A way around this would be to make the script that does the item removal and reward actually be placed on the Text Appears When node for the NPC:
    int StartingConditional() { object oPC = GetPCSpeaker(); object oItem = GetItemPossessedBy(oPC, "teteproprio"); if (GetIsObjectValid(oItem)) { GiveGoldToCreature(oPC, 10000); GiveXPToCreature(oPC, 500); CreateItemOnObject("awesomereward", oPC); DestroyObject(oItem); return TRUE; } return FALSE; }
    So you're looking at:
    NPC: Have you got the foo?
    |-> PC: I sure do have the foo! (item check as StartingConditional)
    | |-> NPC: Thanks for bringing me the foo. (reward script as StartingConditional)
    | |-> NPC: Sorry, it looks like you don't actually have the foo.
    |-> PC: No, I don't have the foo.
  • KrazenpoxKrazenpox Member Posts: 12
    Thanks for these explanations guys! I will try this last script in my next quest :)
  • BalkothBalkoth Member Posts: 161
    Let us know if you have any problems. I can throw together a small module to show how to do it if you'd like.

    This leaves you open to an exploit, though: the PC can have the item and get the prompt, then drop the item while still in conversation, then select the conversation option and obtain the reward.

    Fair point.

    That said, unless this is for a PW (which wasn't said) I wouldn't worry too much about a deliberate exploit like this. If someone is determined to cheat, DebugMode is a hell of a lot easier. So if it's a major pain to fix a deliberate exploit, better to spend your time being productive. This isn't too bad of a case, though, and I wasn't thinking with security in mind since it seemed unnecessary.
  • TarotRedhandTarotRedhand Member Posts: 1,481
    FWIW I included a pdf document that lists 230+ starting conditional scripts that are already part of NwN, as part of my TR's Basics - Variables, Types and Functions (TBC) tutorial package on the vault.

    TR
Sign In or Register to comment.