How to use a NumberDesc as if it were an integer?

Asked by Andre Novellino Gouvêa on 2013-09-23

I am not managing to get information out of array by using a NumberDesc. For instance, with this code:

 { Pos = {114, 11}, More = {"Text", {Text = DwarvenNames[ActiveUnitVar("CharacterName")], Centered = true}} },

The text doesn't appear, but if I replace the "ActiveUnitVar("CharacterName")" part with an integer, then it works perfectly. What am I doing wrong?

Question information

Language:
English Edit question
Status:
Solved
For:
Stratagus Edit question
Assignee:
No assignee Edit question
Solved by:
cybermind
Solved:
2013-09-23
Last query:
2013-09-23
Last reply:
2013-09-23
Best cybermind (iddqd-mail) said : #1

There is no way to convert NumberDesc into ordinary Lua number, since NumberDesc is computed real-time and so Lua can't index DwarvenNames with it.
The only way to make this thing is to program a new dynamic type (maybe called TableDesc or something like that) which could be initialized with ordinary Lua types and index it's contents by NumberDesc

Thanks, cybermind! In that case, do you happen to know of another way to access a variable of the "active" unit?

Joris Dauphin (joris-dauphin) said : #3

I would like to remove NumberDesc stuff, and expose more variable to lua.

By looking at cybermind's Doom Wars lua code, I was able to come up with code that implements this:

 { Pos = {114, 11}, More = {"Text", {Text =
  If(Equal(ActiveUnitVar("CharacterName"), 1), DwarvenNames[1],
  If(Equal(ActiveUnitVar("CharacterName"), 2), DwarvenNames[2],
  If(Equal(ActiveUnitVar("CharacterName"), 3), DwarvenNames[3],
  If(Equal(ActiveUnitVar("CharacterName"), 4), DwarvenNames[4],
  If(Equal(ActiveUnitVar("CharacterName"), 5), DwarvenNames[5],
  ""
  ))))), Centered = true }}
 },

It is not ideal, since I have to manually add a new line for each possible element of the array, but it works.

Joris, it would be great if more variables were exposed to lua. In particular, I think that it would be nice if we could change the HP, Attack Range and etc. of individual units, instead of just that of unit types. I implemented a trait system which randomly gives units individual traits, but I was only able to change an individual unit's damage and armor (since I could access the damage formula, I could use that to create a work-around to make a unit with the trait "Strong" do more damage, and then I changed ui.lua to reflect that in the unit's display), and the trait system would be much more interesting if I could change things such as HP and etc. as well.

cybermind (iddqd-mail) said : #5

You can use NumberDesc only for active(currently selected) unit variables representation in UI.
For getting and setting individual unit variables you could use GetUnitVariable/SetUnitVariable.