loading WML from a file into a table

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

Moderator: Forum Moderators

Post Reply
SigurdFireDragon
Developer
Posts: 546
Joined: January 12th, 2011, 2:18 am
Location: Pennsylvania, USA

loading WML from a file into a table

Post by SigurdFireDragon »

for my campaign I decided it would be useful if I could read the faction files in {multiplayer/factions} and put the contents of a [multiplayer_side] tag into a lua table, and then send the contents of some of the keys into wesnoth variables.

currently, I tried the following inside a WML [event]:

Code: Select all

[set_variable]
	name=Default_Era_Undead
	[value]
		{multiplayer/factions/undead-aoh.cfg}
	[/value]
[set_variable]

[lua]
	code=<<
		faction_table = wesnoth.get_variable("Default_Era_Undead")
		wesnoth.set_variable("faction_name", faction_table.id)
	>>
[/lua]
and then some WML code to put the contents of "faction_name" on the screen (should put "Undead" on the screen if it works (the contents of the 'id' key in the file read)

it gave the following error:
[string "..."]:3: attempt to index local 'faction_table', nil value
and then some tracebacks.

I'm getting the sense that the contents of undead-aoh.cfg were put into scalar instead of a table, but I'm not sure how to set it up for it to go into a table.

How would I do that?
Co-Author of Winds of Fate
My Add-ons: Random Campaign, Custom Campaign, Ultimate Random Maps, Era of Legends, Gui Debug Tools
Erfworld: The comic that lead me to find Wesnoth.
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: loading WML from a file into a table

Post by Sapient »

I think you meant to write set_variables, not set_variable.
http://www.wesnoth.org/wiki/User:Sapient... "Looks like your skills saved us again. Uh, well at least, they saved Soarin's apple pie."
SigurdFireDragon
Developer
Posts: 546
Joined: January 12th, 2011, 2:18 am
Location: Pennsylvania, USA

Re: loading WML from a file into a table

Post by SigurdFireDragon »

changing [set_variable] to [set_variables] got rid of the error code, but "faction_name" still isn't being loaded with the desired data. :hmm:
is the fact that I'm running this code under a SP Campaign have anything to do with anything?
Co-Author of Winds of Fate
My Add-ons: Random Campaign, Custom Campaign, Ultimate Random Maps, Era of Legends, Gui Debug Tools
Erfworld: The comic that lead me to find Wesnoth.
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: loading WML from a file into a table

Post by zookeeper »

You're forgetting that all the keys are inside a [multiplayer_side] tag. Presumably changing wesnoth.set_variable("faction_name", faction_table.id) to wesnoth.set_variable("faction_name", faction_table.multiplayer_side.id) would work.

However, you don't need Lua for that:

Code: Select all

[set_variables]
   name=Default_Era_Undead
   [value]
      {multiplayer/factions/undead-aoh.cfg}
   [/value]
[set_variables]

{VARIABLE faction_name $Default_Era_Undead.multiplayer_side.id}
SigurdFireDragon
Developer
Posts: 546
Joined: January 12th, 2011, 2:18 am
Location: Pennsylvania, USA

Re: loading WML from a file into a table

Post by SigurdFireDragon »

:doh: misreading the wiki caused me to think lua was necessary.

looks like what I want to do might be possible using only WML, this question should settle it:

I want:
Default_Era_Undead.multiplayer_side.image=units/undead-necromancers/lich.png
(or in some other variable)
so I can do:
message="&$Default_Era.multiplayer_side.image~RC(magenta>red)"
but i am running into problems because:
Default_Era_Undead.multiplayer_side.image="units/undead-necromancers/lich.png"
and it looks like there is no way to strip the " (double quotes) from the variable value. Do I need lua to strip the " ?
Co-Author of Winds of Fate
My Add-ons: Random Campaign, Custom Campaign, Ultimate Random Maps, Era of Legends, Gui Debug Tools
Erfworld: The comic that lead me to find Wesnoth.
Luther
Posts: 128
Joined: July 28th, 2007, 5:19 pm
Location: USA

Re: loading WML from a file into a table

Post by Luther »

As far as I know, there's no way to store an image in a variable. What are you trying to do, exactly?
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: loading WML from a file into a table

Post by zookeeper »

SigurdTheDragon wrote::doh: misreading the wiki caused me to think lua was necessary.

looks like what I want to do might be possible using only WML, this question should settle it:

I want:
Default_Era_Undead.multiplayer_side.image=units/undead-necromancers/lich.png
(or in some other variable)
so I can do:
message="&$Default_Era.multiplayer_side.image~RC(magenta>red)"
If you meant image="$Default_Era_Undead.multiplayer_side.image~RC(magenta>red)", then yes.
SigurdTheDragon wrote:but i am running into problems because:
Default_Era_Undead.multiplayer_side.image="units/undead-necromancers/lich.png"
and it looks like there is no way to strip the " (double quotes) from the variable value. Do I need lua to strip the " ?
Quotes simply enclose values, they're not part of them. You don't ever need to strip quotes.
SigurdFireDragon
Developer
Posts: 546
Joined: January 12th, 2011, 2:18 am
Location: Pennsylvania, USA

Re: loading WML from a file into a table

Post by SigurdFireDragon »

Ok, here's what was going wrong with my testing.

Code: Select all

	[set_variable]
		name=faction_pic
		value=$Default_Era_Undead.multiplayer_side.image
	[/set_variable]
	[message]
		speaker=narrator
		message="&$faction_pic~RC(magenta>red)=undead="
		[option]
			message="&$faction_pic~RC(magenta>red)=undead="
		[/option]
	[/message]
the first message key throws up a conains invalid wml error, the second one works as intended to display a pic. the "=" is necessary for the second to display properly.
I was just using the above minus the option tag to test the code form.
I had assumed that the message key in each place was identical in behavor. also for a while I didn't realize the = was needed

since how it works in the [option] tag is what I was looking to do, that solves my question.

I guess I didn't need lua after all. Thanks, everyone
Co-Author of Winds of Fate
My Add-ons: Random Campaign, Custom Campaign, Ultimate Random Maps, Era of Legends, Gui Debug Tools
Erfworld: The comic that lead me to find Wesnoth.
Post Reply