Page 1 of 1

Lua problem

Posted: Fri Sep 22, 2023 8:32 pm
by Sandra75
Hallo,
I like to create my own spanish civil war campaign with 5 scenarios. Inside the campaign I create an message in the first scenario that one of the Heros killed by an spion. Its possible to remove this core unit hero at the start of the next scenario with an Lua script and replace him with another ?

Thanks in advance!

Re: Lua problem

Posted: Fri Sep 22, 2023 9:21 pm
by Grondel
Sandra75 wrote: Fri Sep 22, 2023 8:32 pm Hallo,
I like to create my own spanish civil war campaign with 5 scenarios. Inside the campaign I create an message in the first scenario that one of the Heros killed by an spion. Its possible to remove this core unit hero at the start of the next scenario with an Lua script and replace him with another ?

Thanks in advance!
u can remove him the second he is killed if u want to.

sers,
Thomas

Re: Lua problem

Posted: Fri Sep 22, 2023 9:36 pm
by Sandra75
Grondel wrote: Fri Sep 22, 2023 9:21 pm
Sandra75 wrote: Fri Sep 22, 2023 8:32 pm Hallo,
I like to create my own spanish civil war campaign with 5 scenarios. Inside the campaign I create an message in the first scenario that one of the Heros killed by an spion. Its possible to remove this core unit hero at the start of the next scenario with an Lua script and replace him with another ?

Thanks in advance!
u can remove him the second he is killed if u want to.

sers,
Thomas
I tested out this here - but dont work:

function IsRodrigez(hero)
return hero.name == NSLOCTEXT("scenario_Madrid", "Tentente Rodrigez", "Tentente Rodrigez")
end

function FindAndUnassignRodrigez(player)
for _, hero in pairs(player.heroes) do
if IsRodrigez(hero) then
return hero.id
end
end
return -1
end

function RetireRodrigez()
local player = world:GetPlayer(0)
local Rodrigez_id = FindAndUnassignRodrigez(player)

if Rodrigez_id ~= -1 then
local dismiss_action = world:MakeDismissHeroAction(player.id, Rodrigez_id)
world:Exec(dismiss_action)
end
end

function ReplaceRodrigezWithDíaz()
RetireRodrigez()

local player = world:GetPlayer(0)

local hero = NewHero()
hero.portrait = "/CivilWar36-38/Gui/Common/Heroes/SP/Tentente_Díaz.Tentente_Díaz"
hero.name = NSLOCTEXT("scenario_Madrid", "Tentente Díaz", "Tentente Díaz")
hero.extra_traits = {UnitTrait.FirstStrike, UnitTrait.Leadership}
hero.unit_classes = {UnitClass.Infantry}

local new_hero_action = world:MakeNewHeroAction(player.id, hero)
world:Exec(new_hero_action)
end

Re: Lua problem

Posted: Fri Sep 22, 2023 9:48 pm
by Grondel
Sandra75 wrote: Fri Sep 22, 2023 9:36 pm function IsRodrigez(hero)
return hero.name == NSLOCTEXT("scenario_Madrid", "Tentente Rodrigez", "Tentente Rodrigez")
end
this won´t work.
the easiestway to search for heros is by a unique trait combination.

in the editor folder of the game is a file called properties.txt. in there u can see what u can acces via lua regarding heros.

sers,
Thomas

Re: Lua problem

Posted: Fri Sep 22, 2023 9:52 pm
by Grondel
Grondel wrote: Fri Sep 22, 2023 9:48 pm
Sandra75 wrote: Fri Sep 22, 2023 9:36 pm function IsRodrigez(hero)
return hero.name == NSLOCTEXT("scenario_Madrid", "Tentente Rodrigez", "Tentente Rodrigez")
end
this won´t work.
the easiestway to search for heros is by a unique trait combination.

in the editor folder of the game is a file called properties.txt. in there u can see what u can acces via lua regarding heros.

if u want to stick with the hero.name method print it via messagebox to check how it looks. my guess is that it would be like

function IsRodrigez(hero)
return hero.name == {"Tentente Rodrigez"}
end

but i haven´t tested that yet.

sers,
Thomas

Re: Lua problem

Posted: Fri Sep 22, 2023 9:52 pm
by Grondel
Grondel wrote: Fri Sep 22, 2023 9:52 pm function IsRodrigez(hero)
return hero.name == NSLOCTEXT("scenario_Madrid", "Tentente Rodrigez", "Tentente Rodrigez")
end
this won´t work.
the easiestway to search for heros is by a unique trait combination.

in the editor folder of the game is a file called properties.txt. in there u can see what u can acces via lua regarding heros.

if u want to stick with the hero.name method print it via messagebox to check how it looks. my guess is that it would be like

function IsRodrigez(hero)
return hero.name == {"Tentente Rodrigez"}
end

but i haven´t tested that yet.

sers,
Thomas

Re: Lua problem

Posted: Fri Sep 22, 2023 9:55 pm
by Sandra75
Grondel wrote: Fri Sep 22, 2023 9:48 pm
Sandra75 wrote: Fri Sep 22, 2023 9:36 pm function IsRodrigez(hero)
return hero.name == NSLOCTEXT("scenario_Madrid", "Tentente Rodrigez", "Tentente Rodrigez")
end
this won´t work.
the easiestway to search for heros is by a unique trait combination.

in the editor folder of the game is a file called properties.txt. in there u can see what u can acces via lua regarding heros.

sers,
Thomas
Thank you - I know! But can you give a short example - how to search for an Trait combination?
{UnitTrait.TenaciousDefender, UnitTrait.FastLearner} is the to remove hero.

Re: Lua problem

Posted: Fri Sep 22, 2023 10:55 pm
by Grondel
Sandra75 wrote: Fri Sep 22, 2023 9:55 pm Thank you - I know! But can you give a short example - how to search for an Trait combination?
{UnitTrait.TenaciousDefender, UnitTrait.FastLearner} is the to remove hero.
function FindItemInTable(table, item)
for index,i in ipairs(table) do
if i == item then
return index
end
end

return -1
end

function IsItemInTable(table, item)
return FindItemInTable(table, item) ~= -1
end

function findhero(hero)
if (not IsItemInTable(hero.extra_traits, UnitTrait.TenaciousDefender)) then return false end
if (not IsItemInTable(hero.extra_traits, UnitTrait.FastLearner)) then return false end
return true
end

this will find all heros with said trait combination

sers,
Thomas

Re: Lua problem

Posted: Sat Sep 23, 2023 12:10 am
by Sandra75
Thanks! This is my Lua script yet:

function FindItemInTable(table, item)
for index, i in ipairs(table) do
if i == item then
return index
end
end
return -1
end

function IsItemInTable(table, item)
return FindItemInTable(table, item) ~= -1
end

function FindHeroesWithTraits(heroes)
local heroesToRemove = {}

for _, hero in ipairs(heroes) do
if IsItemInTable(hero.extra_traits, UnitTrait.TenaciousDefender) and IsItemInTable(hero.extra_traits, UnitTrait.FastLearner) then
table.insert(heroesToRemove, hero.id)
end
end

return heroesToRemove
end

function EliminateHeroesWithTraits()
local playerID = 1
local heroes = GameWorld.GetPlayer(playerID).heroes
local heroesToRemove = FindHeroesWithTraits(heroes)

for _, heroID in ipairs(heroesToRemove) do
GameWorld.GetPlayer(playerID).DefeatHero(heroID)
end
end

function Turn0()
EliminateHeroesWithTraits()
end

Editor:
Trigger name: EraseHero
Count: 1
Lua function: Turn0
Actions: StartTurnAction
Lua function: EliminateHeroesWithTraits()

Any ideas ? - it dont work.

Re: Lua problem

Posted: Sat Sep 23, 2023 1:53 am
by Grondel
Sandra75 wrote: Sat Sep 23, 2023 12:10 am
Any ideas ? - it dont work.
not tested but should be closer to ur goal.

function FindItemInTable(table, item)
for index, i in ipairs(table) do
if i == item then
return index
end
end
return -1
end

function IsItemInTable(table, item)
return FindItemInTable(table, item) ~= -1
end

function FindHeroesWithTraits(heroes)
local heroesToRemove = {}

for _, hero in ipairs(heroes) do
if IsItemInTable(hero.extra_traits, UnitTrait.TenaciousDefender) and IsItemInTable(hero.extra_traits, UnitTrait.FastLearner) then
table.insert(heroesToRemove, hero.id)
end
end
return heroesToRemove
end

function EliminateHeroesWithTraits()
local playerID = 1
local heroes = GameWorld.GetPlayer(playerID).heroes
local heroesToRemove = FindHeroesWithTraits(heroes)

for _, heroID in ipairs(heroesToRemove) do
GameWorld.GetPlayer(playerID).DefeatHero(heroID)
end
end

function Turn0()
return World.round == 0
end

Editor:
Trigger name: EraseHero
Count: 1
Lua function: Turn0
Actions: StartTurnAction
Lua function: EliminateHeroesWithTraits()

sers,
Thomas

Re: Lua problem

Posted: Sat Sep 23, 2023 2:21 am
by Sandra75
Thank you - dont run! Hero isnt erase also!

This line causes errors: return World.round == 0
SCRIPT ERROR: [string'']: attempt to index a nil value (global 'gameWorld')

Any ideas ?

Re: Lua problem

Posted: Sat Sep 23, 2023 2:34 am
by Grondel
Sandra75 wrote: Sat Sep 23, 2023 2:21 am Thank you - dont run! Hero isnt erase also!

This line causes errors: return World.round == 0
SCRIPT ERROR: [string'']: attempt to index a nil value (global 'gameWorld')

Any ideas ?
change it to world.round == 0

sers,
Thomas

Re: Lua problem

Posted: Sat Sep 23, 2023 2:52 am
by Sandra75
Thanks! But...

function FindItemInTable(table, item)
for index, i in ipairs(table) do
if i == item then
return index
end
end
return -1
end

function IsItemInTable(table, item)
return FindItemInTable(table, item) ~= -1
end

function FindHeroesWithTraits(heroes)
local heroesToRemove = {}

for _, hero in ipairs(heroes) do
if IsItemInTable(hero.extra_traits, UnitTrait.TenaciousDefender) and IsItemInTable(hero.extra_traits, UnitTrait.FastLearner) then
table.insert(heroesToRemove, hero.id)
end
end
return heroesToRemove
end

function EliminateHeroesWithTraits()
local playerID = 1
local heroes = gameWorld.GetPlayer(playerID).heroes
local heroesToRemove = FindHeroesWithTraits(heroes)

for _, heroID in ipairs(heroesToRemove) do
gameWorld.GetPlayer(playerID).DefeatHero(heroID)
end
end

function Turn0()
return world.round == 0
end

SCRIPT ERROR: [string'']: Trigger function 'EliminateHeroesWithTraits()' not found

Editor:
Trigger name: EraseHero
Count: 1
Lua function: Turn0
Actions: StartTurnAction
Lua function: EliminateHeroesWithTraits()

Re: Lua problem

Posted: Sat Sep 23, 2023 2:56 am
by Grondel
Sandra75 wrote: Sat Sep 23, 2023 2:52 am
SCRIPT ERROR: [string'']: Trigger function 'EliminateHeroesWithTraits()' not found
change to Lua function: EliminateHeroesWithTraits

sers,
Thomas

Re: Lua problem

Posted: Sat Sep 23, 2023 3:28 am
by Sandra75
Sorry - dont work!

This line causes errors: local heroes = gameWorld.GetPlayer(playerID).heroes
SCRIPT ERROR: [string'']: attempt to index a nil value (global 'gameWorld')

Any ideas ?

Re: Lua problem

Posted: Sat Sep 23, 2023 5:09 am
by Grondel
Sandra75 wrote: Sat Sep 23, 2023 3:28 am Sorry - dont work!

This line causes errors: local heroes = gameWorld.GetPlayer(playerID).heroes
SCRIPT ERROR: [string'']: attempt to index a nil value (global 'gameWorld')

Any ideas ?
not sure where u got that line from.

if u have discord please add me as friend, my handle is :grondel

alot easier to help u there.

sers,
Thomas

Re: Lua problem

Posted: Sat Sep 23, 2023 3:12 pm
by Sandra75
Sorry - dont have discord. Its possible to help me here please.

Re: Lua problem

Posted: Sun Sep 24, 2023 4:26 am
by Kerensky
Grondel wrote: Sat Sep 23, 2023 5:09 am not sure where u got that line from.

if u have discord please add me as friend, my handle is :grondel

alot easier to help u there.

sers,
Thomas
You can try to talk to a brand new user who ends their user name with 2 numbers and immediately ask questions about modding, but I'm guessing this will be deleted as soon as the right people figure out who this is. Again.

Re: Lua problem

Posted: Sun Sep 24, 2023 4:39 am
by Grondel
Kerensky wrote: Sun Sep 24, 2023 4:26 am
Grondel wrote: Sat Sep 23, 2023 5:09 am not sure where u got that line from.

if u have discord please add me as friend, my handle is :grondel

alot easier to help u there.

sers,
Thomas
You can try to talk to a brand new user who ends their user name with 2 numbers and immediately ask questions about modding, but I'm guessing this will be deleted as soon as the right people figure out who this is. Again.
thats why i asked about discord :)

Re: Lua problem

Posted: Sun Sep 24, 2023 4:09 pm
by Sandra75
Kerensky wrote: Sun Sep 24, 2023 4:26 am
Grondel wrote: Sat Sep 23, 2023 5:09 am not sure where u got that line from.

if u have discord please add me as friend, my handle is :grondel

alot easier to help u there.

sers,
Thomas
You can try to talk to a brand new user who ends their user name with 2 numbers and immediately ask questions about modding, but I'm guessing this will be deleted as soon as the right people figure out who this is. Again.
Dont understand that - its a secret code? Sorry thats my year of birth - I have to change my user name without this numbers ?
BTW - I use Skype - its possible to communicate there ?