Utter lua consufion

Discussion of Lua and LuaWML support, development, and ideas.

Moderator: Forum Moderators

Post Reply
User avatar
Spannerbag
Posts: 86
Joined: December 18th, 2016, 6:14 pm

Utter lua consufion

Post by Spannerbag »

Hi,
it's can't-code-lua-guy again... :?

My orignal lua query was sorted in double quick time, thanks again to all who took the trouble to help. I made the fatal error fo fiddling with it and I can't work out how I broke it :annoyed:

This is the original version which works, followed by the WML that calls it:

Code: Select all

local helper = wesnoth.require "lua/helper"
function wesnoth.wml_actions.set_gold(cfg)
  local set_amount = tonumber(cfg.amount) or helper.wml_error "[set_gold] missing required amount= attribute."
  local sides = wesnoth.get_sides(cfg)
  for index, team in ipairs(sides) do
    team.gold = team.gold + set_amount
    if cfg.set == true and team.gold < set_amount then team.gold = set_amount end
  end
end

Code: Select all

[set_gold]
  side=1
  amount=100
  set=yes
[/set_gold]
I was planning to publish this on the add-ons server as a maybe helpful widget but stupidly thought I'd "improve" it first.

What I wanted to do was lose the set key but add an optional floor key.

Lua:

Code: Select all

local helper = wesnoth.require "lua/helper"
function wesnoth.wml_actions.set_gold(cfg)
  local set_amount = tonumber(cfg.amount) or helper.wml_error "[set_gold] missing required amount= attribute."
  local set_floor = tonumber(cfg.floor) or set_floor = tonumber(set_amount)
  local sides = wesnoth.get_sides(cfg)
  for index, team in ipairs(sides) do
    team.gold = team.gold + set_amount
    if team.gold < set_floor then team.gold = set_floor end
  end
end
Proposed WML:

Code: Select all

[set_gold]
  side=1
  amount=100
  floor=150
[/set_gold]
Logic:
Add amount to gold for specified side(s).
For each side if side.gold < floor then set side.gold = floor.

If floor omitted, set floor=amount

Seems simple, right?

When I run the new version I get <Invalid WML found> [set_gold] not supported :doh:

I've tried set_floor = tonumber(set_amount) and set_floor = set_amount but no change.
Adding/omitting floor makes no difference either.
Also purged cache and/or F5. Reboot made no difference either.

Both files have the same name so there's no change needed to _main.cfg.

Clearly I'm doing something stupid but I can't see what?
Any help/suggestions greatly appreciated!

But I only changed a few lines...

Cheers!
--Spannerbag
UMC Campaign: After EI
I suspect the universe is simpler than we think and stranger than we can know.
Luther
Posts: 127
Joined: July 28th, 2007, 5:19 pm
Location: USA

Re: Utter lua consufion

Post by Luther »

This line looks like a syntax error:

Code: Select all

local set_floor = tonumber(cfg.floor) or set_floor = tonumber(set_amount)
You can't do an assignment in the middle of an expression. Change it to this:

Code: Select all

local set_floor = tonumber(cfg.floor) or tonumber(set_amount)
See if that works.

Lua errors should show up both in the scenario chat and in the terminal if you start Wesnoth from the command line.
vghetto
Posts: 464
Joined: November 2nd, 2019, 5:12 pm

Re: Utter lua consufion

Post by vghetto »

Try the following:

Code: Select all

local set_floor = tonumber(cfg.floor) or set_amount
When I run the new version I get <Invalid WML found> [set_gold] not supported :doh:
Try to clear wesnoth cache.

Edit: oh, you already mentioned clearing the cache. I missed that. In that case I don't know, usually that error means the new tag was not loaded, and not that it crashed. I might be wrong.
User avatar
Spannerbag
Posts: 86
Joined: December 18th, 2016, 6:14 pm

Re: Utter lua consufion

Post by Spannerbag »

Hi Luther,

You're right first time again :D

Luther wrote: June 30th, 2021, 5:07 pm This line looks like a syntax error:

Code: Select all

local set_floor = tonumber(cfg.floor) or set_floor = tonumber(set_amount)
You can't do an assignment in the middle of an expression. Change it to this:

Code: Select all

local set_floor = tonumber(cfg.floor) or tonumber(set_amount)
See if that works.

Lua errors should show up both in the scenario chat and in the terminal if you start Wesnoth from the command line.
Seems to work fine under all code variants :)

Many thanks for taking the time to respond, much appreciated!

Cheers!
--Spannerbag
UMC Campaign: After EI
I suspect the universe is simpler than we think and stranger than we can know.
Post Reply