Page 1 of 1

Item in backpack

Posted: Sat Jan 14, 2012 1:22 am
by n3k
How to check whether the item is located in backpack?

Re: Item in backpack

Posted: Sat Jan 14, 2012 7:44 am
by Turley

Code: Select all

Var cont:=item.container;
While (cont)
cont:=cont.container;
Endwhile
If cont==chr
 ...
Something like that

Re: Item in backpack

Posted: Sat Jan 14, 2012 9:14 am
by Tomi
If you only want to check if the item is in the backpack you just check with

Code: Select all

if ( item.container == character.backpack )
but if you want to check all the sub containers aswell you have to follow Turleys advice and loop through them

Re: Item in backpack

Posted: Sun Jan 15, 2012 1:07 pm
by Terciob
also you can use something like this:

Code: Select all

function IsOnBackPack (chr, item)
	if(!item.container) 
		return 0;
	elseif(item.container == chr.backpack)
		return 1;
	else
		return IsOnBackPack(chr, item.container);
	endif
endfunction
/code]

Re: Item in backpack

Posted: Sun Jan 15, 2012 9:04 pm
by CWO
Tericob its better to do a while loop than have a function call itself repeatedly because it will take up more memory and processing going further into call depth. Your script also has the possibility of reaching maximum call depth in the worst case scenario which will halt the script entirely.

Code: Select all

function IsInBackpack(item, backpack)
     while (item.container)
          if (item.container == backpack)
               return 1;
          endif
          item := item.container;
     endwhile
     return 0;
endfunction
This code should work perfect for you.