Author Topic: [Suggestion] ptr for getInventoryItem.(size)  (Read 1685 times)

Offline Buscher

  • Colonel
  • ****
  • Posts: 167
    • View Profile
[Suggestion] ptr for getInventoryItem.(size)
« on: April 15, 2022, 01:26:16 pm »
Currently I try to recolor sprites depending on items in the inventory. The script command getInventoryItem only works with ptre BattleUnit which isn't accessible in the recolorUnitSprite script hook. In this hook only ptr BattleUnit is available. I can imagine that iterating through the inventory every cycle might considered a waste of CPU and therefore ptr isn't allowed there. If this is not a problem I would like to suggest that a ptr version for getInventoryItem(.size) is made available.

Thanks for reading and possible consideration.

Offline Yankes

  • Commander
  • *****
  • Posts: 3207
    • View Profile
Re: [Suggestion] ptr for getInventoryItem.(size)
« Reply #1 on: April 16, 2022, 02:52:37 am »
What is exactly thing you want archive? Maybe there are other options to have this?

`getInventoryItem` is not very "heavy" function, bigger problem is that this code will be called 100 times for every pixel in soldier.
And then multiply by 60fps and number of visible units. This could add up.

I try approach very conservative with scripts, this could be that some times I was doing it with too much caution.

Offline Buscher

  • Colonel
  • ****
  • Posts: 167
    • View Profile
Re: [Suggestion] ptr for getInventoryItem.(size)
« Reply #2 on: April 16, 2022, 09:51:35 am »
What I try to solve is to do a shield flash indicator for an active personal shield generator being inside the inventory. For now I scan the inventory of the unit in 'newTurnUnit' and tag the unit for the turn. The tag is used to determine if the unit should flash with 'recolorUnitSprite' or not. It works well enough. The only edge case I cannot cover is when the player decides to drop the personal shield because of the potential explosion hazard with its interaction with Las weaponry.

Maybe I just turn it into a medikit item and do funny stuff with the 'healUnit' by turning the 'medikit_action_type' into an on/off switch. Or I could just use a prime/unprime action. And that together with a skill so I can lock the personal shield item into a specific slot which might have horrendous TU move costs. Limitations sometimes create creative solutions.

Offline Yankes

  • Commander
  • *****
  • Posts: 3207
    • View Profile
Re: [Suggestion] ptr for getInventoryItem.(size)
« Reply #3 on: April 16, 2022, 01:04:59 pm »
I think for proper solution for `recolorUnitSprite` like scripts, that visit multiple pixel in same context, my script should have way to transfer data between invocations. With this first "pixel" will be very costly but but any after cheap.

Idea is to have some static variables that do not change its state between invocations in same context, like:

Code: [Select]
- offset: 1
  code: |
    if eq static_z 0;
      set static_z 1;
    else;
      debug_log "another iteration when z was set!";
    end;
    return;
- offset: 2
  code: |
    if eq static_z 1; #see value that was set in "1"
      debug_log "z was set!";
    end;
    return;

Question is how best handle indention for this variables in yaml and how effective implement this is code.
One possible example would be:
Code: [Select]
extended:
  scriptsVarStatic:
    recolorUnitSprite:
      - name: XXXX #name visible in script
        type: int #type of variable, could be too `type: "ptr BattleUnit"`
        public: 1 #will be visible in other files/mods too

Offline Buscher

  • Colonel
  • ****
  • Posts: 167
    • View Profile
Re: [Suggestion] ptr for getInventoryItem.(size)
« Reply #4 on: April 16, 2022, 02:34:43 pm »
Static Variables would be really interesting. Maybe this is also a way to get towards code reuse/functions.

The example looks good. I could probably work very well with that.

Offline Meridian

  • Global Moderator
  • Commander
  • *****
  • Posts: 8616
    • View Profile
Re: [Suggestion] ptr for getInventoryItem.(size)
« Reply #5 on: July 25, 2022, 11:17:42 am »
just FYI, this is not on my todolist, I leave it to Yankes