Author Topic: [Scripting] Unit-Tags wont work for me (SOLVED)  (Read 238 times)

Offline Fampat

  • Squaddie
  • *
  • Posts: 6
    • View Profile
[Scripting] Unit-Tags wont work for me (SOLVED)
« on: December 19, 2024, 11:22:28 am »
Hi everyone!

last time ive modded was in april, everything worked fine then with the tags.
Ive started to play again with one of my mods and it stopped working, ive testen alot of possible issues i could imagein, ive also reverted to the OXCE version i used back in the day without luck.

Since i cannot image what might got wrong i kindly ask for help.

This is a little submod of mine for xpiratez. I REALLY DISLIKE hurting animals, even in video-games, therefor i made this mod which renders dogs, which would normaly die, unconscious.

The issue i see is that the tag i add to the BattleUnit STR_DOGG_TERRORIST (Tag: UNIT_LIVES_FOREVER: 1) is not available within the script-sections "damageUnit"-event.
To test this i debugged the value, also ive tested that script again by hard setting the UNIT_LIVES_FOREVER to the value of 1, which then triggered the desired effect correctly.

What might be wrong here?
Shouldnt the value for the tag always be 1 on the script in case the damaged BattleUnit is a STR_DOGG_TERRORIST?

The unit type is correct, the script gets triggered but the units tag value is always 0
Debug unit: BattleUnit(type: "STR_DOGG_TERRORIST" race: "STR_DEMON" id: 1000002 faction: Hostile hp: 25/25)

Another test i made was changing the units race to see if any changes i made via "units:" are applied, result is that changes apply

Here is the script:
Code: [Select]
units:
  - type: STR_DOGG_TERRORIST
    race: STR_DEMON
    tags:
      UNIT_LIVES_FOREVER: 1
extended:
  tags:
    BattleUnit:
      UNIT_LIVES_FOREVER: int
  scripts:
    damageUnit:
      - offset: 1
        code: |
          # Inits
          var int stunSummed;
          var int liveForever;
          var int armorForever;
          var int unitCurrStun;
          var int unitMaxStun;

          # Load game values into variables
          unit.getStun unitCurrStun;
          unit.getStunMax unitMaxStun;

          # DEBUG TEST
          #unit.setTag Tag.UNIT_LIVES_FOREVER 1;

          # Fetch unit-tag
          unit.getTag liveForever Tag.UNIT_LIVES_FOREVER;

          # DEBUG
          debug_log "UNIT: " unit;
          debug_log "LF_STATE: " liveForever;

          # Check if this unit shouldnt die
          if gt liveForever 0;
            # Transfer health-damage to stun-damage
            if gt to_health 0;
              set to_stun to_health;
              set to_health 0;

              add stunSummed to_stun;
              add stunSummed unitCurrStun;

              # Never overstun
              if gt stunSummed unitMaxStun;
                set to_stun unitMaxStun;
              end;
            end;

            # Never bleed
            if gt to_wound 0;
              set to_wound 0;
            end;
          end;

          return;

Ive installed only xpirates and my submod.

Any help would be appriciated!
Thanks

« Last Edit: December 19, 2024, 01:47:03 pm by Fampat »

Offline Meridian

  • Global Moderator
  • Commander
  • *****
  • Posts: 9136
    • View Profile
Re: [Scripting] Unit-Tags wont work for me
« Reply #1 on: December 19, 2024, 11:34:09 am »
"units:" are unit types, not unit instances
"BattleUnit:" are unit instances (objects), not unit types

you probably wanted to use "RuleUnit" instead of "BattleUnit" ?
or maybe "RuleArmor" ?

Offline Fampat

  • Squaddie
  • *
  • Posts: 6
    • View Profile
Re: [Scripting] Unit-Tags wont work for me
« Reply #2 on: December 19, 2024, 12:03:18 pm »
Thanks for you feedback.

Ive tested your suggestions without any luck, the tag remains empty (0).
Using the RuleUnit renders the script unusable with an invalid operation error on unit.getTag

RuleArmor and BattleUnit dont crash, but also not seem to containe the tag defined in "units:"

Am i wrong in the assumption that a "units:"-tags are also part of these units instances (BattleUnit)?
Maybe, at least i cannot see any tag-information on the debugging log for the "unit".




Offline Meridian

  • Global Moderator
  • Commander
  • *****
  • Posts: 9136
    • View Profile
Re: [Scripting] Unit-Tags wont work for me
« Reply #3 on: December 19, 2024, 12:37:15 pm »
Am i wrong in the assumption that a "units:"-tags are also part of these units instances (BattleUnit)?

99% chance that you are wrong.
Those tags afaik are not part of the unit instance, they are part of the unit type.

still 1% chance that I am wrong and there is an exception here and the tags are somehow automatically duplicated on both places


If you want to get a tag from a unit instance, you can:
1. initiate it on unit instance creation, and then read later (BattleUnit tag)
2. get the unit type from the unit instance and then read the tag from the unit type (RuleUnit or RuleSoldier tags)
3. get the unit's armor type from the unit instance and then read the tag from the armor type (RuleArmor tag)

PS: not sure if RuleUnit getter even exists, RuleSoldier and RuleArmor definitely do exist.

Offline Fampat

  • Squaddie
  • *
  • Posts: 6
    • View Profile
Re: [Scripting] Unit-Tags wont work for me
« Reply #4 on: December 19, 2024, 01:02:53 pm »
I see, one thing is preventing me from that.

Is there any way for "get the unit type" within the scripts you know of?
Without that i would also be not able to set a tag on the "createUnit"-event, because its the same BattleUnit with the same getters/setters as in the damageUnit-event

Within the scripts i dont see how i can get the unit-type from the unit instance (BattleUnit) for a condintionaly check, there is no getter available, that is why i want to use the tags in the first place, its the only way for me to identify the BattleUnit new ability to be "UNIT_LIVES_FOREVER", which i would only need on a hand full unit-types. The script-api dont show any way to directly identify the BattleUnits type.

Identifying the unit in an event would solve all problems for me i guess, maybe iam just not seeing a obvious solution for this :) Old blind man me is haha

Offline Yankes

  • Global Moderator
  • Commander
  • *****
  • Posts: 3374
    • View Profile
Re: [Scripting] Unit-Tags wont work for me
« Reply #5 on: December 19, 2024, 01:12:31 pm »
I suggest using armors instead, as all units need have specific armor and most behaviors was linked to armors anyway.

but in next version we should add `getRuleUnit` as is lack was oversight (for at lest 8y nobody ask for it :D )

Offline Fampat

  • Squaddie
  • *
  • Posts: 6
    • View Profile
Re: [Scripting] Unit-Tags wont work for me
« Reply #6 on: December 19, 2024, 01:27:30 pm »
Thanks you both for your highly valueable feedback on this, its working now via armor-tagging.

Now my mind can go on with conquering the world since i dont have any tears in my eyes anymore, which blocks my sight, with every dead dog!


I realy could have sworn that the variant via BattleUnit did work back the day on oxce 7.8

Offline Meridian

  • Global Moderator
  • Commander
  • *****
  • Posts: 9136
    • View Profile
Re: [Scripting] Unit-Tags wont work for me
« Reply #7 on: December 19, 2024, 04:07:26 pm »
Moved to Suggestions (so that we don't forget to add the missing getter).