Creation script


#1

Trying to make a generic script for placables where if x items are in the container on close it destroys those objects and has y object in container. Lilac soul Blacksmith scripts dont seem to work.

Using GetNumItems in script and think its falling down - Any help would be appreciated.


void RemoveAllItems(object oCreature, string sTag="");
void RemoveAllItems(object oCreature, string sTag="")
{
object oItem = GetFirstItemInInventory(oCreature);
while ( oItem != OBJECT_INVALID )
{
if ( “” == sTag || GetTag(oItem) == sTag )
DestroyObject(oItem);
oItem = GetNextItemInInventory(oCreature);
}
}

void main()
{
object oSelf = OBJECT_SELF;
object oMade = “potion001”;
string sItem = “marrow”;
int iCount = 10;
int imarrowcount = (GetNumItems(oSelf,sItem));

If (imarrowcount <iCount)
return;

RemoveAllItems(oSelf, "marrow");
CreateItemOnObject(oSelf,oMade);

}


#2

Hi John_Black,
There are a couple of issues with that script. It won’t compile as is much less do what you want.

You need to #include “nw_i0_plot” at the top

oMade is assigned a string so it probable should be string oMade = “potion001” (or sMade to be consistent).

You probably want to remove the extra parens around the call to GetNumItems()

“If” should be “if” (lower case eye).

You need to switch the arguments in CreateItemOnObject since it wants the resref string first then the target object.

Keep at it. You’ll get it :slight_smile:


#3

Well it compiles now :slight_smile:
Appreciate the input - very rusty after 5 years or so.
Thanks again for taking the time.


#4

Compiling, of course, is good, but is only part of it … it also has to do what you want :wink:


#5

Nope - not yet -
was supposed to be simple put x amount of something in get 1 thing back.
any indication on where its falling down?

#include “nw_i0_plot”
// remove all items from lilac soul
void RemoveAllItems(object oCreature, string sTag="");
void RemoveAllItems(object oCreature, string sTag="")
{
object oItem = GetFirstItemInInventory(oCreature);
while ( oItem != OBJECT_INVALID )
{
if ( “” == sTag || GetTag(oItem) == sTag )
DestroyObject(oItem);
oItem = GetNextItemInInventory(oCreature);
}
}

void main()
{
object oSelf = OBJECT_SELF; // container
string sMade = “potion001”; // thing to make
string sItem = “marrow”; // thing to make from
int iCount = 10; // how many to make from
int imarrowcount = GetNumItems(oSelf,sItem); // get how many in container

if (imarrowcount <iCount)    // if not enough return - I think this is where its breaking still
return;

RemoveAllItems(oSelf, "marrow"); // if enough destroy ingredients
CreateItemOnObject(sMade,oSelf);   // produce final item

}


#6

Try this:

#include "nw_i0_plot"

//
void RemoveNumItems(object oTarget, string sItemTag, int nNumItems);

//** MAIN ** //
void main()
{
	object oSelf = OBJECT_SELF; // container
	string sTake = "marrow";	// tag of the thing to make from
	string sGive = "potion001";	// resref of the thing to make
	int nNumTake = 10;				// how many to make from
	int nNumGive = 1;				// how many to make
	int nCurrent = GetNumItems(oSelf, sTake); // get how many in container

	while (nCurrent >= nNumTake)
	{
		RemoveNumItems(oSelf, sTake, nNumTake);
		CreateItemOnObject(sGive, oSelf, nNumGive);
		nCurrent -= nNumTake;
	}
}

//
void RemoveNumItems(object oTarget, string sItemTag, int nNumItems)
{
	int nCount, nStack;
	object oItem = GetFirstItemInInventory(oTarget);
	while (GetIsObjectValid(oItem) && nCount < nNumItems)
	{
		if (GetTag(oItem) == sItemTag)
		{
			nStack = GetItemStackSize(oItem);

			if ((nNumItems - nCount) >= nStack)
			{
				DestroyObject(oItem, 0.2f);
				nCount += nStack;
			}
			else
			{
				SetItemStackSize(oItem, nStack - nNumItems + nCount);
				nCount += nNumItems - nCount;
			}
		}
		oItem = GetNextItemInInventory(oTarget);
	}
}

Make sure the tag of the item to take is actually marrow, not MARROW or something else.


#7

Tried the above script - was getting a container full of potions on close, even without marrow. Checked tags etc all correct (I try to keep name resref and tag the same if I can).

Looked at the script and changed what I thought was causing the issue but now back to not doing anyything again :frowning:

#include “nw_i0_plot”

//
void RemoveNumItems(object oTarget, string sItemTag, int nNumItems);

//** MAIN ** //
void main()
{
object oSelf = OBJECT_SELF; // container
string sTake = “marrow”; // tag of the thing to make from
string sGive = “it_mpotion002”; // resref of the thing to make
int nNumTake = 10; // how many to make from
int nNumGive = 1; // how many to make
int nCurrent = GetNumItems(oSelf, sTake); // get how many in container

if (nCurrent < nNumTake) // not enough items - return
return;

while (nCurrent >= nNumTake)    // more or equal ingredient to make item
{
    RemoveNumItems(oSelf, sTake, nNumTake);    // remove how many needed
    nCurrent -= nNumTake;
}
CreateItemOnObject(sGive, oSelf, nNumGive); // give Item

}

//
void RemoveNumItems(object oTarget, string sItemTag, int nNumItems)
{
int nCount, nStack;
object oItem = GetFirstItemInInventory(oTarget);
while (GetIsObjectValid(oItem) && nCount < nNumItems)
{
if (GetTag(oItem) == sItemTag)
{
nStack = GetItemStackSize(oItem);

        if (nStack <= nNumItems)
        {
            DestroyObject(oItem, 0.2f);
            nCount += nStack;
        }
        else
        {
            SetItemStackSize(oItem, nStack - nNumItems);
            nCount += nNumItems;
        }
    }
    oItem = GetNextItemInInventory(oTarget);
}

}


#8

Sorry, my bad. I corrected the script above, try again.


#9

Just getting the marrow back on close - Ill see if can go through it this evening -
Appreciating all the help.


#10

Changed the Container and presto it worked.
Thanks very much for all the help, Really appreciate it.
:slight_smile:


#11

You’re welcome. If you have any other questions, feel free to ask them :slightly_smiling_face:

UPD: Fixed some error in the RemoveNumItems() function in my script above. The check inside the loop for number of items left was wrong.


#12

So Ive expanded this into a generic Crafting Table - Which almost works :):roll_eyes:
Its called from a conversation on crafting bench which lists recipes. When you pick the recipe response the script fires. The void main sets the Vars to the table for the particular recipe, and my attempt at functions then runs the combine as per the last script. It tells me if I dont have enough of the first ingrdient but then goes dead somewhere - Any insights?

#include “nw_i0_plot”

void RemoveNumItems(object oTarget, string sItemTag, int nNumItems);
void CombineComponents (object oPC);
//** MAIN ** //

void CombineComponents (object oPC)
{
object oSelf = OBJECT_SELF; // container
string sComponent1 = GetLocalString (OBJECT_SELF,“component1”); // tag of the 1st thing to make from
string sComponent2 = GetLocalString (OBJECT_SELF,“component2”); // tag of the 2nd thing to make from
string sComponent3 = GetLocalString (OBJECT_SELF,“component3”); // tag of the 3rd thing to make from
string sGive = GetLocalString (OBJECT_SELF, “product”); // resref of the thing to make
int iComponent1 = GetLocalInt (OBJECT_SELF,“component1num”); // how many to make from
int iComponent2 = GetLocalInt (OBJECT_SELF,“component2num”); // how many to make from
int iComponent3 = GetLocalInt (OBJECT_SELF,“component3num”); // how many to make from
int iNumGive = GetLocalInt (OBJECT_SELF,“productnum”); // how many to make
int iCurrent1 = GetNumItems(oPC, sComponent1); // get how many on PC
int iCurrent2 = GetNumItems(oPC, sComponent2); // get how many on PC
int iCurrent3 = GetNumItems(oPC, sComponent3); // get how many on PC
int iDifficulty = GetLocalInt (OBJECT_SELF, “difficulty”); // recipe difficulty
string sTradeskill = GetLocalString (OBJECT_SELF,“tradeskill”); // tradeskill attempting
int iSkill = GetCampaignInt (“trades”,sTradeskill,oPC); //current tradeskill of player
// check they have enough
if (iCurrent1<iComponent1)
FloatingTextStringOnCreature(“You do not have enough “+sComponent1+” to do this.”,oPC,FALSE);
return;
if (iCurrent2<iComponent2)
FloatingTextStringOnCreature(“You do not have enough “+sComponent2+” to do this.”,oPC,FALSE);
return;
if (iCurrent3<iComponent3)
FloatingTextStringOnCreature(“You do not have enough “+sComponent3+” to do this.”,oPC,FALSE);
return;
//OK they have enough
// play animation
// Have the PC perform a sequence of actions.
AssignCommand(oPC, ActionPlayAnimation(ANIMATION_LOOPING_GET_MID, 1.0, 6.0));
//Check Skill passed
int iRan = Random(1000); //roll random1-1000
int iChance = (iSkill+iDifficulty); // base skill plus difficulty
int ibadfailchance = (iChance+iChance);

// process materials for Bad Fail
if (iRan>ibadfailchance)
{
    RemoveNumItems(oPC, sComponent1, iComponent1);
    RemoveNumItems(oPC, sComponent2, iComponent2);
    RemoveNumItems(oPC, sComponent3, iComponent3);
    FloatingTextStringOnCreature("You Mangled the Components",oPC,FALSE);
    return;
 }
 // Processmaterials for fail but include skill up chance
  if (iRan>iChance)
   {
    RemoveNumItems(oPC, sComponent1, iComponent1);
    RemoveNumItems(oPC, sComponent2, iComponent2);
    RemoveNumItems(oPC, sComponent3, iComponent3);
    FloatingTextStringOnCreature("You Failed to create anything",oPC,FALSE);
    // check for skillup
    int iSkillup = Random (100);

     if (iSkillup<10)
    {
     iSkill =(iSkill+1);
    string sSkillpoints = IntToString(iSkill);
    FloatingTextStringOnCreature("You Managed to raise your skill to "+sSkillpoints,oPC,FALSE);
    SetCampaignInt ("trades",sTradeskill,iSkill,oPC);
    return;
   }
 }

    // good to go for success
    RemoveNumItems(oPC, sComponent1, iComponent1);
    RemoveNumItems(oPC, sComponent2, iComponent2);
    RemoveNumItems(oPC, sComponent3, iComponent3);
    FloatingTextStringOnCreature("You Have Succeeded",oPC,FALSE);
    CreateItemOnObject(sGive, oPC, iNumGive);
    // check for skillup
    int iSkillup = Random (100);

     if (iSkillup<20)
    {
     iSkill =(iSkill+1);
    string sSkillpoints = IntToString(iSkill);
    FloatingTextStringOnCreature("You Managed to raise your skill to "+sSkillpoints,oPC,FALSE);
    SetCampaignInt ("trades",sTradeskill,iSkill,oPC);
    return;
    }

}
//
void RemoveNumItems(object oTarget, string sItemTag, int nNumItems)
{
int nCount, nStack;
object oItem = GetFirstItemInInventory(oTarget);
while (GetIsObjectValid(oItem) && nCount < nNumItems)
{
if (GetTag(oItem) == sItemTag)
{
nStack = GetItemStackSize(oItem);

		if ((nNumItems - nCount) >= nStack)
		{
			DestroyObject(oItem, 0.2f);
			nCount += nStack;
		}
		else
		{
			SetItemStackSize(oItem, nStack - nNumItems + nCount);
			nCount += nNumItems - nCount;
		}
	}
	oItem = GetNextItemInInventory(oTarget);
}

}

void main()
{
object oPC = GetPCSpeaker(); //change depending on container/conversation etc

SetLocalString (OBJECT_SELF,"component1","SEED_GRAPE2");    // tag of the 1st thing to make from
SetLocalString (OBJECT_SELF,"component2","ITEM_GRAPEFRUIT");    // tag of the 2nd thing to make from
SetLocalString (OBJECT_SELF,"component3","");    // tag of the 3rd thing to make from
SetLocalString (OBJECT_SELF, "product","item_juice_001"); // resref of the thing to make
SetLocalInt (OBJECT_SELF,"component1num",1);       // how many to make from
SetLocalInt (OBJECT_SELF,"component2num",1);       // how many to make from
SetLocalInt (OBJECT_SELF,"component3num",0);       // how many to make from
SetLocalInt (OBJECT_SELF,"productnum",3);               // how many to make
SetLocalInt (OBJECT_SELF, "difficulty",500); // recipe difficulty
SetLocalString (OBJECT_SELF,"tradeskill","Brewing"); // tradeskill attempting
CombineComponents (oPC);

}


#13

Never mind, Got it Working. Thanks again for all the help