Dugi's lua questions

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

Moderator: Forum Moderators

User avatar
Dugi
Posts: 4960
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Dugi's lua questions

Post by Dugi »

This was a lua table, created with

Code: Select all

local unit=wesnoth.create_unit{ some properties }
probably not a metatable like Elvish_Hunter thought, because it displayed an error that something went wrong with helper.lua when I tried to use helper.get_child .
Spoiler:
And I needed to extract the numbers behind the IDs within the 'a' things.
I need it to be converted into WML after some operations, so I need it to be compatible with WML.
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Dugi's lua questions

Post by 8680 »

Have you tried unit.variables.__cfg? If the problem is helper.get_child, then not using helper.get_child should fix it.
User avatar
Dugi
Posts: 4960
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Dugi's lua questions

Post by Dugi »

That worked. Thank you.

New problem. A table named unit looks like this:

Code: Select all

{
...
  race="human"
...
}
But I cannot figure out how to get to the value of the race there (actually trying to save something there, but I cannot read it neither). I tried unit.race , unit[1].race , unit[race] , but without any results. How can I get to it?

A question about older things: If also the callback functions of the GUI' widgets make some variable changes that will affect the game, do I have to use the wesnoth.synchronize_choice function on all of them?
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Dugi's lua questions

Post by 8680 »

unit.race or unit["race"] would work if there is no other problem, which I assume there is because you tried the former to no avail.
User avatar
Dugi
Posts: 4960
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Dugi's lua questions

Post by Dugi »

Doesn't help neither. Maybe something is wrong with the table.
It was created with local unit=wesnoth.create_unit{ some properties } , then passed as an argument to a function where I tried to do something with it, does it mean that I should manipulate with that variable in a specific way?
There was no other manipulation with the variable unit after it was created.
mattsc
Developer
Posts: 1173
Joined: October 13th, 2010, 6:14 pm
Location: Hidden on the hex behind Fred

Re: Dugi's lua questions

Post by mattsc »

I can't test this right now, but AFAIK, race is only available through unit.__cfg from the unit proxy table. So it's unit._cfg.race or unit.__cfg["race"]. See LuaWML:Units for what you can access without going through __cfg.
Luther
Posts: 115
Joined: July 28th, 2007, 5:19 pm
Location: USA

Re: Dugi's lua questions

Post by Luther »

Try this:

Code: Select all

wesnoth.unit_types[unit.type].__cfg.race
Race is a property of the unit type, not the unit. (I included the __cfg field because the wiki doesn't list race as a field.)
User avatar
Dugi
Posts: 4960
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Dugi's lua questions

Post by Dugi »

Okay, now I can read the race, but I cannot change it.

I needed to alter the race of a unit as a direct modification. I can do it in WML with ease, just store_unit, set_variable unit.race to something else, unstore_unit, done. However, I tried to alter its x coordinate and it worked. For some reasons, unit's race is protected in lua, is there a way to transform that table into a normal table that is not tied to WML and does not prevent me from writing some things? I know I might change the race with wesnoth.fire, but I believe there is a more sophisticated way to do that.
User avatar
Alarantalara
Art Contributor
Posts: 784
Joined: April 23rd, 2010, 8:17 pm
Location: Canada

Re: Dugi's lua questions

Post by Alarantalara »

For variables other than those available through the proxy values and marked writable, you must call put_unit afterward (much like unstore_unit in WML). There may be other restrictions as well.
User avatar
Dugi
Posts: 4960
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Dugi's lua questions

Post by Dugi »

I have a put_unit afterward, the variable was created with unit=wesnoth.create_unit , I wanted to make some changes with it (including the change of race) between creating it into that variable and using put_unit to unstore it. But I have an impression that it's still marked as a proxy or something, is there a way to unmark it?
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Dugi's lua questions

Post by 8680 »

It should look like this:

Code: Select all

local u = wesnoth.create_unit {...}.__cfg
u.race = "..."
wesnoth.put_unit(u)
Or this:

Code: Select all

local u = wesnoth.create_unit {...}
local cfg = u.__cfg
cfg.race = "..."
wesnoth.put_unit(cfg)
But not like this:

Code: Select all

local u = wesnoth.create_unit {...}
u.__cfg.race = "..."
-- u.__cfg.race is set, but u.__cfg isn't saved anywhere, and u.race is not set.
wesnoth.put_unit(u)
User avatar
Dugi
Posts: 4960
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Dugi's lua questions

Post by Dugi »

The first thing didn't work, but the second one worked perfectly. Thanks!
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Dugi's lua questions

Post by 8680 »

Try parenthesizing the table constructor in the first example.
User avatar
Dugi
Posts: 4960
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Dugi's lua questions

Post by Dugi »

Doesn't work either. But the second example works, and suits my needs more than the first one anyway.

EDIT: Surprise, no question for this night, I got lucky!
Now I can proudly say that I have written 25 kilobytes of working lua code!
User avatar
Dugi
Posts: 4960
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Dugi's lua questions

Post by Dugi »

I came to a new problem, but this time not really a killing one.
I wanted to use wesnoth.fire to place an [item] with image="items/axe.png~CS(-50,0,0)", the name of the image was constructed from other variables, but I verified that it was assembled correctly. I chose wesnoth.fire because it had to have both an image and a halo, and I wanted it to be manipulatable with WML. The halo displayed correctly, but the image did not. Nothing in terminal output, I expected it to bash me for some image not found, but it did not.

Any ideas?
Spoiler:
Post Reply