LightFighter's Lua Lab
Moderator: Forum Moderators
LightFighter's Lua Lab
I've experimented a bit with lua and came up with this macro/tag:
What happens in-game, is that it keeps showing me this error:
Can somebody tell me what I'm doing wrong?
Code: Select all
[lua]
code = <<
local function effects(f)
local filtered = wesnoth.get_units { x,y = f.x,y or id = f.id or type = f.type or error("~wml:[imagemod] expects a filter.", 0) }
wesnoth.add_modification(filtered, "object", { { "effect", { apply_to = "image_mod", replace = "f.image" } } { "effect", { apply_to = "ellipse", ellipse = "f.ellipse" } } } )
end
wesnoth.register_wml_action("imagemod", effects)
>>
[/lua]
Code: Select all
[string "..."]:3: '}' expected near '='
Last edited by Astoria on November 25th, 2010, 5:18 pm, edited 1 time in total.
Formerly known as the creator of Era of Chaos and maintainer of The Aragwaithi and the Era of Myths.
Re: Lua question
The 'x,y = f.x,y' is illegal. You would have to put in each element separately. I can post an example later.
EDIT:
Note that it's easier to put this all into a separate Lua file. It's a pain to edit Lua within WML.
I do not have 1.9 installed, so this code is very untested. I'm sure there are errors.
Let us know how it works.
EDITED to add another comment.
EDITED to fix a syntax error.
EDIT:
Note that it's easier to put this all into a separate Lua file. It's a pain to edit Lua within WML.
I do not have 1.9 installed, so this code is very untested. I'm sure there are errors.
Code: Select all
--Make the code a bit shorter
H = wesnoth.require('lua/helper.lua')
T = H.set_wml_tag_metatable{}
local function effects(f)
--Use a standard unit filter provided by the user. You might be able to pass
--f directly to get_units, but I would require a [filter] tag to be safe. You
--don't need an error message because a nil filter simply catches all units.
local filtered = wesnoth.get_units(H.get_child(f, 'filter'))
--add_modification can only work on one unit at a time.
for i, unit in ipairs(filtered) do
wesnoth.add_modification(
unit,
'object',
{
--Take advantage of set_wml_tag_metatable, which was called at the top.
T.effect{
apply_to = "image_mod",
--You probably want a string provided by the user, not a string
--literal.
replace = f.image
},
T.effect{
apply_to = "ellipse",
ellipse = f.ellipse
}
}
)
end
end
wesnoth.register_wml_action("imagemod", effects)
EDITED to add another comment.
EDITED to fix a syntax error.
Last edited by Luther on October 23rd, 2010, 5:11 pm, edited 2 times in total.
Re: Lua question
That also doesn't work, same sort of error.
Code: Select all
[string "..."]:7: '}' expected near 'T'
Formerly known as the creator of Era of Chaos and maintainer of The Aragwaithi and the Era of Myths.
Re: Lua question
I forgot to put a comma between the two T tables. I've edited the code in my post. Try it again.
Re: Lua question
Thanks, it worked!
Formerly known as the creator of Era of Chaos and maintainer of The Aragwaithi and the Era of Myths.
Re: Lua question
Another quick question: Is there a way to make an #ifdef in lua?
Formerly known as the creator of Era of Chaos and maintainer of The Aragwaithi and the Era of Myths.
-
- Inactive Developer
- Posts: 2461
- Joined: August 15th, 2008, 8:46 pm
- Location: Germany
Re: Lua question
No. Lua isn't preprocessed.
EDIT
Well, if you "embed" it into wml it is, or can be, but it is very questionable whether that makes sense at all. You need to match the syntaxes of both wml and lua and must write lua as if it was wml which is pointless.
EDIT
Well, if you "embed" it into wml it is, or can be, but it is very questionable whether that makes sense at all. You need to match the syntaxes of both wml and lua and must write lua as if it was wml which is pointless.
projects (BfW 1.12):
A Simple Campaign: campaign draft for wml starters • Plan Your Advancements: mp mod
The Earth's Gut: sp campaign • Settlers of Wesnoth: mp scenario • Wesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
A Simple Campaign: campaign draft for wml starters • Plan Your Advancements: mp mod
The Earth's Gut: sp campaign • Settlers of Wesnoth: mp scenario • Wesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
Re: LightFighter's Lua Lab
Finished the (ugly) quantity variable stacker:
It is really simple, but it should work.
Code: Select all
function quantity(cfg)
if easy
then
wesnoth.set_variable("quantity", cfg.easy)
end
if medium
then
wesnoth.set_variable("quantity", cfg.medium)
end
if hard
then
wesnoth.set_variable("quantity", cfg.hard)
end
end
wesnoth.register_wml_action("quantity", quantity)
Code: Select all
[event]
name=preload
first_time_only=no
#workarounds here
#ifdef EASY
{VARIABLE easy yes}
#endif
#ifdef MEDIUM
{VARIABLE medium yes}
#endif
#ifdef HARD
{VARIABLE hard yes}
#endif
{WML_TAGS}
[/event]
Formerly known as the creator of Era of Chaos and maintainer of The Aragwaithi and the Era of Myths.
Re: LightFighter's Lua Lab
That only allows you three difficulty levels.
Northern Rebirth, for one, uses 4. It needs its own macros because of that, but now it can't use this tag either...
Northern Rebirth, for one, uses 4. It needs its own macros because of that, but now it can't use this tag either...
Re: LightFighter's Lua Lab
AI wrote:That only allows you three difficulty levels.
Northern Rebirth, for one, uses 4. It needs its own macros because of that, but now it can't use this tag either...
Code: Select all
function quantity(cfg)
if easy
then
wesnoth.set_variable("quantity", cfg.easy)
end
if medium
then
wesnoth.set_variable("quantity", cfg.medium)
end
if hard
then
wesnoth.set_variable("quantity", cfg.hard)
end
if nightmare
then
wesnoth.set_variable("quantity", cfg.nightmare)
end
end
wesnoth.register_wml_action("quantity", quantity)
Code: Select all
[event]
name=preload
first_time_only=no
#workarounds here
#ifdef EASY
{VARIABLE easy yes}
#endif
#ifdef MEDIUM
{VARIABLE medium yes}
#endif
#ifdef HARD
{VARIABLE hard yes}
#endif
#ifdef NIGHTMARE
{VARIABLE nightmare yes}
#endif
{WML_TAGS}
[/event]
Code: Select all
[quantity]
easy=10
medium=20
hard=30
[/quantity]
[gold]
side=1
amount=$quantity
[/gold]
Formerly known as the creator of Era of Chaos and maintainer of The Aragwaithi and the Era of Myths.
Re: LightFighter's Lua Lab
Yes, but what if someone wants to use 5 difficulties, or different difficulty DEFINEs? Should they have to replace the tag or craft a macro to work around its shortcomings?
Re: LightFighter's Lua Lab
Yes, they have to, but this system will allow UMC designers to make another type of difficulty system, like making formulas.AI wrote:Yes, but what if someone wants to use 5 difficulties, or different difficulty DEFINEs? Should they have to replace the tag or craft a macro to work around its shortcomings?
Formerly known as the creator of Era of Chaos and maintainer of The Aragwaithi and the Era of Myths.
Re: LightFighter's Lua Lab
IIRC, Invasion from the Unknown uses a macro of this sort without any Lua.
Theoretically you could add:
#ifdef NIGHTMARE
#define DIFF _ON_EASY _ON_NORMAL _ON_HARD _ON_NIGHTMARE
{_ON_NIGHTMARE}
and alter the others accordingly. Usage:
{VARIABLE monkeys ({DIFF 12 14 16})}
or
[event]
name=turn {DIFF 2 4 6}
etc
Code: Select all
#ifdef EASY
#define DIFF _ON_EASY _ON_NORMAL _ON_HARD
{_ON_EASY}#enddef
#endif
#ifdef NORMAL
#define DIFF _ON_EASY _ON_NORMAL _ON_HARD
{_ON_NORMAL}#enddef
#endif
#ifdef HARD
#define DIFF _ON_EASY _ON_NORMAL _ON_HARD
{_ON_HARD}#enddef
#endif
#ifdef NIGHTMARE
#define DIFF _ON_EASY _ON_NORMAL _ON_HARD _ON_NIGHTMARE
{_ON_NIGHTMARE}
and alter the others accordingly. Usage:
{VARIABLE monkeys ({DIFF 12 14 16})}
or
[event]
name=turn {DIFF 2 4 6}
etc
Re: LightFighter's Lua Lab
You can make it even simpler.LightFighter wrote:It is really simple, but it should work.
Code: Select all
#ifdef EASY
{VARIABLE difficulty easy}
#endif
...
wesnoth.register_wml_action("quantity", function(cfg)
wesnoth.set_variable("quantity", cfg[wesnoth.get_variable "difficulty"])
end)
Re: LightFighter's Lua Lab
New code (thank you silene!)
Usage:
Code: Select all
wesnoth.register_wml_action("quantity", function(cfg)
wesnoth.set_variable("cfg.save_in", cfg[wesnoth.get_variable "difficulty"])
end)
Code: Select all
[quantity]
save_in=foo
easy=b
medium=a
hard=r
[/quantity]
Formerly known as the creator of Era of Chaos and maintainer of The Aragwaithi and the Era of Myths.