Lua problem
Moderator: Panzer Corps 2 Moderators
Lua problem
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!
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
u can remove him the second he is killed if u want to.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!
sers,
Thomas
Re: Lua problem
I tested out this here - but dont work:Grondel wrote: ↑Fri Sep 22, 2023 9:21 pmu can remove him the second he is killed if u want to.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!
sers,
Thomas
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
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
Grondel wrote: ↑Fri Sep 22, 2023 9:48 pmthis 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
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
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
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
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.
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
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
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 ?
This line causes errors: return World.round == 0
SCRIPT ERROR: [string'']: attempt to index a nil value (global 'gameWorld')
Any ideas ?
Re: Lua problem
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()
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.
Re: Lua problem
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 ?
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
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
Sorry - dont have discord. Its possible to help me here please.
Re: Lua problem
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
Re: Lua problem
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 ?


