Spawn Biter When Item Placed

Place to get help with not working mods / modding interface.
Post Reply
FishSandwich
Smart Inserter
Smart Inserter
Posts: 1847
Joined: Sun Feb 23, 2014 3:37 pm
Contact:

Spawn Biter When Item Placed

Post by FishSandwich »

So, I'm trying to mod a way to spawn a biter when you place an item. I've already created the recipe, entity etc, and I can craft the item needed and research it.

When trying to place the item, I want to run this script:

Code: Select all

spawncoords = game.findnoncollidingposition("small-biter", game.player.character.position, 10, 1)
game.createentity{name = "small-biter", position = spawncoords, force = game.player.character.force}
Is this possible? Thanks! :D

User avatar
L0771
Filter Inserter
Filter Inserter
Posts: 516
Joined: Tue Jan 14, 2014 1:51 pm
Contact:

Re: Spawn Biter When Item Placed

Post by L0771 »

FishSandwich wrote:So, I'm trying to mod a way to spawn a biter when you place an item. I've already created the recipe, entity etc, and I can craft the item needed and research it.

When trying to place the item, I want to run this script:

Code: Select all

spawncoords = game.findnoncollidingposition("small-biter", game.player.character.position, 10, 1)
game.createentity{name = "small-biter", position = spawncoords, force = game.player.character.force}
Is this possible? Thanks! :D
i don't know if character has property force (must be the same like player.force), but it must work.

With little changes...

Code: Select all

game.onevent(defines.events.onbuiltentity, function(event)
	if event.createdentity.name == "item" then
		local spawncoords = game.findnoncollidingposition("small-biter", event.createdentity.position, 10, 1)
		if spawncoords ~= nil then
			game.createentity{name = "small-biter", position = spawncoords, force = game.getplayer(event.playerindex).force}
		end
	end
end)

Rahjital
Filter Inserter
Filter Inserter
Posts: 435
Joined: Thu May 29, 2014 10:44 am
Contact:

Re: Spawn Biter When Item Placed

Post by Rahjital »

There is a much easier way: You can clone defender capsules and make them spawn biters instead of robots. No scripting required.

User avatar
L0771
Filter Inserter
Filter Inserter
Posts: 516
Joined: Tue Jan 14, 2014 1:51 pm
Contact:

Re: Spawn Biter When Item Placed

Post by L0771 »

Rahjital wrote:There is a much easier way: You can clone defender capsules and make them spawn biters instead of robots. No scripting required.
Can create an unit type? because combat-robot type needs time_to_live.

Rahjital
Filter Inserter
Filter Inserter
Posts: 435
Joined: Thu May 29, 2014 10:44 am
Contact:

Re: Spawn Biter When Item Placed

Post by Rahjital »

Yes, you can create any entity type with it. Robots, units, assemblers, as long as it's an entity, it can be created.

User avatar
L0771
Filter Inserter
Filter Inserter
Posts: 516
Joined: Tue Jan 14, 2014 1:51 pm
Contact:

Re: Spawn Biter When Item Placed

Post by L0771 »

in that case it is better with capsules ;)

FishSandwich
Smart Inserter
Smart Inserter
Posts: 1847
Joined: Sun Feb 23, 2014 3:37 pm
Contact:

Re: Spawn Biter When Item Placed

Post by FishSandwich »

Rahjital wrote:There is a much easier way: You can clone defender capsules and make them spawn biters instead of robots. No scripting required.
Ah cool, I didn't know that, I'll give this a try.
L0771 wrote:

Code: Select all

game.onevent(defines.events.onbuiltentity, function(event)
	if event.createdentity.name == "item" then
		local spawncoords = game.findnoncollidingposition("small-biter", event.createdentity.position, 10, 1)
		if spawncoords ~= nil then
			game.createentity{name = "small-biter", position = spawncoords, force = game.getplayer(event.playerindex).force}
		end
	end
end)
I can still use this to spawn biter spawners, thanks! :D

FishSandwich
Smart Inserter
Smart Inserter
Posts: 1847
Joined: Sun Feb 23, 2014 3:37 pm
Contact:

Re: Spawn Biter When Item Placed

Post by FishSandwich »

Rahjital wrote:Yes, you can create any entity type with it. Robots, units, assemblers, as long as it's an entity, it can be created.
Hmm, I'm not having much luck with this. My modding skills are limited, could you tell me how to do this exactly?

User avatar
L0771
Filter Inserter
Filter Inserter
Posts: 516
Joined: Tue Jan 14, 2014 1:51 pm
Contact:

Re: Spawn Biter When Item Placed

Post by L0771 »

FishSandwich wrote:
Rahjital wrote:Yes, you can create any entity type with it. Robots, units, assemblers, as long as it's an entity, it can be created.
Hmm, I'm not having much luck with this. My modding skills are limited, could you tell me how to do this exactly?
Some like this.
__base__/prototypes/item/capsule.lua > line 108, and change the proyectile

Code: Select all

  {
    type = "capsule",
    name = "put-unit-capsule",
    icon = "__base__/graphics/icons/defender-capsule.png",
    flags = {"goes-to-quickbar"},
    capsule_action =
    {
      type = "throw",
      attack_parameters =
      {
        ammo_category = "capsule",
        cooldown = 15,
        projectile_creation_distance = 0.6,
        range = 20,
        ammo_type =
        {
          category = "capsule",
          target_type = "position",
          action =
          {
            type = "direct",
            action_delivery =
            {
              type = "projectile",
              projectile = "put-unit-projectile",          -- i only change it
              starting_speed = 0.3
            }
          }
        }
      }
    },
   subgroup = "capsule",
    order = "d[defender-capsule]",
    stack_size = 100
  },
and add the proyectile
__base__/prototypes/entity/projectiles.lua > line 402, and change the entity created

Code: Select all

{
    type = "projectile",
    name = "put-unit-projectile",
    flags = {"not-on-map"},
    acceleration = 0.005,
    action =
    {
      type = "direct",
      action_delivery =
      {
        type = "instant",
        target_effects =
        {
          {
            type = "create-entity",
            entity_name = "your-unit",                    -- i only change it
          },
        }
      }
    },
    light = {intensity = 0.5, size = 4},
    animation =
    {
      filename = "__base__/graphics/entity/combat-robot-capsule/defender-capsule.png",
      frame_count = 1,
      width = 32,
      height = 32,
      priority = "high"
    },
    shadow =
    {
      filename = "__base__/graphics/entity/combat-robot-capsule/combat-robot-capsule-shadow.png",
      frame_count = 1,
      width = 32,
      height = 32,
      priority = "high"
    },
    smoke = capsule_smoke,
  },
And need a recipe if you want craft it.

FishSandwich
Smart Inserter
Smart Inserter
Posts: 1847
Joined: Sun Feb 23, 2014 3:37 pm
Contact:

Re: Spawn Biter When Item Placed

Post by FishSandwich »

Okay, I'm having problems here. :(

Edit: Never mind, it works, it was my mistake! Thanks! :D

User avatar
L0771
Filter Inserter
Filter Inserter
Posts: 516
Joined: Tue Jan 14, 2014 1:51 pm
Contact:

Re: Spawn Biter When Item Placed

Post by L0771 »

FishSandwich wrote:Okay, I'm having problems here. :(

Edit: Never mind, it works, it was my mistake! Thanks! :D
Only the subgroup?
I just got the speed x20, spawned 50 small biters and game has crashed

No one born knowing

FishSandwich
Smart Inserter
Smart Inserter
Posts: 1847
Joined: Sun Feb 23, 2014 3:37 pm
Contact:

Re: Spawn Biter When Item Placed

Post by FishSandwich »

Okay, I have this working quite well, I have a problem though. When throwing capsules, I'll sometimes get biters/worms/spawners spawning on top of each other or on water, which is annoying.

Is there any way to get capsules to check collision and spawn in a free spot?

User avatar
L0771
Filter Inserter
Filter Inserter
Posts: 516
Joined: Tue Jan 14, 2014 1:51 pm
Contact:

Re: Spawn Biter When Item Placed

Post by L0771 »

FishSandwich wrote:Is there any way to get capsules to check collision and spawn in a free spot?
no, you can't, this action haven't a defined event.

This don't happen with robots because they fly and they haven't collusion_box
collision_box = {{0, 0}, {0, 0}}

Can create a unit for this without collusion_box.
Some like this in data.lua, and need change entity created in projectile.

Code: Select all

function newUnit()
	newunit = data.raw["unit"]["small-biter"]
	newunit.name = "new-small-biter"
	newunit.collision_box = {{0, 0}, {0, 0}}
	return newunit
end


data:extend(
{
	newUnit()
}
)

FishSandwich
Smart Inserter
Smart Inserter
Posts: 1847
Joined: Sun Feb 23, 2014 3:37 pm
Contact:

Re: Spawn Biter When Item Placed

Post by FishSandwich »

Hmm, when I use that code the way you say I get error message

Code: Select all

Error while loading prototype "new-small-biter" (unit): Prototype "new-small-biter" registered twice
I'm doing something wrong I'm sure. :( Sorry for my noobness

User avatar
L0771
Filter Inserter
Filter Inserter
Posts: 516
Joined: Tue Jan 14, 2014 1:51 pm
Contact:

Re: Spawn Biter When Item Placed

Post by L0771 »

FishSandwich wrote:Hmm, when I use that code the way you say I get error message

Code: Select all

Error while loading prototype "new-small-biter" (unit): Prototype "new-small-biter" registered twice
I'm doing something wrong I'm sure. :( Sorry for my noobness
I'm sorry, my bad, i don't tested it (cost only 10 sec, but i'm lazy)

Code: Select all

local newunit = util.table.deepcopy(data.raw["unit"]["small-biter"])
newunit.name = "new-small-biter"
newunit.collision_box = {{0, -0.01}, {0, 0.01}}

data:extend( { newunit } )
... with low collision_box they don't run on water and can't bug one over other... (i was searched this for long time, now, with it, i can improve an entity )

FishSandwich
Smart Inserter
Smart Inserter
Posts: 1847
Joined: Sun Feb 23, 2014 3:37 pm
Contact:

Re: Spawn Biter When Item Placed

Post by FishSandwich »

L0771 wrote:

Code: Select all

local newunit = util.table.deepcopy(data.raw["unit"]["small-biter"])
newunit.name = "new-small-biter"
newunit.collision_box = {{0, -0.01}, {0, 0.01}}

data:extend( { newunit } )
... with low collision_box they don't run on water and can't bug one over other...
You are champion. :) This works nicely. Can still throw capsule on water to spawn biter, but, that is maybe unsolvable? It's no problem anyway.
L0771 wrote:(i was searched this for long time, now, with it, i can improve an entity )
I really appreciate your efforts to help me. :)

User avatar
L0771
Filter Inserter
Filter Inserter
Posts: 516
Joined: Tue Jan 14, 2014 1:51 pm
Contact:

Re: Spawn Biter When Item Placed

Post by L0771 »

FishSandwich wrote:Can still throw capsule on water to spawn biter, but, that is maybe unsolvable? It's no problem anyway.
i think no, it's a problem, because this don't cause a event, the only way is scan the nearest map every second... it's hard and use a lot of resources, i think resources bad used.

i'm glad for help

LordFedora
Filter Inserter
Filter Inserter
Posts: 310
Joined: Fri Nov 07, 2014 3:46 am
Contact:

Re: Spawn Biter When Item Placed

Post by LordFedora »

Does is spawn a onUnitCreated? or similar?

User avatar
L0771
Filter Inserter
Filter Inserter
Posts: 516
Joined: Tue Jan 14, 2014 1:51 pm
Contact:

Re: Spawn Biter When Item Placed

Post by L0771 »

LordFedora wrote:Does is spawn a onUnitCreated? or similar?
i use test mode for this, and use a capsule don't have a event. I think this must have

Rahjital
Filter Inserter
Filter Inserter
Posts: 435
Joined: Thu May 29, 2014 10:44 am
Contact:

Re: Spawn Biter When Item Placed

Post by Rahjital »

Yeah, unfortunately there is no generic onEntityCreated event. I think finding non-colliding positions is only possible through Lua code, and that can only be accessed through the onEntityPlaced event which doesn't work with capsules.

Post Reply

Return to “Modding help”