Dugi's lua questions
Moderator: Forum Moderators
Re: Dugi's lua questions
This was a lua table, created with
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 .
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.
Code: Select all
local unit=wesnoth.create_unit{ some properties }
Spoiler:
I need it to be converted into WML after some operations, so I need it to be compatible with WML.
Re: Dugi's lua questions
Have you tried
unit.variables.__cfg
? If the problem is helper.get_child
, then not using helper.get_child
should fix it.Re: Dugi's lua questions
That worked. Thank you.
New problem. A table named unit looks like this:
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?
New problem. A table named unit looks like this:
Code: Select all
{
...
race="human"
...
}
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?
Re: Dugi's lua questions
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.Re: Dugi's lua questions
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.
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.
Re: Dugi's lua questions
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.SP campaigns: Galuldur's First Journey (1.12 & 1.14) & Grnk the Mighty (1.10 & 1.12)
AI experiments: Micro AIs (wiki, forum thread, known/fixed bugs), Fred, AI-demos add-on
AI experiments: Micro AIs (wiki, forum thread, known/fixed bugs), Fred, AI-demos add-on
Re: Dugi's lua questions
Try this:
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.)
Code: Select all
wesnoth.unit_types[unit.type].__cfg.race
Re: Dugi's lua questions
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.
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.
- Alarantalara
- Art Contributor
- Posts: 786
- Joined: April 23rd, 2010, 8:17 pm
- Location: Canada
Re: Dugi's lua questions
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.
Re: Dugi's lua questions
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?
Re: Dugi's lua questions
It should look like this:
Or this:
But not like this:
Code: Select all
local u = wesnoth.create_unit {...}.__cfg
u.race = "..."
wesnoth.put_unit(u)
Code: Select all
local u = wesnoth.create_unit {...}
local cfg = u.__cfg
cfg.race = "..."
wesnoth.put_unit(cfg)
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)
Re: Dugi's lua questions
The first thing didn't work, but the second one worked perfectly. Thanks!
Re: Dugi's lua questions
Try parenthesizing the table constructor in the first example.
Re: Dugi's lua questions
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!
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!
Re: Dugi's lua questions
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?
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: