How to spawn in units mid-game?

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

Moderator: Panzer Corps 2 Moderators

Post Reply
ChristianC
Senior Corporal - Ju 87G
Senior Corporal - Ju 87G
Posts: 93
Joined: Mon Mar 04, 2019 11:55 am

How to spawn in units mid-game?

Post by ChristianC »

Hi, I'm seeking help with spawning units mid-game. I've taken a look around the forums on this subject, scenario lua files and manual - haven't figured it out. Can someone lend me assistance?

Just trying to spawn in a single tank at turn 2 for now to get a general understanding of this.

What I have at the moment in my scenario's lua file is the following:

function OnNewRound(action)
if world.round == 2 then

player = 1 -- I input because this is player 1 in the game. I tried "0" as well as I see is written in other scenario lua files, but this doesn't work either.
zone = { {55,36} } -- this hex is currently empty in my scenario
units = { {"M4Sherman", "", 0, 1500} }

SpawnWave(player, zone, units)

if not world.round == 2 then return end

end
end


From looking at other scenarios' lua files, I guess I should also make a trigger in the editor with the function? The screenshot below is what I have input currently.
Image


What should I do to get this to work?
Grondel
Sr. Colonel - Battleship
Sr. Colonel - Battleship
Posts: 1685
Joined: Mon Aug 04, 2014 10:07 pm

Re: How to spawn in units mid-game?

Post by Grondel »

ChristianC wrote: Fri May 26, 2023 7:11 pm Hi, I'm seeking help with spawning units mid-game. I've taken a look around the forums on this subject, scenario lua files and manual - haven't figured it out. Can someone lend me assistance?

Just trying to spawn in a single tank at turn 2 for now to get a general understanding of this.
Lua should look like this to make it reusable:

function isturn2() --<----put this in the editor in the trigger Condition and put the check in NewRoundAction
return world.round == 2
end

function spawntank()--<---put this in the editor in the trigger function
player = 0 --<-- when u open scenarioparameters->players tab this is the player on the left, count up from there
zone = {{1,1},{2,1},{3,1},{4,1},{5,1},{1,2},{2,2},{3,2},{4,2},{5,2},{6,2},{1,3},{2,3},{3,3},{4,3},{5,3},{1,4},{2,4},{3,4},{4,4},{5,4},{6,4}}--<---just a cupple of hexes to make sure one is empty
units = { {"M4Sherman", "", 0, 1500} }
SpawnWave(player, zone, units)
end

function SpawnWave(player, zone, units)
local owner = world:GetPlayer(player)
local cur_hex = 1
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 --<----------false if u want a core unit
create_action.cost = 0
create_action.faction = owner.factions[1]
world:Exec(create_action)
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
end


if u still run into issues just ask, did this from scratch and didn´t do a testrun. might have a flaw or two.

sers,
Thomas
ChristianC
Senior Corporal - Ju 87G
Senior Corporal - Ju 87G
Posts: 93
Joined: Mon Mar 04, 2019 11:55 am

Re: How to spawn in units mid-game?

Post by ChristianC »

Grondel wrote: Fri May 26, 2023 7:31 pm
Wow, thanks for your quick and comprehensive response. I've followed your instructions (seemingly), however, I receive in game this error: "SCRIPT ERROR: Condition check function 'isturn2()' not found" Below are some images of how I have things set up.

Image

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

Re: How to spawn in units mid-game?

Post by Grondel »

ChristianC wrote: Fri May 26, 2023 8:47 pm Wow, thanks for your quick and comprehensive response. I've followed your instructions (seemingly), however, I receive in game this error: "SCRIPT ERROR: Condition check function 'isturn2()' not found" Below are some images of how I have things set up.
the editor does not like brackets. :) remove them and it will work.

sers,
Thomas
ChristianC
Senior Corporal - Ju 87G
Senior Corporal - Ju 87G
Posts: 93
Joined: Mon Mar 04, 2019 11:55 am

Re: How to spawn in units mid-game?

Post by ChristianC »

Excellent, thanks a lot )
ChristianC
Senior Corporal - Ju 87G
Senior Corporal - Ju 87G
Posts: 93
Joined: Mon Mar 04, 2019 11:55 am

Re: How to spawn in units mid-game?

Post by ChristianC »

Hi, attempting to get my final product out of this but running into trouble - the game now crashes upon start of the scenario. I changed..

function isturn2() to function isturn10()

function spawntank() to function spawnreinforcements(); with a change of player, zones and units

added unit.name = u[5] to add names for the units

Any idea what's wrong?

Image

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

Re: How to spawn in units mid-game?

Post by Grondel »

condition and function are wrong. switch them.
ChristianC
Senior Corporal - Ju 87G
Senior Corporal - Ju 87G
Posts: 93
Joined: Mon Mar 04, 2019 11:55 am

Re: How to spawn in units mid-game?

Post by ChristianC »

Grondel wrote: Sat May 27, 2023 6:51 pm condition and function are wrong. switch them.
I see - I switched them, but unfortunately still getting the crash..
Grondel
Sr. Colonel - Battleship
Sr. Colonel - Battleship
Posts: 1685
Joined: Mon Aug 04, 2014 10:07 pm

Re: How to spawn in units mid-game?

Post by Grondel »

without looking at the files i can just do wild guesses what is causing the crash.

sers,
Thomas
ChristianC
Senior Corporal - Ju 87G
Senior Corporal - Ju 87G
Posts: 93
Joined: Mon Mar 04, 2019 11:55 am

Re: How to spawn in units mid-game?

Post by ChristianC »

Grondel wrote: Sat May 27, 2023 7:18 pm without looking at the files i can just do wild guesses what is causing the crash.

sers,
Thomas
If you'd like to, I have a link to the file below )

https://drive.google.com/file/d/14NzDBT ... sp=sharing
Grondel
Sr. Colonel - Battleship
Sr. Colonel - Battleship
Posts: 1685
Joined: Mon Aug 04, 2014 10:07 pm

Re: How to spawn in units mid-game?

Post by Grondel »

line 7 is a missing -->,<--- that is causing the issue.

if u go to:
https://rextester.com/l/lua_online_compiler

u can run ur scripts and easily find such issues.

sers,
Thomas
ChristianC
Senior Corporal - Ju 87G
Senior Corporal - Ju 87G
Posts: 93
Joined: Mon Mar 04, 2019 11:55 am

Re: How to spawn in units mid-game?

Post by ChristianC »

Great thanks :)
ChristianC
Senior Corporal - Ju 87G
Senior Corporal - Ju 87G
Posts: 93
Joined: Mon Mar 04, 2019 11:55 am

Re: How to spawn in units mid-game?

Post by ChristianC »

Hi, while this works in hot seat MP, a malfunction occurs when this is implemented in online MP...

When Axis hit end turn and a new round begins (the game switching to Allies, as the Allies move first on the round) the Axis reinforcement can be seen deploying onto the map as the game simultaneously uploads. However, then on next Axis turn the units are vanished.

I tried switching the "Actions:" to "StartTurnAction" - in this case the deployed units are visible for the next turn (immovable as I understand they should be for a turn) but then on the next turn they are vanished again.

Does anyone know how to fix this?

Code for reference:

Code: Select all

function isturn2()
return world.round == 2
end

function spawnreinforcements()
player = 1
zone = {{11,8},{12,8},{12,7},{12,6},{13,7},{13,8},{14,6},{14,7},{15,8},{10,7},{13,5},{12,5},{11,5}}
units = {{"TigerI", "", 0, 2000, "26th Panzer Division"},{"Flammpanzer38","", 0, 2000, "26th Panzer Division"},{"Hummel", "", 0, 2000, "26th Panzer Division"},{"Osttruppen", "OpelBlitz", 0, 2000, "162nd Turkestan Division"},{"Osttruppen", "OpelBlitz", 0, 2000, "162nd Turkestan Division"}}
SpawnWave(player, zone, units)
end

function SpawnWave(player, zone, units)
local owner = world:GetPlayer(player)
local cur_hex = 1
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 --<----------false if u want a core unit
create_action.cost = 0
create_action.faction = owner.factions[1]
world:Exec(create_action)
local unit = world:GetUnit(create_action.id)
deploy_action = world:MakeDeployAction(unit, zone[cur_hex])
world:Exec(deploy_action)
unit.experience = u[4]
unit.name = u[5]
player_aggressiveness = { 0, 85, 70, 70 }
unit.aggressiveness = player_aggressiveness[player+1]
end
end
Post Reply

Return to “Panzer Corps 2 Scenario Design”