Cannot clone various projectile entities

Bugs that are actually features.
unhott
Inserter
Inserter
Posts: 30
Joined: Tue Jan 09, 2018 3:01 am
Contact:

Cannot clone various projectile entities

Post by unhott »

Minimum replication:

mod

Code: Select all

-- data-final-fixes.lua

--Add a script effect for the following: 
capsules = {"grenade", "cluster-grenade", "poison-capsule", "slowdown-capsule", "defender-capsule", "distractor-capsule", "destroyer-capsule"}


for _, capsule in pairs(capsules) do
    data.raw.projectile[capsule].created_effect = {
        {
            type = 'direct',
            action_delivery = 
                {
                type = 'instant',
                source_effects = 
                    {
                        type = 'script',
                        effect_id = 'test-script-trigger',
                    }
                }
        }
    }
    
end

--end of data-final-fixes.lua

Code: Select all

--control.lua 

script.on_event({defines.events.on_script_trigger_effect}, function(event)
    if event.effect_id ~= "test-script-trigger" then return end
    local entity = event.source_entity
    game.print(entity.name .. " " .. entity.type .. " " .. serpent.block(entity.position) .. " " ) 
    -- verifies that the entity is a grenade projectile, for example, with position data 
    local cloned_entity = entity.clone{position=entity.position, surface=entity.surface, force=entity.force}
    game.print(cloned_entity) -- prints nil
end)

--end of control.lua
I have tested this for all the projectiles here. The event triggers. The entity appears successfully. it just fails to clone.

Edit:
Thanks to some assistance we were able to discover that luaEntity.clone is hardcoded to not work with projectiles.
I had also tried to intercept on_player_used_capsule and manually create the entity with this alternative method:

Code: Select all

--control.lua
local function contains(table, val)
    for i=1,#table do
       if table[i] == val then 
          return true
       end
    end
    return false
 end

script.on_event({defines.events.on_player_used_capsule}, function(event)
    --items to check: 
    local thrown_capsules = {"grenade", "cluster-grenade", "poison-capsule", "slowdown-capsule", "defender-capsule", "distractor-capsule", "destroyer-capsule"}
    
    local player = game.get_player(event.player_index)
    local surface = player.surface
    local item = event.item

    if not contains(thrown_capsules, item.name) then return end

    local entity_to_create = {name = item.name, position = player.position, player = player, force = player.force, target=event.position, speed = 0.1, source = player.character} -- this works to create an entity, however this is not associated with the player in any way. 
    --setting source = player in the above line caused an error: "Given object is not lua entity" while source = player.character seems to do nothing 
    local entity = surface.create_entity(entity_to_create)
    entity.last_user = player 
    game.print(entity.last_user) -- didn't work 
    -- entity.source = player -- bad syntax / causes crash
    
end)


script.on_event({defines.events.on_entity_died}, function(event)
    local dealer = event.cause
    game.print(dealer) -- nil when the created entity is the killer
end)
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 3051
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: Cannot clone various projectile entities

Post by boskid »

Entities of type "projectile" cannot be cloned. This is intended because they also contain state where they may point at a specific entity and there would be ambiguity if a cloned projectile should point at a original target entity or cloned target entity in case of a clone area. What if only the projectile would be cloned etc. It is just easier to not allow cloning them.
Post Reply

Return to “Not a bug”