OpenXcom Forum

Modding => OXCE Support => OpenXcom Extended => OXCE Support Y-scripts => Topic started by: Nord on August 09, 2021, 09:18:28 am

Title: [Solved] Script access to builtInWeapons.
Post by: Nord on August 09, 2021, 09:18:28 am
Continuing this discussion:
https://openxcom.org/forum/index.php/topic,9992.0.html (https://openxcom.org/forum/index.php/topic,9992.0.html)

I managed to create an item, which represents shield power. It is built-in in armor, and updates each turn.
But now i want to update it each time the unit is hit. And i can not gain access to this item from "hitUnit:" script hook.

Is this even possible?
Title: Re: Script access to builtInWeapons.
Post by: Yankes on August 12, 2021, 11:30:43 am
 `getSpecialItem` and `getSpecialItem.size` is used to access unit buildin weapons.
You need iterate for each weapon to find one you want.
Title: Re: Script access to builtInWeapons.
Post by: Nord on August 13, 2021, 02:49:18 am
So, if a unit have more than one builtInWeapons, how must i search through them?
Include internal cycle in hitUnit?
Can you provide some kind of example?
Pleeeease.
Thanks.
Title: Re: Script access to builtInWeapons.
Post by: Buscher on August 13, 2021, 02:18:17 pm
You can consider this example if you like
Code: [Select]
    newTurnUnit:
      - offset: 20
        code: |
...
          var int temp;
          var int numInventoryItems;

          var ptr RuleItem rItem;

          var ptre BattleItem tempItem;
          var ptre BattleItem itemDummy;
          var ptre BattleItem itemWeapon;
...
          unit.getSpecialItem itemWeapon 0; # might need a sanity check with getSpecialItem.size
          itemWeapon.getRuleItem rItem;
          rItem.getTag temp Tag.ITEM_IS_VOX_WEAPON;
          if neq temp 1;
            return;
          end;

          unit.getInventoryItem.size numInventoryItems;
          loop var i numInventoryItems;
            unit.getInventoryItem tempItem i;
            tempItem.getRuleItem rItem;
            rItem.getTag temp Tag.ITEM_IS_VOX_DUMMY;
            if and eq temp 1 eq itemDummy null;
              unit.getInventoryItem itemDummy i;
            end;
          end;
...
          return;

Title: Re: Script access to builtInWeapons.
Post by: Nord on August 14, 2021, 03:16:36 am
You can consider this example if you like
Code: [Select]
    newTurnUnit:
      - offset: 20
        code: |
...
          var int temp;
          var int numInventoryItems;

          var ptr RuleItem rItem;

          var ptre BattleItem tempItem;
          var ptre BattleItem itemDummy;
          var ptre BattleItem itemWeapon;
...
          unit.getSpecialItem itemWeapon 0; # might need a sanity check with getSpecialItem.size
          itemWeapon.getRuleItem rItem;
          rItem.getTag temp Tag.ITEM_IS_VOX_WEAPON;
          if neq temp 1;
            return;
          end;

          unit.getInventoryItem.size numInventoryItems;
          loop var i numInventoryItems;
            unit.getInventoryItem tempItem i;
            tempItem.getRuleItem rItem;
            rItem.getTag temp Tag.ITEM_IS_VOX_DUMMY;
            if and eq temp 1 eq itemDummy null;
              unit.getInventoryItem itemDummy i;
            end;
          end;
...
          return;
"newTurnUnit" is not what i seek. I want to look into "hitUnit" hook.
Title: Re: Script access to builtInWeapons.
Post by: Buscher on August 14, 2021, 01:11:49 pm
You were asking how to access a specialWeapon and it makes (nearly) zero difference in syntax if it is a newTurnUnit hook or a hitUnit hook. It should work the same.

'attacker' as well as 'unit' are accessible and editable (ptre BattleUnit) for both script hooks. This means that your specialWeapon is also editable (ptre BattleItem). You don't even have to change anything in the example to access it with the variable 'itemWeapon' from the example.
Title: Re: Script access to builtInWeapons.
Post by: Nord on August 14, 2021, 02:36:46 pm
Ok, i think i got it.
If in "unit.getSpecialItem itemWeapon 0;" 0 - is the special weapon's index number.
I should try. Thank you.

Upd. 2: It works!
Thank you very much!