Page 1 of 1

Check in Entity Exists in Control

Posted: Mon Aug 14, 2023 5:10 pm
by TheSAguy
Hi,
I have a function in Control that randomly selects a unit to spawn. However, there is an option to remove enemies from the game, so they will no longer be valid entities in my table. How can I check to make sure they exist?

Here is my function with enemy possibilities that I need to check for. The "spawn" value.

Code: Select all

        function get_unit_to_spawn()
            local spawn_options = {
                {spawn = "ne-biter-breeder-", weight = 20},
                {spawn = "ne-biter-fire-", weight = 30},
                {spawn = "ne-biter-fast-", weight = 60},
                {spawn = "ne-biter-wallbreaker-", weight = 50},
                {spawn = "ne-biter-tank-", weight = 20},
                {spawn = "ne-spitter-breeder-", weight = 8},
                {spawn = "ne-spitter-fire-", weight = 3},
                {spawn = "ne-spitter-ulaunch-", weight = 2},
                {spawn = "ne-spitter-webshooter-", weight = 10},
                {spawn = "ne-spitter-mine-", weight = 10}
            }

            local calculate_odds = {}
            for k, spawn in ipairs(spawn_options) do
                for i = 1, spawn.weight do
                    calculate_odds[#calculate_odds + 1] = k
                end
            end

            local random_num = #calculate_odds
            return spawn_options[calculate_odds[math.random(random_num)]]

        end

        local unit_to_spawn = get_unit_to_spawn()
        SpawnLaunchedUnits(entity, unit_to_spawn)
Lets say "ne-spitter-mine-xxx" was removed from the game, how can I have the function re-roll till it gets a valid entity to spawn?

Thanks

Re: Check in Entity Exists in Control

Posted: Mon Aug 14, 2023 5:25 pm
by Silari
better way would be to assemble that table at startup and only fill it with an item if that prototype exists. Easier to do it once at startup then every time you need to spawn something.

Re: Check in Entity Exists in Control

Posted: Mon Aug 14, 2023 11:20 pm
by TheSAguy
Okay, not exactly sure how to do that.
I tried the following, but that gave me an error:

Code: Select all


NE_Enemies.Settings.NE_Biter_Breeder = settings.startup["NE_Biter_Breeder"].value
NE_Enemies.Settings.NE_Spitter_Breeder = settings.startup["NE_Spitter_Breeder"].value

NE_Enemies.Settings.NE_Biter_Fire = settings.startup["NE_Biter_Fire"].value
NE_Enemies.Settings.NE_Spitter_Fire = settings.startup["NE_Spitter_Fire"].value

NE_Enemies.Settings.NE_Biter_Fast = settings.startup["NE_Biter_Fast"].value
NE_Enemies.Settings.NE_Spitter_Ulaunch = settings.startup["NE_Spitter_Ulaunch"].value

NE_Enemies.Settings.NE_Biter_Wallbreaker = settings.startup["NE_Biter_Wallbreaker"].value
NE_Enemies.Settings.NE_Spitter_Webshooter = settings.startup["NE_Spitter_Webshooter"].value

NE_Enemies.Settings.NE_Biter_Tank = settings.startup["NE_Biter_Tank"].value
NE_Enemies.Settings.NE_Spitter_Mine = settings.startup["NE_Spitter_Mine"].value

        -- Randomly choose a unit to shoot - weighted
        function get_unit_to_spawn()
            local spawn_options = {
                NE_Enemies.Settings.NE_Biter_Breeder and
                    {spawn = "ne-biter-breeder-", weight = 20} or nil,
                NE_Enemies.Settings.NE_Biter_Fire and
                    {spawn = "ne-biter-fire-", weight = 30} or nil,
                NE_Enemies.Settings.NE_Biter_Fast and
                    {spawn = "ne-biter-fast-", weight = 60} or nil,
                NE_Enemies.Settings.NE_Biter_Wallbreaker and
                    {spawn = "ne-biter-wallbreaker-", weight = 50} or nil,
                NE_Enemies.Settings.NE_Biter_Tank and
                    {spawn = "ne-biter-tank-", weight = 20} or nil,
                NE_Enemies.Settings.NE_Spitter_Breeder and
                    {spawn = "ne-spitter-breeder-", weight = 8} or nil,
                NE_Enemies.Settings.NE_Spitter_Fire and
                    {spawn = "ne-spitter-fire-", weight = 3} or nil,
                NE_Enemies.Settings.NE_Spitter_Ulaunch and
                    {spawn = "ne-spitter-ulaunch-", weight = 2} or nil,
                NE_Enemies.Settings.NE_Spitter_Webshooter and
                    {spawn = "ne-spitter-webshooter-", weight = 10} or nil,
                NE_Enemies.Settings.NE_Spitter_Mine and
                    {spawn = "ne-spitter-mine-", weight = 10} or nil
            }

            local calculate_odds = {}
            for k, spawn in ipairs(spawn_options) do
                for i = 1, spawn.weight do
                    calculate_odds[#calculate_odds + 1] = k
                end
            end

            local random_num = #calculate_odds
            return spawn_options[calculate_odds[math.random(random_num)]]

        end

Error:

Code: Select all

The mod Natural Evolution Enemies (1.1.27) caused a non-recoverable error.
Please report this error to the mod author.

Error while running event Natural_Evolution_Enemies::on_trigger_created_entity (ID 24)
__Natural_Evolution_Enemies__/control.lua:2278: bad argument #1 of 1 to 'random' (interval is empty)
stack traceback:
	[C]: in function 'random'
	__Natural_Evolution_Enemies__/control.lua:2278: in function 'get_unit_to_spawn'
	__Natural_Evolution_Enemies__/control.lua:2282: in function <__Natural_Evolution_Enemies__/control.lua:2165>
Any thoughts on how to update this?
Thanks.

Re: Check in Entity Exists in Control

Posted: Wed Aug 16, 2023 4:29 pm
by TheSAguy
Hi,
Just seeing if anyone had any advice on how to check if a hard coded entity in a table exists, during the Control phase.
Thanks.

Re: Check in Entity Exists in Control

Posted: Wed Aug 16, 2023 5:08 pm
by Bilka
TheSAguy wrote: Wed Aug 16, 2023 4:29 pm how to check if a hard coded entity in a table exists, during the Control phase.

Code: Select all

if game.entity_prototypes[entity_name] then
-- do stuff
end

Re: Check in Entity Exists in Control

Posted: Wed Aug 16, 2023 8:48 pm
by TheSAguy
Thanks Bilka!