Lua problem

A forum to discuss custom scenarios, campaigns and modding in general.

Moderator: Panzer Corps 2 Moderators

Locked
Sandra75
Lance Corporal - Panzer IA
Lance Corporal - Panzer IA
Posts: 10
Joined: Fri Sep 22, 2023 8:25 pm

Lua problem

Post 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!
Grondel
Brigadier-General - 15 cm Nblwf 41
Brigadier-General - 15 cm Nblwf 41
Posts: 1997
Joined: Mon Aug 04, 2014 10:07 pm

Re: Lua problem

Post 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
Sandra75
Lance Corporal - Panzer IA
Lance Corporal - Panzer IA
Posts: 10
Joined: Fri Sep 22, 2023 8:25 pm

Re: Lua problem

Post 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
Grondel
Brigadier-General - 15 cm Nblwf 41
Brigadier-General - 15 cm Nblwf 41
Posts: 1997
Joined: Mon Aug 04, 2014 10:07 pm

Re: Lua problem

Post 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
Grondel
Brigadier-General - 15 cm Nblwf 41
Brigadier-General - 15 cm Nblwf 41
Posts: 1997
Joined: Mon Aug 04, 2014 10:07 pm

Re: Lua problem

Post 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
Grondel
Brigadier-General - 15 cm Nblwf 41
Brigadier-General - 15 cm Nblwf 41
Posts: 1997
Joined: Mon Aug 04, 2014 10:07 pm

Re: Lua problem

Post 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
Sandra75
Lance Corporal - Panzer IA
Lance Corporal - Panzer IA
Posts: 10
Joined: Fri Sep 22, 2023 8:25 pm

Re: Lua problem

Post 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.
Grondel
Brigadier-General - 15 cm Nblwf 41
Brigadier-General - 15 cm Nblwf 41
Posts: 1997
Joined: Mon Aug 04, 2014 10:07 pm

Re: Lua problem

Post 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
Sandra75
Lance Corporal - Panzer IA
Lance Corporal - Panzer IA
Posts: 10
Joined: Fri Sep 22, 2023 8:25 pm

Re: Lua problem

Post 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.
Grondel
Brigadier-General - 15 cm Nblwf 41
Brigadier-General - 15 cm Nblwf 41
Posts: 1997
Joined: Mon Aug 04, 2014 10:07 pm

Re: Lua problem

Post 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
Sandra75
Lance Corporal - Panzer IA
Lance Corporal - Panzer IA
Posts: 10
Joined: Fri Sep 22, 2023 8:25 pm

Re: Lua problem

Post 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 ?
Grondel
Brigadier-General - 15 cm Nblwf 41
Brigadier-General - 15 cm Nblwf 41
Posts: 1997
Joined: Mon Aug 04, 2014 10:07 pm

Re: Lua problem

Post 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
Sandra75
Lance Corporal - Panzer IA
Lance Corporal - Panzer IA
Posts: 10
Joined: Fri Sep 22, 2023 8:25 pm

Re: Lua problem

Post 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()
Last edited by Sandra75 on Sat Sep 23, 2023 2:58 am, edited 3 times in total.
Grondel
Brigadier-General - 15 cm Nblwf 41
Brigadier-General - 15 cm Nblwf 41
Posts: 1997
Joined: Mon Aug 04, 2014 10:07 pm

Re: Lua problem

Post 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
Sandra75
Lance Corporal - Panzer IA
Lance Corporal - Panzer IA
Posts: 10
Joined: Fri Sep 22, 2023 8:25 pm

Re: Lua problem

Post 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 ?
Grondel
Brigadier-General - 15 cm Nblwf 41
Brigadier-General - 15 cm Nblwf 41
Posts: 1997
Joined: Mon Aug 04, 2014 10:07 pm

Re: Lua problem

Post 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
Sandra75
Lance Corporal - Panzer IA
Lance Corporal - Panzer IA
Posts: 10
Joined: Fri Sep 22, 2023 8:25 pm

Re: Lua problem

Post by Sandra75 »

Sorry - dont have discord. Its possible to help me here please.
Kerensky
Content Designer
Content Designer
Posts: 8624
Joined: Wed Jan 12, 2011 2:12 am

Re: Lua problem

Post 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.
Grondel
Brigadier-General - 15 cm Nblwf 41
Brigadier-General - 15 cm Nblwf 41
Posts: 1997
Joined: Mon Aug 04, 2014 10:07 pm

Re: Lua problem

Post 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 :)
Sandra75
Lance Corporal - Panzer IA
Lance Corporal - Panzer IA
Posts: 10
Joined: Fri Sep 22, 2023 8:25 pm

Re: Lua problem

Post 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 ?
Locked

Return to “Panzer Corps 2 Scenario Design”