Page 1 of 1

Group command doesn't work

Posted: Mon Feb 10, 2025 5:34 am
by tirabayt
Hello.
I'm developing a mod for Factorio (v2.0.32) where I create a large enemy unit group and issue commands for them to attack or move to specific coordinates (e.g., (0,0).

How do i send enemy at begin

Code: Select all

local attack_group = surface.create_unit_group { position = chosen_spawner.position, force = game.forces.enemy }
table.insert(storage.attack_groups, attack_group)   Add to storage to check in the tick

 local i = 0
    while i ~= 50 do
        local position_to_spawn = surface.find_non_colliding_position(biter_to_spawn, chosen_spawner.position, 0, 1)

        local enemy = surface.create_entity({
            name = biter_to_spawn,
            position = position_to_spawn,
            force = game.forces.enemy
        })

        if enemy ~= nil then
            attack_group.add_member(enemy)
            i = i + 1
        end
    end
    
      attack_group.set_command {
        type = defines.command.attack_area,
        destination = { x = 0, y = 0 },
        radius = 100,
        distraction = defines.distraction.by_enemy
    }
    
It works fine.
But when they meet some enemies and destroy them.
They finish thier command.
Then i trying to give them another command on tick. But they are finishing this command on next tick. And just stay afk. And after 20-30 seconds group destroyed.

Code: Select all

               
script.on_event(defines.events.on_tick, function(event)
    if event.tick % 600 == 0 then
        for index, attack_group in ipairs(storage.attack_groups) do
            if not attack_group.valid then
                goto continue_loop
            end

            if attack_group.valid and not attack_group.has_command then
                attack_group.set_command {
                    type = defines.command.go_to_location,
                    destination = { x = 0, y = 0 },
                    radius = 1,
                    distraction = defines.distraction.by_enemy
                }

            end

            ::continue_loop::
        end

        for i = #storage.attack_groups, 1, -1 do
            if not storage.attack_groups[i].valid then
                table.remove(storage.attack_groups, i)
            end
        end
    end
end)


So my question why they finish command so fast and do nothing.
destination is far away from them. I tried to use other type, radius, distraction.




UPD.
I kinda resolved it by setting compoun command with wander for 10 sec and then attack.

Code: Select all

 attack_group.set_command {
                    type = defines.command.compound,
                    structure_type = defines.compound_command.logical_and,
                    commands = {
                        {
                            type = defines.command.wander,
                            ticks_to_wait = 1000,
                        },
                        {
                            type = defines.command.attack_area,
                            destination = { x = 0, y = 0 },
                            radius = 100,
                            distraction = defines.distraction.by_enemy
                        },
                    },
                }
is it good solution?


UPD

In some cases solution above doesn't work.
reference to attack_group in my storage become invalid and biters just go away

Re: Group command doesn't work

Posted: Mon Feb 10, 2025 6:50 pm
by Muche
Welcome to the forum.

116916 LuaCommandable::set_command fails on next tick for attack command / 118082 Unit commands seems not behaving properly might be related.
Their fix went into v2.0.34, so I'd recommend to recheck your scenario with that version.