OSP Code Optimisation Check if item has flag - How to.

Users who are viewing this thread

ConstantA

Knight
Using item_get_type, we can check if item has itp_type_ flags.
But what  if we want to check another itp_ flag, like itp_no_parry, itp_unbalanced or others?
Here's a small script which'll make it possible.
(I use it for SP, so the examples are for SP).

Create new item slot: slot_item_flags

Place the following code either into module_scripts.py before scripts tuple, or in a new file (for example, my_scripts.py) and import it to module scripts using "from my_scripts import *".
Code:
def set_item_flags():
    item_flags = []
    for i_item in xrange(len(items)):
        item_flags.append((item_set_slot, i_item, slot_item_flags, items[i_item][3]))
    return item_flags[:]

Create new script
Code:
#script_setup_item_flags
("setup_item_flags", set_item_flags()),

Call it inside script_game_start
Code:
("game_start",   [
      (call_script,"script_setup_item_flags"),

To check if the item has certain flag, we'll need the following script:
Code:
#script_cf_item_has_flag
#INPUT: item, flag 
("cf_item_has_flag", [
(store_script_param, ":item_no", 1),
(store_script_param, ":flag", 2),
(item_get_slot, ":flags", ":item_no", slot_item_flags),
(store_and, ":result", ":flags", ":flag"),
(neq, ":result", 0), 
]),
 
Aewsome script, preety much what I needed, this could be used to make new item flags I think.
just add the new flags to header_items.py and then use this script to check if the item has the flag.
 
Sorry for the thread necro, but I only just saw this code and realised it's just what I need.  Problem is though for some reason it doesn't recognise 'items' (which I'm guessing is the items tuple in module_items.py) in this part:
Code:
def set_item_flags():
    item_flags = []
    for i_item in xrange(len(items)):
        item_flags.append((item_set_slot, i_item, slot_item_flags, items[i_item][3]))
    return item_flags[:]
The actual error is:
Code:
in set_item_flags
  for i_item in xrange(len(items)):
NameError: global name 'items' is not defined

As per instructions I have this code at the top of module_scripts.py (outside the scripts tuple of course).
 
It sort of works, but breaks other things.  Whoops. :smile:  Was a very nice try though!  Didn't think of import.  :roll:

Tried putting it at the top of module_items.py but not surprisingly it complains that it can't find other variables.  Just don't see how this worked for others as I don't think there has been a new Warband patch since this code was first posted.  Will keep on plugging away.  Maybe I've missed something obvious.

Edit: I'm at a total loss.  Doesn't help that my Python knowledge (which is needed for this bit) is practically zero.  I've tried all sorts of things including putting it into it's own source file and then only importing the bits that function needs.  Just causes other things to break.  Hoping someone on here has actually gotten this to work.

Edit:  If this does work for others, only other thing I can think of (assuming I've put all the script in the right places) is some oddity of whichever Python version I'm using which is 2.7.1.

Edit: Never mind.  I don't think I need this anymore, so am not worried if I can't get it working. :smile:
 
Back
Top Bottom