RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Find item in backpack

zerglingcho

Wanderer
Find item in backpack

Guys, how can i check if a guy has an item with the name oF "blabla" in his backpack? i tried using
foreach ( Item asd in from.Backpack )

but it comes up with an error (it cant access container stuff or sth)
 

Phantom

Knight
I would use a for loop if you want to write quality code :)

For loops are better then foreach loops. You just have to avoid going out of bounds of the index, when you use a for loops.
 

juani

Wanderer
phantom... wouldnt that put the backpack itself in the item variable???

i understood he wanted to find an item thats inside the backpack....
 

juani

Wanderer
the part i dont understnad is this:
Mobile.FindItemOnLayer( Layer.Backpack )
wuldnt that return the item thats on the layer backpack? that should be the backpack itself right?

if not then im doing wrong in the script that equips weapons on double click cause im using something like:
Item twoHandedItem = from.FindItemOnLayer( Layer.TwoHanded );
Item oneHandedItem = from.FindItemOnLayer( Layer.OneHanded );
to find objects players might have equipped...
 
D

Dupre

Guest
ive tried using this in my BaseQuestMonster script and it doesnt work, i was originally using a for loop, but that crashed the shard :(

Error
Code:
 - Error: Scripts\Custom\QuestChest\Monsters\BaseQuestMonster.cs: CS1026: (line
123, column 19) ) expected
 - Error: Scripts\Custom\QuestChest\Monsters\BaseQuestMonster.cs: CS1023: (line
123, column 19) Embedded statement cannot be a declaration or labeled statement
 - Error: Scripts\Custom\QuestChest\Monsters\BaseQuestMonster.cs: CS1002: (line
123, column 28) ; expected
 - Error: Scripts\Custom\QuestChest\Monsters\BaseQuestMonster.cs: CS1525: (line
123, column 28) Invalid expression term 'is'
 - Error: Scripts\Custom\QuestChest\Monsters\BaseQuestMonster.cs: CS1002: (line
123, column 31) ; expected
 - Error: Scripts\Custom\QuestChest\Monsters\BaseQuestMonster.cs: CS1002: (line
123, column 42) ; expected
 - Error: Scripts\Custom\QuestChest\Monsters\BaseQuestMonster.cs: CS1525: (line
123, column 42) Invalid expression term ')'
 - Error: Scripts\Custom\QuestChest\Monsters\BaseQuestMonster.cs: CS1002: (line
123, column 43) ; expected

Code
Code:
Item item = This.FindItemOnLayer( Layer.Backpack )
if ( item != null and item is BowyersBow ) <= Line 123
{
	item.Delete();
}
 

juani

Wanderer
if ( item != null and item is BowyersBow ) <= Line 123
"and" ??
try "&&"

i still dont understand the phantom way tho.
just as a test u could try the foreach way.
 

Phantom

Knight
He didn't do exactly what I suggested...

Code:
Item item = This.FindItemOnLayer( Layer.Backpack )

should be:

Code:
Item item = This.FindItemOnLayer( Layer.Backpack );

Code:
if ( item != null and item is BowyersBow ) <= Line 123
{
	item.Delete();
}

Should be"
Code:
if ( item != null && item is BowyersBow ) <= Line 123
{
	item.Delete();
}

But it really does not matter. I was mistaken when I even suggested that. Use a for loop going through Mobile.Backpack.Items

using the Array method .Count

Be sure not to go out of the bounds of the array index. You can't go past 0 ( first element ).
 

ArteGordon

Wanderer
hehe.
here, take a look at this code. It does what you want. It will even search recursively through other containers that are in the container you start with. If you only want to search the top level of the container, just cut out the recursive container test conditional.

Code:
public Item SearchPackForItem(Container pack, string targetName)
        {
        		  if(pack != null && !pack.Deleted && targetName != null && targetName.Length > 0){
            			// go through all of the items in the pack
            			ArrayList packlist = pack.Items;

            			for ( int i = 0; i < packlist.Count; ++i )
            			{
            				Item item = (Item)packlist[i];
            				if(item != null && !item.Deleted && item is Container){
                                    Item itemTarget = SearchPackForItem((Container)item, targetName);
                                    if(itemTarget != null) return itemTarget;
                            } else
            				// test the item name against the string
            				if(item != null && !item.Deleted && (item.Name != null) && (item.Name == targetName)){
                                //found it
                                return item;
            				}
            			}
                  }
                  return null;
        }
 

Ceday

Page
Phantom said:
Well the idea would be

Pick an item like

Dagger dagger = Mobile.FindItemOnLayer( Layer.Backpack );

Then test if the item is a dagger.

You can also use this in a for loop.

You could also do the Mobile.FindItemOnLayer in the if test itself something like:

if ( Mobile.FindItemOnLayer( Layer.Backpack ) != null && Mobile.FindItemOnLayer( Layer.Backpack ) is Dagger )

That would make sure basicly your have something in your Backpack and only pass if the Item ( FindItemOnLayer is a Item Method ) is a Dagger.

I did something similar in my Faction code. Although its "core" level unlike how FindItemOnLayer( Layer layer ) is user level.

There is a difference how code written for "Core" use and "user" use is written.
lol phantom, i really would like to see that faction codes :)
dont lead people wrong, they were doing right thing..
 

Phantom

Knight
I am more then welcome to show you what I mean:

Code:
		public TownControlStone GetTownControlStone( int l ) 
		{ return ( TownControlStone ) TownControlStoneList[l]; }
		
		public TownStone GetTownStone( int l ) 
		{ return ( TownStone ) TownStoneList[l]; }

		public TownSigil GetTownSigilStone( int l ) 
		{ return ( TownSigil ) TownSigilList[l]; }

		public Faction GetFaction( int l ) 
		{ return ( Faction ) FactionList[l]; }

		public FactionStone GetFactionStone( int l ) 
		{ return ( FactionStone ) FactionStoneList[l]; }

		public TownMonolith GetTownMonolithStone( int l ) 
		{ return ( TownMonolith ) TownMonolithList[l]; }

		public TownMonolith GetFactionTownMonolithStone( int m, int n ) 
		{ return ( TownMonolith )       ((Faction)FactionList[m]).GetFactionMonolithStone(n); }

Exactly like FindItemOnLayer(Layer layer)

The person is right now that I think about it. It would return the Backpack item.... :p
 

Ceday

Page
btw, this is just a suggestion, dont get wrong:
dont use l,m,n in your code, use some meaningful variables.
otherwise, you have to do double work, if you dont, people wont understand that code much..
 

Phantom

Knight
They are just counter variables.

I learned 2 years ago what counter variables should be. The method are not to be used outside a certain context. So its not really a problem.

Like I said that "code" is the "core" ie "heart" of the Faction System at this point. I would hope alot of people will not be editing that part :)

Not alot of Heart Doctors on this board, might cut something vitial :p
 
Top