T7nowhere Posted April 13, 2005 Share Posted April 13, 2005 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 More sharing options...
tk102 Posted April 13, 2005 Share Posted April 13, 2005 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 More sharing options...
envida Posted April 13, 2005 Share Posted April 13, 2005 Just to nitpick on tk102 code 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 Link to comment Share on other sites More sharing options...
T7nowhere Posted April 13, 2005 Author Share Posted April 13, 2005 Thanks tk Your script is much leaner than mine. Originally posted by envida Just to nitpick on tk102 code 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 More sharing options...
envida Posted April 13, 2005 Share Posted April 13, 2005 hmm you are right...I missed that one...goes hide in a corner Link to comment Share on other sites More sharing options...
T7nowhere Posted April 13, 2005 Author Share Posted April 13, 2005 Don't worry about it You still know more about scripting than I do. I've been modding these games for over a year and I'm just learning how to script now Link to comment Share on other sites More sharing options...
stoffe Posted April 13, 2005 Share Posted April 13, 2005 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 More sharing options...
tk102 Posted April 13, 2005 Share Posted April 13, 2005 Jeez, when did eveyone become code critics? Link to comment Share on other sites More sharing options...
stoffe Posted April 13, 2005 Share Posted April 13, 2005 Originally posted by tk102 Jeez, when did eveyone become code critics? I'm sorry, I'll keep my thoughts to myself in the future. Link to comment Share on other sites More sharing options...
tk102 Posted April 13, 2005 Share Posted April 13, 2005 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 More sharing options...
Prime Posted April 13, 2005 Share Posted April 13, 2005 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 More sharing options...
shosey Posted April 13, 2005 Share Posted April 13, 2005 I was playing around with changing the stack size before and I always get the "Items Lost" message affter the script is complete. Is there a way to suppress this message? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.