Get the movement_type

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
Jovycos
Posts: 59
Joined: October 12th, 2007, 4:15 pm
Location: Germany

Get the movement_type

Post by Jovycos »

Hi!

I would like to get the movement_type of a unit from WML/Lua.
But unfortunately it seems not to be stored in the units.
How can I receive it? Do I have to get the unit_type and can I access the movement_type there? How to get the unit_type?

Regards
Jovycos
Full of Nerdiness...
silene
Posts: 1109
Joined: August 28th, 2004, 10:02 pm

Re: Get the movement_type

Post by silene »

Jovycos wrote:Do I have to get the unit_type and can I access the movement_type there?
Yes. Here is some Lua for creating a WML tag that stores the movement name into a variable.

Code: Select all

local helper = wesnoth.require "lua/helper.lua"
wesnoth.register_wml_action("get_movement_type", function(cfg)
  local u = wesnoth.get_units(cfg)[1] or helper.wml_error "[get_movement_type] filter didn't match any unit"
  local t = wesnoth.get_unit_type(u.__cfg.type)
  local v = cfg.variable or "movement_type"
  wesnoth.set_variable(v, t.__cfg.movement_type)
end)
Usage:

Code: Select all

[get_movement_type]
  # a Standard Unit Filter
  x=$x1
  y=$y1
  # a variable name or "movement_type" if missing
  variable=variable_name
[/get_movement_type]
Jovycos
Posts: 59
Joined: October 12th, 2007, 4:15 pm
Location: Germany

Re: Get the movement_type

Post by Jovycos »

Okay, thanks, I will try that:

Code: Select all

function get_movement_type(unit)
  local type = unit.type
  if type == nil then
    return nil
  else
    return wesnoth.get_unit_type(unit.type).movement_type
  end
end
Thanks.
Full of Nerdiness...
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: Get the movement_type

Post by zookeeper »

Jovycos wrote:I would like to get the movement_type of a unit from WML/Lua.
But unfortunately it seems not to be stored in the units.
How can I receive it? Do I have to get the unit_type and can I access the movement_type there? How to get the unit_type?
While movement_type is not stored in the units, the defense, movement costs and resistances are. Maybe that's enough for whatever you're doing, maybe it isn't; just saying.
silene
Posts: 1109
Joined: August 28th, 2004, 10:02 pm

Re: Get the movement_type

Post by silene »

Jovycos wrote:Okay, thanks, I will try that:
While it looks sensible, it unfortunately won't work. First, there is no "type" field for units. (It must have been accidentally deleted from the engine somehow, sorry.) So you have to recover it through the WML data (__cfg field) of the unit. Similarly, there is no "movement_type" for unit types. (I didn't expect someone to need it, so it is not a native field.)

Code: Select all

function get_movement_type(unit)
  local type = wesnoth.get_unit_type(unit.__cfg.type)
  return type and type.__cfg.movement_type
end
Jovycos
Posts: 59
Joined: October 12th, 2007, 4:15 pm
Location: Germany

Re: Get the movement_type

Post by Jovycos »

Oh, thanks for correcting that. I had not yet tested it and simply wanted to share my approach.
I want to get the movement-type, because it can be used as an indicator how "strong" a unit is.
I am not interested in the concrete values.
Full of Nerdiness...
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: Get the movement_type

Post by Anonymissimus »

@silene
You're mixing some C++ into it: "&&" :P
By the way did you knew lua before implementing it in wesnoth ? You're so quick with its syntax, though supposedly untested, mostly correct...
projects (BfW 1.12):
A Simple Campaign: campaign draft for wml startersPlan Your Advancements: mp mod
The Earth's Gut: sp campaignSettlers of Wesnoth: mp scenarioWesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
Jovycos
Posts: 59
Joined: October 12th, 2007, 4:15 pm
Location: Germany

Re: Get the movement_type

Post by Jovycos »

@Anonymissimus
Well, Lua is not that difficult if you have got experiences with some other languages.
Full of Nerdiness...
silene
Posts: 1109
Joined: August 28th, 2004, 10:02 pm

Re: Get the movement_type

Post by silene »

Anonymissimus wrote:By the way did you knew lua before implementing it in wesnoth ? You're so quick with its syntax, though supposedly untested, mostly correct...
Yes, I knew Lua, since it can be found in lots of applications, especially games. Though it had been quite some time since I last programmed with it. But as Jovycos said, except for some discrepancies in the syntax (thanks, fixed), once you know a high-level language, you are able to program in most of them. There aren't that many paradigm shifts between them. I could just have well written this code snippet in C++, Java, Python, Ruby, OCaml, and so on, and it would have been mostly correct too (even though I never programmed in some of these languages).
Post Reply