Home > Tutorials > A Better Way to Check for Existing Items Before Spawning

A Better Way to Check for Existing Items Before Spawning

Many of you who have done my very first tutorial to create an item have been running into a problem.  When the script checks to see if you already have the item before creating a new one, it essentially only looks in your bags.  If the item isn’t in your bags, it can’t find it so creates a new one.

In this small tutorial we will look at a better way of checking for the item before spawning a new one.  A method that should accurately find the item even if it is no longer in your bags.

CountItemsByTag

int CountItemsByTag ( object oPossessor,
string sTag
)

Counts items with the given tag in the given container/inventory.

Parameters:
oPossessor – The placeable or creature to count items in.
sTag – The tag of the items to count.

In the original tutorial, we used the CountItemsByTag function to look and see if the player already had the item.  Essentially this function counts how many of items with the indicated tag are currently present in the specified objects inventory.  If the count was 0, then we figured the player did not have the item, so we created a new one.

This works, but it has several drawbacks.  It only checks the player’s bags.  If the player had the item, but it was somewhere else, the count would still come back as 0, so we would end up spawning a new one.  This most commonly happens when you equip the item on the player or a party member, or stick it in your storage chest.

Essentially, the problem with CountItemsByTag is its limited scope.  It only looks in one spot.  We need something that will have a wider scope, looking in more places for the item.

GetObjectByTag

object GetObjectByTag ( string sTag,
int nNth = 0
)

Returns the nth specified object with the appropriate tag, or OBJECT_INVALID on failure

Parameters:
sTag – The tag reference to get the object by
nNth – If there are multiple objects with the same tag, get the ‘nth’ specified object.

Returns:
the desired object or OBJECT_INVALID on failure

GetObjectByTag is a more generic object function, and is exactly what we need. This function has a much wider scope and will return any object, anywhere, that matches the specified tag as long as that object is currently loaded by the game.

You will notice though that this function works a bit different. Instead of the function returning to us the number of items with the specified tag, it instead returns the actual object itself.   By default it will return the first one it finds.  To make this do what we need we need to combine it with another function.

IsObjectValid

int IsObjectValid ( object oObject )

Returns TRUE if the object oObject exists and is a valid object, FALSE otherwise.

Parameters:
oObject – The object which may be valid.

Returns:
Returns TRUE if the object exists and is valid, FALSE otherwise.
Remarks:
This function is very similiar to checking whether an object is not equal to the OBJECT_INVALID constant, but this is a more robust check. An object that has been passed as a paramater in a delayed function call or has been stored as a local object will not equal the OBJECT_INVALID constant if the object has been destroyed since it was stored or passed. This function however will return FALSE in those circumstances. There is no instance where an object is equal to the OBJECT_INVALID constant and this function will return TRUE.

This function is very simple, and does exactly what it sounds like.  It tells us if the specified object is a valid object or not.

So how would we use these to check for an item before spawning a new one?  In pseudo-code:

Sword = GetObjectByTag("my_sword_tag")
If IsObjectValid(Sword) Then
     Do nothing - We already have the item
Else
     Spawn new Sword

Here is how this might fit into our original script

// All module events
#include "utility_h"
#include "wrappers_h"
#include "events_h"
void main()
{
    event ev   = GetCurrentEvent();
    int nEvent = GetEventType(ev);
    Log_Events("", ev);
    switch (nEvent)
    {
        case EVENT_TYPE_MODULE_LOAD:
        {
            // get the object which contains the player
            object oPlayer = GetHero();

            object oBow = GetObjectByTag("aga_bowofathena");
            if (!IsObjectValid(oBow))
                UT_AddItemToInventory(R"aga_bowofathena.uti",1);
            break;
        }
        default:
        {
            break;
        }
    }
}

Credits

This tutorial is based upon a writeup by Attono on DAMods

Categories: Tutorials
  1. doo
    November 18, 2009 at 7:32 pm

    can u post a complete script for multiple items that we can copy and paste on to our scripts? i know its rather lazy on my part, but i don’t understand scripting but i like making whole sets of armor in one module instead of separate mods for each item.
    looking at all these words above and trying to understand what it all means is the equivalent of somebody opening my brain and placing a cherry bomb inside.

  2. doo
    November 18, 2009 at 7:43 pm

    also, for our mod to be as professional as possible,(like if we are going to upload and share our mod)should the script just make the items appear once? so that once you sell/destroy all of the items you do not have them re-appear on the next load?

    and another question if you don’t mind, is it possible to add a new area to the single player campaign where the player would have to fight a small battle to have the items drop or to access a chest containing the items?

  3. doo
    November 18, 2009 at 7:47 pm

    oh and also i was wondering, why for your mods do i extract them to the overrides folder and for others i use the daupdater to install them?

    • John Vanderbeck
      November 18, 2009 at 8:43 pm

      It actually depends on what the mod overrides. However, since I made my early mods i’ve learned more and i’m actually rewriting them. I’ve already convert my No Follower Autolevel over to use custom events so it is now installed through DAUpdater. Hopefully I can do the same with the others.

  4. November 20, 2009 at 10:38 am

    I think a better way is using a plot mechanism. By using a plot it is like a scripted event, only that it happens on load – runs the script to give the items then saves in the module that the script has already been run. Then there is no reason to run the adding script again because the plot is never going to be set to the state that the plot needs to be for it to run.. Let me show you the code picked up out of a blog discussion on the community site.

    (I assume images aren’t allowed in comments so I’ll just post the links to my screenshots.)

    The code and process below was taken from Joshimoo
    here: http://social.bioware.com/project/422/discussion/166/&p=6


    ^Make a new plot resource. For the code here UID is quickitem000pl_main


    ^Add a new flag. For the code here name is QUICKITEM_ADDED

    Then we go to the scripts.
    http://de.pastebin.ca/1662840

    It works seemingly flawlessly, and doesn’t give the items multiple times if you destroy them as it is checking for the plot flag data not the inventory or game load status.

    • November 20, 2009 at 10:47 am

      I realize I put the wrong image link for the second one. My mistake.

    • November 20, 2009 at 10:49 am

      Also for those having trouble with creating a plotflag to begin with. Right clicking on the open area then creating a main flag works. I do not fully understand the difference between a main and defined flag, but main has worked for me.

      Or you can press Ctrl+M.

    • John Vanderbeck
      November 20, 2009 at 10:57 am

      A Plot flag is a good way, but i’ve avoided them for now because plot flags are supposedly broken in the toolset and can corrupt the single player campaign. I’m waiting for the toolset patch that fixes them before I talk about them.

      • November 20, 2009 at 11:04 am

        I believe the problem with plot flags are matching id’s with the same warnings as using scripts.

        I haven’t seen in any problems with my current play through using them but it is an exceptionally large game so it is quite likely there are some.

        🙂 Honestly it is kinda fun messing around in a slightly broken toolset, because every little thing you do is an adventure. 😀

        The main thing with plot flags that it seems you must do is make them module specific. In the properties that is.

        That said, I understand waiting for the updates before messing with them.

      • John Vanderbeck
        November 20, 2009 at 11:49 am

        Yeah there are ways to make it safe. I’ve done it with my No Starting Abilities mod, which actually modifies base game plot flags.

        But polls show that about 80% of the readers for this blog consider themself a base beginner, so as long as there is even the possibility of a mistake irrevocably damaging a save game, I choose to avoid the subject 🙂 But you are 100% correct in that plot flags are a nice way to go. And once they fix the GUIDs I will talk about them 😀

      • November 20, 2009 at 11:06 am

        Sometimes I am retarded. My first sentence makes no sense.

        I meant to say with the broken database that we are currently working with. I don’t even know where that sentence came from.

        I blame it on the 4 hours of sleep over the last 2 days.

  5. Caladon
    November 21, 2009 at 11:01 pm

    Hey John I had to start all over (completely remove\reinstall game\toolset\sql so I’m trying to redo your Tutorials. I tried this script and tried for multiple items but I keep getting and erroe when compiling — E: 22:55:40 – cdon_event_handler.nss – cdon_event_handler.nss(15): No semicolon after expression —

    Line 10 is my first object line and it has a semi colon at the end here is my script can you tell me what is wrong?

    /——–start script—-/

    #include “utility_h”
    #include “wrappers_h”
    #include “events_h”
    void main()
    {
    event ev = GetCurrentEvent();
    int nEventType = GetEventType(ev);
    Log_Events(“”, ev);
    switch (nEvent)
    {
    case EVENT_TYPE_MODULE_LOAD:
    {
    object oPlayer = GetHero();

    objest oAttrib = GetObjectByTag(“cdon_book_attrib”);
    if (!IsObjectValid(oAttrib))
    UT-AddItemToInventory(R”cdon_book_attrib.uti”, 99);

    objest oSkill = GetObjectByTag(“cdon_book_skill”);
    if (!IsObjectValid(oSkill))
    UT-AddItemToInventory(R”cdon_book_skill.uti”, 99);

    objest oTalentm = GetObjectByTag(“cdon_book_talentm”);
    if (!IsObjectValid(oTalentm))
    UT-AddItemToInventory(R”cdon_book_talentm.uti”, 99);

    objest oTalentw = GetObjectByTag(“cdon_book_talentw”);
    if (!IsObjectValid(oTalentw))
    UT-AddItemToInventory(R”cdon_book_talentw.uti”, 99);

    objest oHeal = GetObjectByTag(“cdon_pot_heal”);
    if (!IsObjectValid(oHeal))
    UT-AddItemToInventory(R”cdon_pot_heal.uti”, 99);

    objest oInj = GetObjectByTag(“cdon_pot_inj”);
    if (!IsObjectValid(oInj))
    UT-AddItemToInventory(R”cdon_pot_inj.uti”, 99);

    objest oMana = GetObjectByTag(“cdon_pot_mana”);
    if (!IsObjectValid(oMana))
    UT-AddItemToInventory(R”cdon_pot_mana.uti”, 99);
    break;
    }
    default;
    break;
    }
    }

    /——end script—–/

  6. John Vanderbeck
    November 21, 2009 at 11:11 pm

    Caladon :

    Hey John I had to start all over (completely remove\reinstall game\toolset\sql so I’m trying to redo your Tutorials. I tried this script and tried for multiple items but I keep getting and erroe when compiling — E: 22:55:40 – cdon_event_handler.nss – cdon_event_handler.nss(15): No semicolon after expression –

    Line 10 is my first object line and it has a semi colon at the end here is my script can you tell me what is wrong?

    /——–start script—-/

    #include “utility_h”
    #include “wrappers_h”
    #include “events_h”
    void main()
    {
    event ev = GetCurrentEvent();
    int nEventType = GetEventType(ev);
    Log_Events(“”, ev);
    switch (nEvent)
    {
    case EVENT_TYPE_MODULE_LOAD:
    {
    object oPlayer = GetHero();

    objest oAttrib = GetObjectByTag(“cdon_book_attrib”);

    The #1 cause of errors in programming is typos. “objest”? Try object 🙂

    • Caladon
      November 21, 2009 at 11:22 pm

      Now I feel dumb lol, that what I get for deciding to type it all out instead of copy\paste\replace””

      • Caladon
        November 21, 2009 at 11:24 pm

        Fixed object now getting this lol
        E: 23:22:50 – cdon_event_handler.nss – cdon_event_handler.nss(17): No semicolon after expression

      • Caladon
        November 21, 2009 at 11:25 pm

        NV used – instead of _ lol

  7. Lethal_Brahms
    November 21, 2009 at 11:33 pm

    This is my working multiple items script using IsObjectValid:

    // All module events
    #include “utility_h”
    #include “wrappers_h”
    #include “events_h”
    void main()
    {
    event ev = GetCurrentEvent();
    int nEvent = GetEventType(ev);
    Log_Events(“”, ev);
    switch (nEvent)
    {
    case EVENT_TYPE_MODULE_LOAD:
    {
    // get the object which contains the player
    object oPlayer = GetHero();

    object oArmor = GetObjectByTag(“my_custom_armor”);
    object oBoots = GetObjectByTag(“my_custom_boots”);
    object oGloves = GetObjectByTag(“my_custom_gloves”);
    object oHelm = GetObjectByTag(“my_custom_helm”);

    if (!IsObjectValid(oArmor))
    UT_AddItemToInventory(R”my_custom_armor.uti”,1);
    if (!IsObjectValid(oBoots))
    UT_AddItemToInventory(R”my_custom_boots.uti”,1);
    if (!IsObjectValid(oGloves))
    UT_AddItemToInventory(R”my_custom_gloves.uti”,1);
    if (!IsObjectValid(oHelm))
    UT_AddItemToInventory(R”my_custom_helm.uti”,1);
    break;
    }
    default:
    {
    break;
    }
    }
    }

    • John Vanderbeck
      November 21, 2009 at 11:35 pm

      Nicely done Lethal_Brahms

      • Lethal_Brahms
        November 21, 2009 at 11:49 pm

        Thanks

  8. Dark_Balor
    November 22, 2009 at 9:09 am

    Hi,
    i did something that could be interesting for newbies 🙂

    int addItemToPlayer(resource item,string tag)
    { 
    /*
    * Made by Dark_Balor with the code
    * provided by John Vanderbeck here
    * https://dragonagemodding.wordpress.com/2009/11/18/a-better-way-to-check-for-existing-items-before-spawning/
    */
        
         // get the object which contains the player
         object oPlayer = GetHero();
         // get the object of your item
         object oItem = GetObjectByTag(tag);
         // Verify if the item is already used by the player (inventory,sold,etc...)
         if (!IsObjectValid(oItem))
         { 
         //add the item to the player inventory (it's automaticaly take the oPlayer variable)
            UT_AddItemToInventory(item,1);
         //item added, return 0
            return 0;
         } 
         //item already used, return 1
         else
            return 1;
    }
    

    to be used like this :

    addItemToPlayer(R"ITEM_FILE_WITH_DOT_UTI","ITEM_TAG");
    

    exemple :

    addItemToPlayer(R"test.uti","test");
    
  9. diino
    November 23, 2009 at 3:57 am
    #include "utility_h"
    #include "wrappers_h"
    #include "events_h"
    
    void main()
    {
        event ev   = GetCurrentEvent();
        int nEvent = GetEventType(ev);
        Log_Events("", ev);
    
        switch (nEvent)
        {
            case EVENT_TYPE_MODULE_LOAD:
            {
                resource[] fileNames;
                fileNames[0] = R"item.uti";
                //just add another resource name to the uti
    
                int x = 0;
                while(x < GetArraySize(fileNames))
                {
                    int y = 0;
                    int stack = 1;
                    while(y < stack)
                    {
                        if(UT_CountItemInInventory(fileNames[x]) < stack)
                            UT_AddItemToInventory(fileNames[x], 1);
    
                        y++;
                    }
                    x++;
                }
                break;
            }
            default:
            {
                break;
            }
        }
    }
    

    Here's a script I made if you guys wanna add multiple items

  10. SyiSeiko
    November 24, 2009 at 6:59 am

    If your like me and you add more of an item you already have one of, like a ring or amulet. Instead of destroying or selling the copies you already have on your character or in your inventory. You can use this little function I made. For a function to work you need to place the header above void main() and the definition after. Then you can use the function as many times as you like within void main().

    #include "utility_h"
    #include "wrappers_h"
    #include "events_h"
    
    // Function Header
    void additem(resource rItem, int iCount, string iTag);
    // rItem is R"whatever.uti", iCount is how may you want, iTag is the tag of your item
    
    void main()
    {
        event ev   = GetCurrentEvent();
        int nEvent = GetEventType(ev);
        Log_Events("", ev);
        switch (nEvent)
        {
            case EVENT_TYPE_MODULE_LOAD:
            {
                // get the object which contains the player
                object oPlayer = GetHero();
    
                // Add items
                // Function utilization
                additem(R"my_ring.uti", 4, "my_ring");  // Don't forget the R
                additem(R"my_amulet.uti", 2, "my_amulet");
    
                break;
            }
            default:
            {
                break;
            }
        }
    }
    
    // Function Definition
    void additem(resource rItem, int iCount, string iTag)
    {
        /* Function made by SyiSeiko with some code
        ** provided by John Vanderbeck here:
        ** https://dragonagemodding.wordpress.com/2009/11/18/a-better-way-to-check-for-existing-items-before-spawning/#more-149
        ** and a little of my own coding skill
        */
    
        // Integers to count the number of item already held
        int i=0, count=0;
    
        // Count the number of your item already held
        // Checks for n items by the tag of your item
        // Count increments for each item already held
        for (i=0; i &lt; iCount; i++)
        {
            if(IsObjectValid(GetObjectByTag(iTag, i)))
                count++;
        }
    
        // If the number of items held is less than how many
        // you want, add the difference to the inventory
        object oWhat = GetObjectByTag(iTag);
            if (count &lt; iCount)
                UT_AddItemToInventory(rItem, iCount-count);
    }
    • September 2, 2011 at 10:41 pm

      // Function Definition
      void additem(resource rItem, int iCount, string iTag)
      {
      object oPlayer = GetHero();
      int iItemCount = CountItemsByTag(oPlayer, iTag);

      if(iItemCount < iCount)
      {
      int nItem = iCount-iItemCount;
      UT_AddItemToInventory(rItem, nItem);
      }
      }

  11. TaurosElan
    November 25, 2009 at 12:00 pm

    You know, you could condense your script a little further. There’s really no need to assign the result of GetObjectByTag() to a variable and then call IsObjectValid().

    GetObjectByTag() returns OBJECT_INVALID if no object is found. So you could condense your code to:

    if (GetObjectByTag("aga_bowofathena") == OBJECT_INVALID)
    • John Vanderbeck
      November 25, 2009 at 12:20 pm

      TaurosElan :

      You know, you could condense your script a little further. There’s really no need to assign the result of GetObjectByTag() to a variable and then call IsObjectValid().

      GetObjectByTag() returns OBJECT_INVALID if no object is found. So you could condense your code to:

      1 if (GetObjectByTag("aga_bowofathena") == OBJECT_INVALID)

      Checking against OBJECT_INVALID isn’t 100% and can give a false result. That’s why the function IsObjectValid() exists. Granted you could still condense it down:

      if (!IsObjectValid(GetObjectByTag("aga_bowofathena")))

      But in the end its a matter of style. I’ve just always tended to write more verbose code. Its easier to read in a few years when I go back to it.

  12. TaurosElan
    November 25, 2009 at 1:18 pm

    John Vanderbeck :

    TaurosElan :
    You know, you could condense your script a little further. There’s really no need to assign the result of GetObjectByTag() to a variable and then call IsObjectValid().
    GetObjectByTag() returns OBJECT_INVALID if no object is found. So you could condense your code to:

    view source

    print?

    1
    if (GetObjectByTag("aga_bowofathena") == OBJECT_INVALID)

    Checking against OBJECT_INVALID isn’t 100% and can give a false result. That’s why the function IsObjectValid() exists. Granted you could still condense it down:
    view sourceprint?1if (!IsObjectValid(GetObjectByTag("aga_bowofathena")))
    But in the end its a matter of style. I’ve just always tended to write more verbose code. Its easier to read in a few years when I go back to it.

    Not just a matter of style, but also an extra step by the script when it is executed. I always try to condense when writing code professionally, you never know when you need that extra cycle. But to each his or her own. 🙂

  13. gocek
    November 29, 2009 at 9:32 am

    Hi, I’m just wondering if the line “object oPlayer = GetHero();” is actually necessary with the new function. I mean: oPlayer variable is never used, right? Neither in GetObjectByTag(), nor in IsObjectValid(). It’s not even used by UT_AddItemToInventory().
    The code compiles ok without that line.

  14. John Vanderbeck
    November 29, 2009 at 9:35 am

    gocek :

    Hi, I’m just wondering if the line “object oPlayer = GetHero();” is actually necessary with the new function. I mean: oPlayer variable is never used, right? Neither in GetObjectByTag(), nor in IsObjectValid(). It’s not even used by UT_AddItemToInventory().
    The code compiles ok without that line.

    You are correct in this case, because we use the utility function UT_AddItemToInventory which by default goes to the player. I guess its just such a habit to get the player object 🙂

  15. Lantern
    December 1, 2009 at 12:34 pm
    #include “utility_h”
    #include “wrappers_h”
    #include “events_h”
    void main()
    {
    event ev = GetCurrentEvent();
    int nEvent = GetEventType(ev);
    Log_Events(“”, ev);
    switch (nEvent)
    {
    case EVENT_TYPE_MODULE_LOAD:
    {
    // get the object which contains the player
    object oPlayer = GetHero();
    
    object oArmor = GetObjectByTag(“mc_body”);
    object oBoots = GetObjectByTag(“mc_boots”);
    object oGloves = GetObjectByTag(“mc_arm”);
    object oHelm = GetObjectByTag(“mc_head”);
    
    if (!IsObjectValid(oArmor))
    UT_AddItemToInventory(R”mc_body.uti”,1);
    if (!IsObjectValid(oBoots))
    UT_AddItemToInventory(R”mc_boots.uti”,1);
    if (!IsObjectValid(oGloves))
    UT_AddItemToInventory(R”mc_arm.uti”,1);
    if (!IsObjectValid(oHelm))
    UT_AddItemToInventory(R”mc_head.uti”,1);
    break;
    }
    default:
    {
    break;
    }
    }
    }
    

    //error I’m receiving its pointing out in (#include “utility_h”)
    E: 01:08:47 – ma_script_mutipleitems.nss – ma_script_mutipleitems.nss(2): After compound statement at end

    • John Vanderbeck
      December 1, 2009 at 12:44 pm

      All “Compound Statement” errors occur because of bad quotes, usually caused by copying and pasting the code. Simply go through your code and re-type all the quotes as standard quotes.

  16. Lantern
    December 1, 2009 at 11:10 pm

    John Vanderbeck :
    All “Compound Statement” errors occur because of bad quotes, usually caused by copying and pasting the code. Simply go through your code and re-type all the quotes as standard quotes.

    Lolz… it took me all night figuring what went wrong … i re type the quotes and it works, i do not know much of scripting or so and i just simply follow your tutorial … thanks John .. God Bless

  17. Darren Hydrick
    December 8, 2009 at 8:21 pm

    Hey all i have been followin the tutorial and they are great. but i run into an issue. i am using the same script as Caladon. the issue is that three of the items in the list of five that i am spawning keep spawning even after they are in my back pack. i have went over the script with a fine tooth comb and cannot find out why they are still spawning. can yo help?

    here is my code:

    // All module events
    #include "utility_h"
    #include "wrappers_h"
    #include "events_h"
    void main()
    {
        event ev   = GetCurrentEvent();
        int nEvent = GetEventType(ev);
        Log_Events("", ev);
        switch (nEvent)
        {
    
            case EVENT_TYPE_MODULE_LOAD:
            {
    
                // get the object which contains the player
                object oPlayer = GetHero();
    
                object oShield = GetObjectByTag("dsh_dragonsetshield");
                if (!IsObjectValid(oShield))
                UT_AddItemToInventory(R"dsh_dragonsetshield.uti", 1);
    
                object oBoots = GetObjectByTag("dsh_dragonsetshoes");
                if (!IsObjectValid(oBoots))
                UT_AddItemToInventory(R"dsh_dragonsetshoes.uti", 1);
    
                object oSword = GetObjectByTag("dsh_dragonsetsword");
                if (!IsObjectValid(oSword))
                UT_AddItemToInventory(R"dsh_dragonsetsword.uti", 1);
    
                object oArmor = GetObjectByTag("dsh_dragonsetchest");
                if (!IsObjectValid(oArmor))
                UT_AddItemToInventory(R"dsh_dragonsetchest.uti", 1);
    
                object oGloves = GetObjectByTag("dsh_dragonsetgloves");
                if (!IsObjectValid(oGloves))
                UT_AddItemToInventory(R"dsh_dragonsetgloves.uti", 1);
    
                object oHelm = GetObjectByTag("dsh_dragonsethood");
                if (!IsObjectValid(oHelm))
                UT_AddItemToInventory(R"dsh_dragonsethood.uti", 1);
                break;
            }
            default:
            {
                break;
            }
        }
    }
  18. dkdodd
    December 12, 2009 at 1:39 pm

    okay, super noobish question
    does “object oSword” include 2-handers?

    • John Vanderbeck
      December 12, 2009 at 1:48 pm

      “oSword” is just a name we give the variable so we can reference it again. It could just as well be “oKitten” or “oHamburgerTastesGood”, it really is irrelevant to the code.

  19. Mattias71
    December 26, 2009 at 2:17 am

    I want to thank you for making these tutorials, they realy helped me out alot… since I’m new to the world of modding. but my question is… how do i tie these scripts into the original script that you made in your last tutorial without messing up the script an there for messing up the game!

  20. Mattias71
    December 26, 2009 at 2:27 am

    also does this script

    // All module events
    #include "utility_h"
    #include "wrappers_h"
    #include "events_h"
    void main()
    {
        event ev   = GetCurrentEvent();
        int nEvent = GetEventType(ev);
        Log_Events("", ev);
        switch (nEvent)
        {
            case EVENT_TYPE_MODULE_LOAD:
            {
                // get the object which contains the player
                object oPlayer = GetHero();
    
                object oBow = GetObjectByTag("aga_bowofathena");
                if (!IsObjectValid(oBow))
                    UT_AddItemToInventory(R"aga_bowofathena.uti",1);
                break;
            }
            default:
            {
                break;
            }
        }
    }
    

    do what this script does

    // All module events
    #include "utility_h"
    #include "wrappers_h"
    #include "events_h"
    void main()
    {
        event ev   = GetCurrentEvent();
        int nEvent = GetEventType(ev);
        Log_Events("", ev);
        switch (nEvent)
        {
            ////////////////////////////////////////////////////////////////////////
            // Sent by: The engine
            // When: The module loads from a save game, or for the first time. This event can fire more than
            //       once for a single module or game instance.
            ////////////////////////////////////////////////////////////////////////
            case EVENT_TYPE_MODULE_LOAD:
            {                           
                // this event can fire multiple times, and we only want to do this once
                // So we save a flag inside a number variable.  We check it here and if its
                // set already then we know we've already done this before.  The name of the
                // variable in quotes can be whatever you want.
                int iModuleLoaded = GetLocalInt(OBJECT_SELF, "aga_bowathena");
                if (iModuleLoaded == 1)
                    break;
    
                // get the object which contains the player
                object oPlayer = GetHero();
    
                // The flag we save won't persist beyond the game session, so its a good idea to actually
                // look to see if the player already has the item, unless you WANT them to get another one
                // The name in quotes is the TAG of the item.  So what we do is see how many of the item
                // the player already has, and if its 0, we know we haven't given it to them yet (or they sold it :p)
                int iItemCount = CountItemsByTag(oPlayer, "aga_bowofathena");                                       
                // if its 0, then let's give them the item.  Now this first parameter is a RESOURCE identifier.  Note
                // how its formatted.  R"resource_file_name"
                // The "R" part is important and identifies it as a resource.  The filename is just the filename of the
                // resource.  You can see this in the toolset on the tabs up top.
                // The number 1 means give 1 item.
                if (iItemCount  == 0)
                    UT_AddItemToInventory(R"aga_bowofathena.uti",1);
    
                // lastly we set that number variable we talked about earlier, to save the fact that we've
                // done this already.
                SetLocalInt(OBJECT_SELF, "aga_bowathena", 1);
                break;
            }
            default:
            {
                break;
            }
        }
    }
    

    in your tutorial about creating items on toolsets?… or is there a way to add this script into the other original one you wrote

    • John Vanderbeck
      December 27, 2009 at 12:56 am

      Pretty much, yes

  21. INFerno--
    December 26, 2009 at 3:35 pm

    with this method, the object will be added each time, ie it can not be removed from the inventory, it is not good, better to use a test plot. We add a plot and named my_item_check_plt, add the Main Flag, and rename FLAG_0 in MY_ITEM_CHECK_FLAG, and use this script

    #include “utility_h”
    #include “plt_my_item_check_plt” // plt_ + name created plot

    void main()
    {
    //Check if the flag is set to TRUE, exit
    //PLT_ + name created plot (MY_ITEM_CHECK_PLT)
    if (WR_GetPlotFlag(PLT_MY_ITEM_CHECK_PLT, MY_ITEM_CHECK_FLAG) == TRUE)
    return;

    event ev = GetCurrentEvent();
    int nEventType = GetEventType(ev);

    switch (nEventType)
    {
    case EVENT_TYPE_MODULE_LOAD:
    {
    UT_AddItemToInventory(R”my_ring001.uti”, 1);

    // Set the flag that the item is added to the inventory
    WR_SetPlotFlag(PLT_MY_ITEM_CHECK_PLT, MY_ITEM_CHECK_FLAG, TRUE);

    break;
    }
    default:
    break;
    }
    }

    Now if we remove the item it will not be added again.
    PS Sorry for my English 🙂

    • John Vanderbeck
      December 27, 2009 at 12:55 am

      Plots were intentionally not used due to bugs in the toolset. Now that v1.01 of the toolset is out, and a lot of those bugs fixed, it is probably time to take another look at this.

    • John Vanderbeck
      December 28, 2009 at 12:43 pm

      The compound statement error means your quote marks are invalid. This is often caused by copying & pasting the script. Retype the quote marks.

  22. Stuart
    December 28, 2009 at 9:43 am

    Cheers for the guides.

    Now the problem i run into is that when i Write up the code from the multiple objects script provided by Lethal_brah and replace the items i had created with ones similar to the ones provided it comes up with the ” E: 01:35:49 – test.nss – test.nss(1): After compound statement at end ” error and doesn’t complile. having read the previous posts and seeing that the comments is where the issue should be. i decided hey why not just delete the comments completly leaving my code like so

    #include “utility_h”
    #include “wrappers_h”
    #include “events_h”
    void main()
    {
        event ev = GetCurrentEvent();
        int nEvent = GetEventType(ev);
        Log_Events(“”, ev);
        switch (nEvent)
        {
            case EVENT_TYPE_MODULE_LOAD:
            {
                object oPlayer = GetHero();
    
                object oArmor = GetObjectByTag(“my_custom_armor”);
                object oBoots = GetObjectByTag(“my_custom_boots”);
                object oGloves = GetObjectByTag(“my_custom_gloves”);
                object oBelt = GetObjectByTag(“my_custom_belt”);
                object oDagger1 = GetObjectByTag(“my_custom_dagger1”);
                object oDagger2 = GetObjectByTag(“my_custom_dagger2”);
    
                if (!IsObjectValid(oArmor))
                UT_AddItemToInventory(R”my_custom_armor.uti”,2);
                if (!IsObjectValid(oBoots))
                UT_AddItemToInventory(R”my_custom_boots.uti”,2);
                if (!IsObjectValid(oGloves))
                UT_AddItemToInventory(R”my_custom_gloves.uti”,2);
                if (!IsObjectValid(oBelt))
                UT_AddItemToInventory(R”my_custom_belt.uti”,4);
                if (!IsObjectValid(odagger1))
                UT_AddItemToInventory(R”my_custom_dagger1.uti”,2);
                if (!IsObjectValid(odagger2))
                UT_AddItemToInventory(R”my_custom_dagger2.uti”,2);
                break;
            }
            default:
            {
                break;
            }
        }
    }
    

    but when i try and re-complie said script, i continue to get the same error. now either something really simple is wrong that i’m missing or i need to look at re-writing the code without copy-paste.

  23. BowHunter
    December 28, 2009 at 9:57 pm

    i keep getting this error

    S: 21:51:46 – Export Summary, 1 Requested, 0 Success, 0 Skipped, 1 Failed. Please see below for more details.
    I: 21:51:46 – Offers.xml file has been updated successfully.
    I: 21:51:46 – AddIns.xml file has been updated successfully.
    I: 21:51:45 – Generated the campaign file.
    W: 21:51:45 – No starting area specified for campaign.
    I: 21:51:44 – PostProcessing
    E: 21:51:44 – dk_sword_script.nsc – Exporter FAILED for resource: dk_sword_script.nsc
    E: 21:51:44 – dk_sword_script.nsc – Unable to get the resource information for “utility_h” of type “nsc”
    I: 21:51:43 – Shared Addin resources will be exported to “C:\Documents and Settings\XXXXXXX\My Documents\BioWare\Dragon Age\addins\bh_dragonkiller\core\override\toolsetexport\”.
    I: 21:51:43 – Campaign resources will be exported to “C:\Documents and Settings\XXXXXXX\My Documents\BioWare\Dragon Age\addins\bh_dragonkiller\module\override\toolsetexport\”.
    I: 21:51:43 – Campaign resources will be exported to “C:\Documents and Settings\XXXXXXX\My Documents\BioWare\Dragon Age\modules\Single Player\override\toolsetexport\”.
    I: 21:51:43 – Shared resources will be exported to “C:\Documents and Settings\XXXXXXX\My Documents\BioWare\Dragon Age\packages\core\override\toolsetexport\”.
    S: 21:51:42 – Starting export: “Export without dependent resources”

    and this is the script i used // All module events
    #include "utility_h"
    #include "wrappers_h"
    #include "events_h"
    void main()
    {
    event ev = GetCurrentEvent();
    int nEvent = GetEventType(ev);
    Log_Events("", ev);
    switch (nEvent)
    {
    ////////////////////////////////////////////////////////////////////////
    // Sent by: The engine
    // When: The module loads from a save game, or for the first time. This event can fire more than
    // once for a single module or game instance.
    ////////////////////////////////////////////////////////////////////////
    case EVENT_TYPE_MODULE_LOAD:
    {
    // this event can fire multiple times, and we only want to do this once
    // So we save a flag inside a number variable. We check it here and if its
    // set already then we know we've already done this before. The name of the
    // variable in quotes can be whatever you want.
    int iModuleLoaded = GetLocalInt(OBJECT_SELF, "dksword");
    if (iModuleLoaded == 1)
    break;

    // get the object which contains the player
    object oPlayer = GetHero();

    // The flag we save won't persist beyond the game session, so its a good idea to actually
    // look to see if the player already has the item, unless you WANT them to get another one
    // The name in quotes is the TAG of the item. So what we do is see how many of the item
    // the player already has, and if its 0, we know we haven't given it to them yet (or they sold it :p)
    int iItemCount = CountItemsByTag(oPlayer, "dksword");
    // if its 0, then let's give them the item. Now this first parameter is a RESOURCE identifier. Note
    // how its formatted. R"resource_file_name"
    // The "R" part is important and identifies it as a resource. The filename is just the filename of the
    // resource. You can see this in the toolset on the tabs up top.
    // The number 1 means give 1 item.
    if (iItemCount == 0)
    UT_AddItemToInventory(R"dksword.uti",1);

    // lastly we set that number variable we talked about earlier, to save the fact that we've
    // done this already.
    SetLocalInt(OBJECT_SELF, "dksword", 1);
    break;
    }
    default:
    {
    break;
    }
    }
    }

    what am i doing wrong

  24. Gotcha!
    January 10, 2010 at 11:29 am

    Hello,

    Could someone please have a look at this script and see what is wrong?
    I get no errors in-game, but no items are added to my inventory upon load.

    // All module events
    #include "utility_h"
    #include "wrappers_h"
    #include "events_h"
    void main()
    {
        event ev = GetCurrentEvent();
        int nEvent = GetEventType(ev);
        Log_Events("", ev);
        switch (nEvent)
        {
            case EVENT_TYPE_MODULE_LOAD:
            {
    // get the object which contains the player
                object oPlayer = GetHero();
    
                object oMordainCleaver = GetObjectByTag("gotcha_mordaincleaver");
                object oPainRing = GetObjectByTag("gotcha_painring");
                object oShamanRobes = GetObjectByTag("gotcha_shamanrobes");
                object oBlackStaff = GetObjectByTag("gotcha_theblackstaff");
    
                if (!IsObjectValid(oMordainCleaver))
                UT_AddItemToInventory(R"gotcha_mordaincleaver.uti",1);
                if (!IsObjectValid(oPainRing))
                UT_AddItemToInventory(R"gotcha_painring.uti",1);
                if (!IsObjectValid(oShamanRobes))
                UT_AddItemToInventory(R"gotcha_shamanrobes.uti",1);
                if (!IsObjectValid(oBlackStaff))
                UT_AddItemToInventory(R"gotcha_theblackstaff.uti",1);
                break;
            }
            default:
            {
                break;
            }
        }
    }
    
    • Gotcha!
      January 10, 2010 at 2:41 pm

      Or maybe I should actually attach this script to the module first. >_< Forgot about that. Thanks, all, it works now! 😀

  25. Steven Homen
    January 14, 2010 at 1:30 pm

    😦

    Well, I just can’t get the script to work for me. I don’t know why it won’t work, I just can’t find the item in my inventory. Darn it, was hoping to make an uber staff. 😛

    Tried copying and pasting your script (while just changing the name since my is demonslayer not bowofathena), but no luck. 😦

    Wait, fit into the original script? that final part goes into the original script? I’m quite confused. I tried copy and pasting the original script and it didnt work then uninstalled and the mod and re did the process only to find out the one on this page didn’t work either. grr! It makes me really appreciate the modding tool for Fallout 3. The GECK is a breeze to use compared to this. >: /

  26. BlessedEternal
    January 16, 2010 at 2:02 pm

    Frustrations with the Toolset

    I’ve tried multiple peoples’ “Working Scripts” and I can’t get any working for the items I’m trying to import. At the moment I’m working on your (John’s) step-by-step for creating a custom vendor, figuring that if I can’t get the item to spawn, I’ll try to buy it instead. Here’s my current script, I get no errors except the starting area warning, and all exports are listing ‘successful’. Am I exporting wrong?

    // All module events
    #include “utility_h”
    #include “wrappers_h”
    #include “events_h”
    void main()
    {
    event ev = GetCurrentEvent();
    int nEvent = GetEventType(ev);
    Log_Events(“”, ev);
    switch (nEvent)
    {
    case EVENT_TYPE_MODULE_LOAD:
    {
    // get the object which contains the player
    object oPlayer = GetHero();

    object oKal_Armor = GetObjectByTag(“kal_armor”);
    object oKal_Gloves = GetObjectByTag(“kal_gloves”);
    object oKal_Lel_bow = GetObjectByTag(“kal_lel_bow”);

    if (!IsObjectValid(oKal_Armor))
    UT_AddItemToInventory(R”kal_armor.uti”,1);
    if (!IsObjectValid(oKal_Gloves))
    UT_AddItemToInventory(R”kal_gloves.uti”,1);
    if (!IsObjectValid(oKal_Lel_bow))
    UT_AddItemToInventory(R”kal_lel_bow.uti”,1);

    break;
    }
    default:
    {
    break;
    }
    }
    }

    Obvious items, 1pc of Armor, 1 pair of gloves, and a longbow. Not getting any to spawn. While I wait for a response I’m going to clean out the mod folder and try another export from scratch… been editing and exporting for the last 2 1/2 hours to try and get it to work…. lol

  27. Tue
    January 31, 2010 at 6:11 pm

    I’ve tried using your guide but I’m unable to make my item appear in the game. I’ve created a ring – copying most of the stats including inventory subgroup, material type etc. from the blood ring – but it doesn’t show up. I have removed the blood mage requirement but I don’t know if the “Improves blood magic” is the cause of it not showing up.

    This is my script:

    // All module events
    #include “utility_h”
    #include “wrappers_h”
    #include “events_h”
    void main()
    {
    event ev = GetCurrentEvent();
    int nEvent = GetEventType(ev);
    Log_Events(“”, ev);
    switch (nEvent)
    {
    case EVENT_TYPE_MODULE_LOAD:
    {
    // get the object which contains the player
    object oPlayer = GetHero();

    object oRing = GetObjectByTag(“tun_angantyr”);
    if (!IsObjectValid(oRing))
    UT_AddItemToInventory(R”tun_angantyr.uti”,1);
    break;
    }
    default:
    {
    break;
    }
    }
    }

    Any help is appreciated

  28. Tue
    January 31, 2010 at 6:12 pm

    // All module events
    #include “utility_h”
    #include “wrappers_h”
    #include “events_h”
    void main()
    {
    event ev = GetCurrentEvent();
    int nEvent = GetEventType(ev);
    Log_Events(“”, ev);
    switch (nEvent)
    {
    case EVENT_TYPE_MODULE_LOAD:
    {
    // get the object which contains the player
    object oPlayer = GetHero();

    object oRing = GetObjectByTag(“tun_angantyr”);
    if (!IsObjectValid(oRing))
    UT_AddItemToInventory(R”tun_angantyr.uti”,1);
    break;
    }
    default:
    {
    break;
    }
    }
    }

  29. March 18, 2010 at 12:27 pm

    My addin only seems to work once, after that it does not work for a new game. It seems the plot flag is being set and stays that way.
    // —- SCRIPT STARTS HERE —-

    // Later on in this script, we will use the UT_AddItemToInventory()
    // function to add an item to the player’s inventory.
    // But before we could use it, we have to tell the toolset where
    // to look for it. The function is in the “utility_h” script file
    // under _Core Includes, we will include this file at the top of
    // our script file, above the main function.

    #include “utility_h”
    #include “wrappers_h”
    #include “plt_makers_set_plot”

    void main()
    {
    // If our plot flag is set to TRUE, that means we have already
    // given the items to the player, there is no need to continue
    // running this script.
    if ( WR_GetPlotFlag( PLT_MAKERS_SET_PLOT, MAKERS_SET_CHECK_FLAG ) == TRUE )
    return;

    event ev = GetCurrentEvent();
    int nEventType = GetEventType(ev);

    // We will watch for every event type and if the one we need
    // appears we will handle it as a special case. We will ignore the rest
    // of the events
    switch ( nEventType )
    {
    // This event happenes every time the module loads
    // This usually happenes when creating a new game
    // or loading a savegame
    case EVENT_TYPE_MODULE_LOAD:
    {
    // The UT_AddItemToInventory function adds various resources to a
    // creature’s inventory. Here we add one weapon and one shield.
    UT_AddItemToInventory(R”makers_hat.uti”, 1);
    UT_AddItemToInventory(R”makers_light_armor.uti”, 1);
    UT_AddItemToInventory(R”makers_light_hat.uti”, 1);
    UT_AddItemToInventory(R”makers_light_slippers.uti”, 1);
    UT_AddItemToInventory(R”makers_longsword.uti”, 1);
    UT_AddItemToInventory(R”makers_med_armor.uti”, 1);
    UT_AddItemToInventory(R”makers_shield.uti”, 1);
    UT_AddItemToInventory(R”makers_slippers.uti”, 1);
    UT_AddItemToInventory(R”makers_sting.uti”, 2);
    UT_AddItemToInventory(R”makers_wand.uti”, 1);
    UT_AddItemToInventory(R”makers_med_touch.uti”, 1);
    UT_AddItemToInventory(R”makers_light_touch.uti”, 1);

    // Set our plot flag to TRUE, so the next time this script tries
    // to run it will not add extra items to the player’s inventory
    WR_SetPlotFlag( PLT_MAKERS_SET_PLOT, MAKERS_SET_CHECK_FLAG, TRUE );

    // We have dealt with the event we were waiting for.
    // At this point we can stop looking for other events
    break;
    }
    default:
    break;
    }
    }
    // —- SCRIPT ENDS HERE —-

  30. Vilgoo
    April 4, 2010 at 4:37 am

    I have a problem to solve.
    I created the item and works well, but when I load the game again, the item is equipped but also is in the bag, as it multiplies.
    Maybe the script, but not understand the reason for which this happens.
    Could someone help me

  31. Gavin
    August 2, 2010 at 8:25 am

    / All module events
    #include “utility_h”
    #include “wrappers_h”
    #include “events_h”
    void main()
    {
    event ev = GetCurrentEvent();
    int nEvent = GetEventType(ev);
    Log_Events(“”, ev);
    switch (nEvent)
    {
    case EVENT_TYPE_MODULE_LOAD:
    {
    // get the object which contains the player
    object oPlayer = GetHero();

    object oArmor = GetObjectByTag(“gvn_arcarm_armor”);
    object oBoots = GetObjectByTag(“gvn_arcarm_boots”);
    object oGloves = GetObjectByTag(“gvn_arcarm_glove”);
    object oHelm = GetObjectByTag(“gvn_arcarm_helm”);
    object oSword = GetObjectByTag(“gvn_arcarm_wep”);
    object oDagger = GetObjectByTag(“gvn_arcarm_wep2″);

    if (!IsObjectValid(oArmor))
    UT_AddItemToInventory(R”gvn_arcarm_armor.uti”,1);
    if (!IsObjectValid(oBoots))
    UT_AddItemToInventory(R”gvn_arcarm_boots.uti”,1);
    if (!IsObjectValid(oGloves))
    UT_AddItemToInventory(R”gvn_arcarm_glove.uti”,1);
    if (!IsObjectValid(oHelm))
    UT_AddItemToInventory(R”gvn_arcarm_helm.uti”,1);
    if (!IsObjectValid(oHelm))
    UT_AddItemToInventory(R”gvn_arcarm_wep.uti”,1);
    if (!IsObjectValid(oHelm))
    UT_AddItemToInventory(R”gvn_arcarm_wep2.uti”,1);
    break;
    }
    default:
    {
    break;
    }
    }
    }

    I cant seem to get this to work, can any1 help me?
    PS: i have 0 scripting experience. this is purely copy paste and trial and error.

  32. Gavin
    August 2, 2010 at 8:28 am

    Oh yes and I have already changed the 2 xtra Helm in if (!IsObjectValid(oHelm))
    to Sword and dagger to correspond to the object o(thing).
    But it still gives me unknown state in compiller.

  33. Gavin
    August 2, 2010 at 8:44 am

    Sorry for the mass post, but i have figured out the Unknown state in compiler problem.
    *But after sucessfully exporting it, the items, and generating the 2 files, the items wont appear in game*

    *I have also solved the problem of it, for any1 that is interested heres how.
    Remove the mod you have added(I use DAmodder,so its easy for me). Now go in game and destroy any item related to the mod you just removed( sometimes incomplete removal of the mod occurs, such as an item still in a shop which the DAmoddder failed to remove). Now find all the items related to the mod, and destroy them(if their in a shop, buy it, den right click the item, destroy)

    Hope this little thing helps!
    And thanks to whoever wrote the script I used!

  1. November 22, 2009 at 10:42 am

Leave a reply to Gaviteros Cancel reply