aliens

Author Topic: [Solved] Script access to builtInWeapons.  (Read 2085 times)

Offline Nord

  • Commander
  • *****
  • Posts: 1625
  • The Gate is open.
    • View Profile
[Solved] Script access to builtInWeapons.
« on: August 09, 2021, 09:18:28 am »
Continuing this discussion:
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?
« Last Edit: February 12, 2023, 03:01:24 pm by Meridian »

Offline Yankes

  • Commander
  • *****
  • Posts: 3194
    • View Profile
Re: Script access to builtInWeapons.
« Reply #1 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.

Offline Nord

  • Commander
  • *****
  • Posts: 1625
  • The Gate is open.
    • View Profile
Re: Script access to builtInWeapons.
« Reply #2 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.

Offline Buscher

  • Colonel
  • ****
  • Posts: 167
    • View Profile
Re: Script access to builtInWeapons.
« Reply #3 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;


Offline Nord

  • Commander
  • *****
  • Posts: 1625
  • The Gate is open.
    • View Profile
Re: Script access to builtInWeapons.
« Reply #4 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.

Offline Buscher

  • Colonel
  • ****
  • Posts: 167
    • View Profile
Re: Script access to builtInWeapons.
« Reply #5 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.

Offline Nord

  • Commander
  • *****
  • Posts: 1625
  • The Gate is open.
    • View Profile
Re: Script access to builtInWeapons.
« Reply #6 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!
« Last Edit: August 15, 2021, 07:56:49 am by Nord »