Add custom variable to unit

The place to post your WML questions and answers.

Moderator: Forum Moderators

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Post Reply
pancho
Posts: 9
Joined: September 18th, 2009, 4:05 pm

Add custom variable to unit

Post by pancho »

I want that each recruited unit has it's own copy of a custom variable. I have added a variable to [unit_type], everything was OK until i've tryed to modify it and invoke [unstore_unit].

Code: Select all

[unit_type]
...
[myera]
  myvar=no
[/myera]
...
[/unit_type]

Code: Select all


[era]
...
[event]
  name=defender_hits, defender_misses
  first_time_only=no

  [filter]
    [filter_wml]
      [myera]
        myvar=no  # i'm able to filter against it
      [/myera]
    [/filter_wml]
  [/filter]

  {DEBUG_MSG $second_unit.myera.myvar} # prints "no", i'm able to read it

  {VARIABLE_OP second_unit.myera.myvar value yes}
  
  {DEBUG_MSG $second_unit.myera.myvar} # prints "yes", i'm able to change it

  [unstore_unit]
    variable=second_unit
  [/unstore_unit]

  [store_unit]
    [filter]
      x=$x2
      y=$y2
    [/filter]
    variable=sec
  [/store_unit]

  {DEBUG_MSG $sec.myera.myvar} # prints "no", i'm not able to keep it (

[/event]
...
[/era]
Is it possible to make such a thing?
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: Add custom variable to unit

Post by zookeeper »

Try to put the variable as unit.variables.myvar (or unit.variables.myera.myvar, whichever) instead of unit.myera.myvar. Although defining a [variables] in your [unit_type] might not work, so you might need to instead use some recruit events and such to give those variables to units.
User avatar
Ken_Oh
Moderator Emeritus
Posts: 2178
Joined: February 6th, 2006, 4:03 am
Location: Baltimore, Maryland, USA

Re: Add custom variable to unit

Post by Ken_Oh »

I can confirm defining [variables] in your [unit_type] will not work, as I tried this week.

I'm interested to see what happens in this thread, because I can't see anything wrong with the above code.
pancho
Posts: 9
Joined: September 18th, 2009, 4:05 pm

Re: Add custom variable to unit

Post by pancho »

Thanks for your answers.

Works fine with [set_variable] in recruit event, no matter under [variables] or not.

Still works after savegame/loadgame.

If saved after all the recruiting, result WML in savegame file near identical to that without recruit event (WML in [unit_type]). But loading from one savegame works, and another does not.

Looks like kind of magic )
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: Add custom variable to unit

Post by zookeeper »

pancho wrote:Works fine with [set_variable] in recruit event, no matter under [variables] or not.
However, when the variables are not inside [variables], they might get destroyed when the unit levels up, so it's still a good idea to use [variables].
pancho
Posts: 9
Joined: September 18th, 2009, 4:05 pm

Re: Add custom variable to unit

Post by pancho »

Sorry, it works properly only in the [variables] scope.
User avatar
Gambit
Loose Screw
Posts: 3266
Joined: August 13th, 2008, 3:00 pm
Location: Dynamica
Contact:

Re: Add custom variable to unit

Post by Gambit »

zookeeper wrote:
pancho wrote:Works fine with [set_variable] in recruit event, no matter under [variables] or not.
However, when the variables are not inside [variables], they might get destroyed when the unit levels up, so it's still a good idea to use [variables].
They don't. I tested all this in WesPets.
Spoiler:
Even after dozens of level ups I could still access every units color_mod.
Exasperation
Posts: 462
Joined: June 8th, 2006, 3:25 am

Re: Add custom variable to unit

Post by Exasperation »

Those (IIRC) were AMLAs, though. Actually changing unit type is different from getting an AMLA (I ran into some of those differences working on levelup code for NPCs in Wesband). If the unit changes to a new type it's more destructive than if they just AMLA.
User avatar
Gambit
Loose Screw
Posts: 3266
Joined: August 13th, 2008, 3:00 pm
Location: Dynamica
Contact:

Re: Add custom variable to unit

Post by Gambit »

So then I guess you'd need a pre leveup even and a post one to transfer the variable.

But before we get ahead of ourselves has anyone tested this specific scenario?
dvormann
Posts: 51
Joined: July 24th, 2009, 11:33 am

Re: Add custom variable to unit

Post by dvormann »

Gambit wrote:So then I guess you'd need a pre leveup even and a post one to transfer the variable.

But before we get ahead of ourselves has anyone tested this specific scenario?
Modern Combat uses unit.range to define artillery range. It survives leveling.

Moving it to unit.variables.range is no big deal. But there's no reason to do so. At least not yet.
"m" is the key used most in Wesnoth.
pancho
Posts: 9
Joined: September 18th, 2009, 4:05 pm

Re: Add custom variable to unit

Post by pancho »

If you want to modify your variable and then store it with [store_unit], you should declare it explicitly with [set_variable] in event of some kind and keep it in the [variables] scope. Otherwise you will lose write access to it after savegame/loadgame.
Post Reply