Lua question

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

Moderator: Panzer Corps 2 Moderators

Grondel
Sr. Colonel - Battleship
Sr. Colonel - Battleship
Posts: 1685
Joined: Mon Aug 04, 2014 10:07 pm

Lua question

Post by Grondel »

i´m trying to asign a hero to spawned units. this is the script:
--Patrullaazul
--Patrullaazul
string_azulspawn = NSLOCTEXT("scenario_JaramaRiver", "string_azulspawn", "Incoming radio:\nThis is Patrulla Azul. We are here to help. Awaiting your orders.")
azul_unit = {}


function IsTurn4()
return world.round == 4
end

function IsTurn5()
return world.round == 5
end




function SpawnAzul(action)
TutorialMessage(string_azulspawn)
local player = world:GetPlayer(0)
player = 0
zone = { {1,5},{1,6},{1,7},{1,8},{1,9},{1,10},{1,11},{1,12} }
units = { {"CR32", "", 0, 1500},{"CR32", "", 0, 1500},{"CR32", "", 0, 1500} }
local spawned_ids = SpawnWaveAUX(player, zone, units)
table.insert(azul_unit, table.remove(spawned_ids))
end


function AzulHero()
CreateAndAssignAzulHero1(table.remove(azul_unit))
CreateAndAssignAzulHero1(table.remove(azul_unit))
CreateAndAssignAzulHero1(table.remove(azul_unit))
end



function CreateAndAssignAzulHero1(unit_id)
local hero = NewHero()
hero.portrait = "/Game/Gui/Common/Heroes/generic_standin.generic_standin"
hero.name = NSLOCTEXT("scenario_JaramaRiver", "", "")
hero.extra_traits = {UnitTrait.AgressiveCounterattack, UnitTrait.LowProfile, UnitTrait.Survivor, UnitTrait.Prudent }
local action = world:MakeNewHeroAction(1, hero)
world:Exec(action)
local assign_hero_action = world:MakeAssignHeroAction(world:GetUnit(unit_id), hero.id)
world:Exec(assign_hero_action)
end

function SpawnWaveAUX(player, zone, units)
local owner = world:GetPlayer(player)
local cur_hex = 1
local spawned_ids = {}
for _,u in ipairs(units) do
while zone[cur_hex] do
hex = world:GetHex(zone[cur_hex])
if hex:GetUnit(0) == nil then break end
cur_hex = cur_hex + 1
end
if not zone[cur_hex] then return end
local create_action = world:MakePurchaseAction(owner, u[1], u[2], u[3])
create_action.auxiliary = true
create_action.cost = 0
world:Exec(create_action)
table.insert(spawned_ids, create_action.id)
local unit = world:GetUnit(create_action.id)
deploy_action = world:MakeDeployAction(unit, zone[cur_hex])
world:Exec(deploy_action)
unit.experience = u[4]
player_aggressiveness = { 0, 85, 70, 70 }
unit.aggressiveness = player_aggressiveness[player+1]
end
return spawned_ids
end


evrything is working fine, but this function:

function AzulHero()
CreateAndAssignAzulHero1(table.remove(azul_unit))
CreateAndAssignAzulHero1(table.remove(azul_unit))
CreateAndAssignAzulHero1(table.remove(azul_unit))
end

it leads to an unreal crash. Anyone have an idea whats wrong with it?

seers,
thomas
asuser
Sergeant Major - SdKfz 234/2 8Rad
Sergeant Major - SdKfz 234/2 8Rad
Posts: 628
Joined: Tue May 28, 2013 8:48 pm

Re: Lua question

Post by asuser »

Hello!

It seems to me, that you try 1 hero (the same) to 3 units. Could it be that you have to assign the heroes stepwise, like that:

The first Hero:

function SpawnAzul1(action)
TutorialMessage(string_azulspawn1)
local player = world:GetPlayer(0)
player = 0
zone = { {1,5},{1,6},{1,7} }
units = { {"CR32", "", 0, 1500} }
local spawned_ids = SpawnWaveAUX(player, zone, units)
table.insert(azul_unit, table.remove(spawned_ids))
end


function AzulHero1()
CreateAndAssignAzulHero1(table.remove(azul_unit))
end

function CreateAndAssignAzulHero1(unit_id)
local hero = NewHero()
hero.portrait = "/Game/Gui/Common/Heroes/generic_standin.generic_standin"
hero.name = NSLOCTEXT("scenario_JaramaRiver", "", "")
hero.extra_traits = {UnitTrait.AgressiveCounterattack, UnitTrait.LowProfile, UnitTrait.Survivor, UnitTrait.Prudent }
local action = world:MakeNewHeroAction(1, hero)
world:Exec(action)
local assign_hero_action = world:MakeAssignHeroAction(world:GetUnit(unit_id), hero.id)
world:Exec(assign_hero_action)
end


The second Hero:

function SpawnAzul2(action)
TutorialMessage(string_azulspawn2)
local player = world:GetPlayer(0)
player = 0
zone = { {1,8},{1,9},{1,10} }
units = { {"CR32", "", 0, 1500} }
local spawned_ids = SpawnWaveAUX(player, zone, units)
table.insert(azul_unit, table.remove(spawned_ids))
end

function AzulHero2()
CreateAndAssignAzulHero2(table.remove(azul_unit))
end

function CreateAndAssignAzulHero2(unit_id)
local hero = NewHero()
hero.portrait = "/Game/Gui/Common/Heroes/generic_standin.generic_standin"
hero.name = NSLOCTEXT("scenario_JaramaRiver", "", "")
hero.extra_traits = {UnitTrait.AgressiveCounterattack, UnitTrait.LowProfile, UnitTrait.Survivor, UnitTrait.Prudent }
local action = world:MakeNewHeroAction(2, hero)
world:Exec(action)
local assign_hero_action = world:MakeAssignHeroAction(world:GetUnit(unit_id), hero.id)
world:Exec(assign_hero_action)
end


The third Hero:


function SpawnAzul3(action)
TutorialMessage(string_azulspawn3)
local player = world:GetPlayer(0)
player = 0
zone = { {1,11},{1,12},{1,13} }
units = { {"CR32", "", 0, 1500} }
local spawned_ids = SpawnWaveAUX(player, zone, units)
table.insert(azul_unit, table.remove(spawned_ids))
end


function AzulHero3()
CreateAndAssignAzulHero3(table.remove(azul_unit))
end

function CreateAndAssignAzulHero3(unit_id)
local hero = NewHero()
hero.portrait = "/Game/Gui/Common/Heroes/generic_standin.generic_standin"
hero.name = NSLOCTEXT("scenario_JaramaRiver", "", "")
hero.extra_traits = {UnitTrait.AgressiveCounterattack, UnitTrait.LowProfile, UnitTrait.Survivor, UnitTrait.Prudent }
local action = world:MakeNewHeroAction(3, hero)
world:Exec(action)
local assign_hero_action = world:MakeAssignHeroAction(world:GetUnit(unit_id), hero.id)
world:Exec(assign_hero_action)
end


You could test it first with 1 hero, if successful you go to the next one, and so on ...
For every unit you can also use a own message (1,2,3) with the string function (string_azulspawn...).
Grondel
Sr. Colonel - Battleship
Sr. Colonel - Battleship
Posts: 1685
Joined: Mon Aug 04, 2014 10:07 pm

Re: Lua question

Post by Grondel »

asuser wrote: Tue Mar 30, 2021 9:04 am Hello!

It seems to me, that you try 1 hero (the same) to 3 units. Could it be that you have to assign the heroes stepwise, like that:

thx for ur anwser asuser, not what i needed but helped to find the error between my ears. ;)

working now
asuser
Sergeant Major - SdKfz 234/2 8Rad
Sergeant Major - SdKfz 234/2 8Rad
Posts: 628
Joined: Tue May 28, 2013 8:48 pm

Re: Lua question

Post by asuser »

Fine!

I also try with LUA and learned something. One question keeps me busy for a long time, how I can directly name a spawned unit. In the LUA manual there are two entries under "units" such as "name_id" and "name" for the unit type. But how do you create a spawned unit called "1th Bat. / 1th Arm. Div." to appear on the map without making the unit name manually on the unit information area? What is the LUA command for this?

I checked many LUA scripts from the DLC's but found nothing...
Grondel
Sr. Colonel - Battleship
Sr. Colonel - Battleship
Posts: 1685
Joined: Mon Aug 04, 2014 10:07 pm

Re: Lua question

Post by Grondel »

haven´t tried to name units yet.

should be something like this:

unit.name = NSLOCTEXT("scenario_JaramaRiver", "Azul3", "Azul3")

but i don´t know how exactly it needs to be done. maybe this helps.
asuser
Sergeant Major - SdKfz 234/2 8Rad
Sergeant Major - SdKfz 234/2 8Rad
Posts: 628
Joined: Tue May 28, 2013 8:48 pm

Re: Lua question

Post by asuser »

Grondel wrote: Tue Mar 30, 2021 11:39 am haven´t tried to name units yet.
should be something like this:
unit.name = NSLOCTEXT("scenario_JaramaRiver", "Azul3", "Azul3")
but i don´t know how exactly it needs to be done. maybe this helps.
Thanks for the suggestion ... nearly right.

I've tried 3 other ways and now I have it! Happy about. It was the second version ... :wink:
Grondel
Sr. Colonel - Battleship
Sr. Colonel - Battleship
Posts: 1685
Joined: Mon Aug 04, 2014 10:07 pm

Re: Lua question

Post by Grondel »

asuser wrote: Tue Mar 30, 2021 12:41 pm
Thanks for the suggestion ... nearly right.

I've tried 3 other ways and now I have it! Happy about. It was the second version ... :wink:
can u post it?
asuser
Sergeant Major - SdKfz 234/2 8Rad
Sergeant Major - SdKfz 234/2 8Rad
Posts: 628
Joined: Tue May 28, 2013 8:48 pm

Re: Lua question

Post by asuser »

Yes, shure.

Here comes a successful version for a spawned 280mm rail gun with random hex fields and the needed unit name "5th Art. Brigade":

1. First you have to define a new unit value [5] at the standard "SpawnWave" function, see here at step 3:

-- 3. Now modify the unit to set its experience to the value we need.
unit.experience = u[4]
unit.name = u[5]


2. Then you go to the unit characteristics part and write your unit name as the 5th value into the brackets:

player = 0
zone = {{7,1},{7,2},{6,3}}
units = { {"280mmK5", "", 0, Prototypes, "5th Art. Brigade"} }

That's it (1)!


It works also with the normal/easier "Deploy" function and the direct unit placement. Here you have add the command:

unit.name = "5th Art. Brigade"


That's it (2)!
asuser
Sergeant Major - SdKfz 234/2 8Rad
Sergeant Major - SdKfz 234/2 8Rad
Posts: 628
Joined: Tue May 28, 2013 8:48 pm

Re: Lua question

Post by asuser »

Covering the units with a special camouflage color is a very nice feature of the game.
Is there a way to assign a certain camouflage color to a unit via LUA command or to make
a spawned unit appear with a special camouflage color?

That would upgrade the game even more if you automatically highlight elite units through such actions!
Grondel
Sr. Colonel - Battleship
Sr. Colonel - Battleship
Posts: 1685
Joined: Mon Aug 04, 2014 10:07 pm

Re: Lua question

Post by Grondel »

since i don´t know how the camouflage is listed i currently switch the default camo to what i want the spawned units to have and manually adjust the units alrdy on the map.
a list of the camou for lua would be nice to have though.
asuser
Sergeant Major - SdKfz 234/2 8Rad
Sergeant Major - SdKfz 234/2 8Rad
Posts: 628
Joined: Tue May 28, 2013 8:48 pm

Re: Lua question

Post by asuser »

Another two interesting questions…

1.Does anybody try to give a spawned unit a special command like "move to hex" or "attack" or "hold position hexfield x,y"?

I saw some approaches in the Spain DLC where you can control the ai-nationalists forces.


2.Does anybody used or tested from the editor the objective "Leave map" or the special hexfield "shroud"? With that units could flee or be saved from the
scenario if they are in danger to eleminate or if an enemy unit keeps his life for a later mission…

I'm excited about some answers...
asuser
Sergeant Major - SdKfz 234/2 8Rad
Sergeant Major - SdKfz 234/2 8Rad
Posts: 628
Joined: Tue May 28, 2013 8:48 pm

Re: Lua question

Post by asuser »

asuser wrote: Wed Mar 31, 2021 1:55 pm Covering the units with a special camouflage color is a very nice feature of the game.
Is there a way to assign a certain camouflage color to a unit via LUA command or to make
a spawned unit appear with a special camouflage color?

That would upgrade the game even more if you automatically highlight elite units through such actions!
I tested with some variations:

unit.faction =
unit.camouflage =
unit.skins =

Sadly nothing works...

Anybody can help?
asuser
Sergeant Major - SdKfz 234/2 8Rad
Sergeant Major - SdKfz 234/2 8Rad
Posts: 628
Joined: Tue May 28, 2013 8:48 pm

Re: Lua question

Post by asuser »

Finally I found the right way... Yippie! :D

For tanks and artillery it works in this way:

unit.skin = "DE_Africa_Default"

With that a normal grey tank comes with sandcolored Africa camo.


For infantry you have to take that:

unit.skin = "DE_AfrikaKorps"


For further variations you have to go to the skins.csv file...
asuser
Sergeant Major - SdKfz 234/2 8Rad
Sergeant Major - SdKfz 234/2 8Rad
Posts: 628
Joined: Tue May 28, 2013 8:48 pm

Re: Lua question

Post by asuser »

Hello everybody!

I am looking for a LUA command, with that you can cover an empty hex field (landscape or desert) with a road after a certain round. Does anyone have any ideas about this? Is that possible with "ground_state" or something similar?
asuser
Sergeant Major - SdKfz 234/2 8Rad
Sergeant Major - SdKfz 234/2 8Rad
Posts: 628
Joined: Tue May 28, 2013 8:48 pm

Re: Lua question

Post by asuser »

Hello everybody!

With the LUA command package, you can give units various properties, such as names, experience and a skin/uniform. I'm looking for a LUA command with that you can assign a certain start orientation to a spawned unit, such as "north" or "south east", as it is also possible in the editor. I tried "unit.orientation =... ", unfortunately without success. Does anyone have better experiences with this?
SirAllan
Staff Sergeant - StuG IIIF
Staff Sergeant - StuG IIIF
Posts: 287
Joined: Thu Nov 03, 2016 5:24 pm

Re: Lua question

Post by SirAllan »

I can open the lua script file but dont know how and where to edit it, so I can get my missing CP's.
Does anyone know how to do it?
asuser
Sergeant Major - SdKfz 234/2 8Rad
Sergeant Major - SdKfz 234/2 8Rad
Posts: 628
Joined: Tue May 28, 2013 8:48 pm

Re: Lua question

Post by asuser »

Usually the core units are placed directly by the player at the beginning of a scenario … or not.
Is it possible to spawn own, not yet placed core units on the map using LUA commands? Or does that collide with the maximum possible number of unit slots?
nexusno2000
Sr. Colonel - Battleship
Sr. Colonel - Battleship
Posts: 1679
Joined: Mon Feb 24, 2014 5:15 pm

Re: Lua question

Post by nexusno2000 »

asuser wrote: Mon Sep 06, 2021 6:52 am Usually the core units are placed directly by the player at the beginning of a scenario … or not.
Is it possible to spawn own, not yet placed core units on the map using LUA commands? Or does that collide with the maximum possible number of unit slots?
Why not check out the Lua scripts that give you precisely such units?

The Verdeja tank is one such example. The PzIIA unit (also SCW) is another. At Saarbrücken you get S35 that's also above your core cap.
Green Knight
https://www.youtube.com/c/GreenKnight2001
dalfrede
Colonel - Fallschirmjäger
Colonel - Fallschirmjäger
Posts: 1462
Joined: Wed Mar 15, 2017 7:48 pm

Re: Lua question

Post by dalfrede »

asuser wrote: Mon Sep 06, 2021 6:52 am Is it possible to spawn own, not yet placed core units on the map using LUA commands? Or does that collide with the maximum possible number of unit slots?
If you spawn a unit during the Deployment Phase that unit will count against your core count,
as the spawning will occur before you place your units.

If you spawn on turn One [or later] it will not count against your core count.
There comes a time on every project when it is time to shoot the engineer and ship the damn thing.
asuser
Sergeant Major - SdKfz 234/2 8Rad
Sergeant Major - SdKfz 234/2 8Rad
Posts: 628
Joined: Tue May 28, 2013 8:48 pm

Re: Lua question

Post by asuser »

What is the correct command for a spawned unit to be landed from sea?
If you set a unit on a sea hex, this unit is "naked" and don't move.

unit.transport = "Landing Craft" does not work.

Maybe, because sea landing operations are still not of interest?
Post Reply

Return to “Panzer Corps 2 Scenario Design”