Page 1 of 1

Change unit orders

Posted: Tue Jun 11, 2024 3:50 pm
by maczathras
In a scenario, I'm trying to set up two sets of unit orders - the first that covers the first several turns, then a second set once a turn-based trigger fires.

I've attached the unit's AI configuration (for the Axis player, #1). This is the LUA code from the script

Code: Select all

turn12Message = NSLOCTEXT("scenario_KasserinePass","","The Eighth Army's lead elements have finally reached Medenine, only a few kilometres from the Mareth Line. The Germans have no other option than to call off this attack. Pursue the enemy and recapture Thelepte, Kasserine and Sbeitla in case they fell into enemy hands.")
function OnTurn12()
    for id,unit in pairs(world.units) do
        if unit.owner_id == 0 then
            local orders = unit.orders
            local newOrders = {};
            local i = 2
            while(orders[i]) do
                newOrders.append(orders[i])
            end
            unit.orders = newOrders
        end
    end

    TutorialMessage(turn12Message)
end
I know the trigger fires, but I cannot tell if I have a Lua error or if I'm setting the new orders correctly.

Is there something obvious I'm missing? Is there a better way to change unit orders that isn't documented in the API?

Re: Change unit orders

Posted: Sat Jul 06, 2024 6:58 pm
by maczathras
I'm still having issue with changing unit orders. I've looked at several scenarios where this happens, and I'm not seeing what I'm going wrong.

I've uploaded a copy of the scenario, in case someone has time to see if I've messed up something obvious.

https://www.mediafire.com/file/bmojt20q ... 6.zip/file

Thank you.

Re: Change unit orders

Posted: Sat Jul 06, 2024 9:41 pm
by Grondel
maczathras wrote: Tue Jun 11, 2024 3:50 pm In a scenario, I'm trying to set up two sets of unit orders - the first that covers the first several turns, then a second set once a turn-based trigger fires.

I've attached the unit's AI configuration (for the Axis player, #1). This is the LUA code from the script

Code: Select all

turn12Message = NSLOCTEXT("scenario_KasserinePass","","The Eighth Army's lead elements have finally reached Medenine, only a few kilometres from the Mareth Line. The Germans have no other option than to call off this attack. Pursue the enemy and recapture Thelepte, Kasserine and Sbeitla in case they fell into enemy hands.")
function OnTurn12()
    for id,unit in pairs(world.units) do
        if unit.owner_id == 0 then
            local orders = unit.orders
            local newOrders = {};
            local i = 2
            while(orders[i]) do
                newOrders.append(orders[i])
            end
            unit.orders = newOrders
        end
    end

    TutorialMessage(turn12Message)
end
I know the trigger fires, but I cannot tell if I have a Lua error or if I'm setting the new orders correctly.

Is there something obvious I'm missing? Is there a better way to change unit orders that isn't documented in the API?
This looks like a LUA written by either someone who knows LUA but has zero clue about PC2 specific LUA or chatGPT.
U are using several "commands" that just do not exist in PC2 specific LUA.
Here is an example for a function that will remove the first command of a list of units.
Image

sers,
Thomas

Re: Change unit orders

Posted: Mon Jul 08, 2024 4:01 pm
by maczathras
I had rewritten the logic in the Lua script since my original post. I've also modified the first order, in case the use of Default for the first order was causing the issue.

Looking at the Lua code you provided, and my updated code, the only difference I see is that I didn't use parentheses in the if statement. I based the new code on the Orlik_active() method in the PanzerCorps 1 scenario "Bydgoszc"

Re: Change unit orders

Posted: Mon Jul 08, 2024 7:07 pm
by maczathras
I freely admit that Lua isn't a programming language I'm comfortable wit. I isn't my first, or even tenth programming language, but I'm not the best with scripting languages, even with the Lua manual.

Re: Change unit orders

Posted: Mon Jul 08, 2024 7:32 pm
by Grondel
maczathras wrote: Mon Jul 08, 2024 7:07 pm I freely admit that Lua isn't a programming language I'm comfortable wit. I isn't my first, or even tenth programming language, but I'm not the best with scripting languages, even with the Lua manual.
LUA for PC2 is very diffrent than LUA for any other programm since all but the basic LUA commands are derived from the C++ the main programm, in our case PC2, is written in. since we have no acces to that tzhe only available hints are in the Properties.txt u can find in the editor folder of the game. if u are used to programming this should help a lot.

in general in PC2 u need 2 LUA functions to fire a script. 1 trigger and 1 execute.
In ur case the trigger would look like this:
function IsTurn12()
return world.round == 12
end
All it does is check the condition and if it´s met fire the execute.
U put it here in the editor:
Image

Then u have the execute function, like i posted earlier and put it here:
Image

Then u set the editor to check evry turn if the condition is met:
Image

like this on turn 12 the first order will be removed from the order list of the unit.


Those 2 lines a gibberish. variable "orders" is not set anywhere. it could be unit.orders, but i doubt that append is a viable command here.

while(orders) do
newOrders.append(orders)

hope this helps.

sers,
Thomas

Re: Change unit orders

Posted: Mon Jul 08, 2024 7:51 pm
by maczathras
The trigger is set up properly, and fires as expected. This is the current code dealing with the order changes. The removal of the air units works successfully, as does the final tutorial message. Only the order change is failing.

Code: Select all

function Turn12Condition()
    return world.round == 1
end

turn12Message = NSLOCTEXT("scenario_KasserinePass","","The Eighth Army's lead elements have finally reached Medenine, only a few kilometres from the Mareth Line. The Germans have no other option than to call off this attack. Pursue the enemy and recapture Thelepte, Kasserine and Sbeitla in case they fell into enemy hands.")
axis_ground = {25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,51,52,53,54,55,60,61,62,63,64,65,67,69,70}
axis_air = {0,1,2,3,4,5,6}
function OnTurn12()

[color=#0000BF]	for _,unit_id in ipairs(axis_ground) do
		local unit = world:GetUnit(unit_id) 
        if (unit ~= nil) then
			local orders = Copy(unit.orders) 
			table.remove(orders, 1) 
			unit.orders = orders 
        end
    end[/color]

    for _, unit_id in ipairs(axis_air) do
        Disbandunit(unit_id)
    end

    TutorialMessage(turn12Message)
end

function Disbandunit(unit_id)
    local unit = world:GetUnit(unit_id)
    if unit ~= null then
        if unit:HasPlace() then
            local disband_action = world:MakeDisbandAction(unit)
            world:Exec(disband_action)
        end
    end
end

Re: Change unit orders

Posted: Mon Jul 08, 2024 11:22 pm
by Grondel
maczathras wrote: Mon Jul 08, 2024 7:51 pm

Code: Select all

function Turn12Condition()
    return world.round == 2
end
Just a hunch. try this and let me know if it works now.

sers,
Thomas

Re: Change unit orders

Posted: Tue Jul 09, 2024 4:19 pm
by maczathras
I tried it, and the orders haven't changed even by turn 5. Thank you for the idea.

In an odd way, I feel better that I didn't miss something completely obvious when someone like you looks over my scenario.

Re: Change unit orders

Posted: Tue Jul 09, 2024 5:04 pm
by Grondel
maczathras wrote: Tue Jul 09, 2024 4:19 pm I tried it, and the orders haven't changed even by turn 5. Thank you for the idea.

In an odd way, I feel better that I didn't miss something completely obvious when someone like you looks over my scenario.
hard to say what might be wrong. if u want provide the files and i have a look.

sers,
Thomas

Re: Change unit orders

Posted: Tue Jul 09, 2024 5:48 pm
by maczathras
This is the latest copy of the scenario, with the last recommended change.

https://www.mediafire.com/file/xlsj3ylg ... 9.zip/file

Re: Change unit orders

Posted: Tue Jul 09, 2024 6:15 pm
by Grondel
maczathras wrote: Tue Jul 09, 2024 5:48 pm This is the latest copy of the scenario, with the last recommended change.

https://www.mediafire.com/file/xlsj3ylg ... 9.zip/file
Image

if the count is set to 1 it will only check it once. if u set it to 0 it will check evry turn.

Let me know if this was it.

sers,
Thomas

Re: Change unit orders

Posted: Wed Jul 10, 2024 12:43 am
by maczathras
The trigger has been firing, and I get the tutorial message at the end of the function. Setting the count to 0 still didn't change the orders.

Re: Change unit orders

Posted: Wed Jul 10, 2024 1:08 am
by Grondel
maczathras wrote: Wed Jul 10, 2024 12:43 am The trigger has been firing, and I get the tutorial message at the end of the function. Setting the count to 0 still didn't change the orders.
Image
working fine for me. if u want the units to only move and do nothing else check those.
Image

sers,
Thomas