Jump to content

Home

DestroyObject Function: How would I destroy 1 item and not all.


T7nowhere

Recommended Posts

What i'm trying to do is make my script destroy an item by tag without it destroying all the items inthe item stack.

 

This is the script that i wrote:

 

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

if (GetIsObjectValid(GetItemPossessedBy (oPC, "g_w_shortsbr01")))
{
	TRUE;
	DestroyObject(GetObjectByTag("g_w_shortsbr01",));{

	return;
	}
}

else if (GetIsObjectValid(GetItemPossessedBy (oPC, "g_w_lghtsbr01")))
{
	TRUE;
	DestroyObject(GetObjectByTag("g_w_lghtsbr01",));{
		return;
	}
}

}

 

It does do what I want it to except it will destroy all the items with that item tag and I only want it to destroy 1.

 

 

EDIT: Well I have been able to make it work the way I want it to except I feel there should be a more efficient way of doing it. Here is my new script.

 

 void main()
{
object oPC = GetFirstPC();
object oOlds = GetItemPossessedBy (GetFirstPC(),"g_w_shortsbr01");
object oOlds1 = GetItemPossessedBy (GetFirstPC(),"g_w_lghtsbr01");
int nQuantity = GetScriptParameter( 1 );


if(nQuantity == 0) nQuantity = 1;

if (GetIsObjectValid(GetItemPossessedBy (oPC, "g_w_shortsbr01")))
{
	TRUE;
int nStackSize = GetItemStackSize(oOlds);
       if(nQuantity < nStackSize)
       {
           nQuantity = nStackSize - nQuantity;
           SetItemStackSize(oOlds, nQuantity);
       }
	DestroyObject(GetObjectByTag("g_w_shortsbr01",nQuantity));{

	return;
	}
}

else if (GetIsObjectValid(GetItemPossessedBy (oPC, "g_w_lghtsbr01")))
{
	TRUE;
int nStackSize = GetItemStackSize(oOlds1);
       if(nQuantity < nStackSize)
       {
           nQuantity = nStackSize - nQuantity;
           SetItemStackSize(oOlds1, nQuantity);
       }
	DestroyObject(GetObjectByTag("g_w_lghtsbr01",nQuantity));{
		return;
	}
}

} 

Link to comment
Share on other sites

Hello T7,

You're on the right track with Stack Size.

 

Try this:

void main () {
object oPC=GetFirstPC();
int nStack;
object oShortSbr= GetItemPossessedBy (oPC, "g_w_shortsbr01");
object oLongSbr=GetItemPossessedBy (oPC, "g_w_lghtsbr01");


if (GetIsObjectValid(oShortSbr)) {
  nStack=GetItemStackSize(oShortSbr);
  if (nStack>1) {
    SetItemStackSize(oShortSbr,nStack-1);
  }
  else {
    DestroyObject(oShortSbr);
  }
  return;
}


if (GetIsObjectValid(oLongSbr)) {
  nStack=GetItemStackSize(oLongSbr);
  if (nStack>1) {
    SetItemStackSize(oLongSbr,nStack-1);
  }
  else {
    DestroyObject(oLongSbr);
  }
  return;
}
}
Link to comment
Share on other sites

Thanks tk Your script is much leaner than mine.

 

 

Originally posted by envida

Just to nitpick on tk102 code :D

 

There is no need for the "return;" in the if statements since the functions doesnt return anything. It s not very important but it makes the code a bit "cleaner" without it ;)

 

But without the return; the script will continue to remove 1 of the other items as well, which I don't want.

Link to comment
Share on other sites

I don't mean to be annoying, but it's probably more efficient and re-usable to write it like this instead:

 

int DestroyOneItem(object oItem) {
   if (GetIsObjectValid(oItem)) {
       int nStack = GetItemStackSize(oItem);

       if (nStack > 1)
           SetItemStackSize(oItem, nStack - 1);
       else
           DestroyObject(oItem);

       return TRUE;
   }
   return FALSE;
}

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

   object oSaber = GetItemPossessedBy(oPC, "g_w_shortsbr01");
   if (DestroyOneItem(oSaber))
       return;

   oSaber = GetItemPossessedBy(oPC, "g_w_lghtsbr01");     
   if (DestroyOneItem(oSaber))
       return;     
}

 

Then you can stick the DestroyOneItem() function in a "utility" include file and re-use it whenever you need to do something similar. :)

Link to comment
Share on other sites

I hope you do not keep your thoughts to yourself in the future! :) We need all the scripting experts we can get.

 

In the past there have been relatively few people who'd respond to script questions (Darth333 and me). That's why I was surprised to see two critiques on this thread, especially since the author of the thread accepted the code offered and the code is error-free.

Link to comment
Share on other sites

Originally posted by tk102

I hope you do not keep your thoughts to yourself in the future! :) We need all the scripting experts we can get.

My next mod will be all script based, so hopefully I'll be more help in the future. I have the programming background, it is just getting familiar with all the built in functions...

 

And as stoffe -mkb- mentioned, code reuse is definitely the way to go... :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...