"can_place_entity" question

Place to get help with not working mods / modding interface.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

"can_place_entity" question

Post by TheSAguy »

Hi,

I'm using "can_place_entity", but still I'm getting units spawned on water tiles. (I thought it would check for water tiles)

This is the code:

Code: Select all

	while Dens_Spawned < 10 do
		
		local surface = game.surfaces['nauvis']
		local pos_x = math.random(-200,200)
		local pos_x = math.random(-200,200)
	
		if surface.can_place_entity({ name="alien-army-26", position={pos_x, pos_y}, force = game.forces.alien}) then					
			
			local lords = surface.create_entity({name="alien-army-26", position={pos_x, pos_y},force = game.forces.alien})						
			Dens_Spawned = Dens_Spawned + 1
			Scatter = Scatter + 100	
			
		end
		
	end
Image

Next I tried looking at the tile, to see if it's a water tile, but I can't seem to get "get_tile" working during "script.on_init"

Code: Select all

script.on_init(on_initialize)


local waterTiles =
{
  ["deepwater"] = true,
  ["deepwater-green"] = true,
  ["water"] = true,
  ["water-green"] = true
}



function Initial_Spawn(surface)

	local spawn_zone = 100
	local Dens_Spawned = 0
	local Scatter = 0
	local chart_radius = 10
	local radius_from_player = settings.startup["Alien_Distance"].value
	
	while Dens_Spawned < 20 do
		
		local surface = surface
		local pos_x = math.random(-200,200)
		local pos_y = math.random(-200,200)
		
		local currentTilename = game.surfaces[1].get_tile(pos_x,pos_y).name
		
		if surface.can_place_entity({ name="alien-army-26", position={pos_x, pos_y}, force = game.forces.alien}) and not waterTiles[currentTilename] then
					
			local lords = surface.create_entity({name="alien-army-26", position={pos_x, pos_y},force = game.forces.alien})	
			
nd			
			
			Dens_Spawned = Dens_Spawned + 1
			Scatter = Scatter + 100
		else 
		
			Dens_Spawned = Dens_Spawned
			
		end
		
	end

	
    return lords
end



Image

Last question for now.
Besides using "surface = game.surfaces['nauvis']" is there a better surface to use? I've had some issues in the past with hard coding the surface in my other mods, but can't use event or created_entity or any of those here.

I've attached the file in question and full mod.


Thanks.
Attachments
Alien_Biters_0.1.1.zip
Mod
(16.27 KiB) Downloaded 40 times
initialSpawn.lua
Spawn Settings
(3.34 KiB) Downloaded 46 times

Bilka
Factorio Staff
Factorio Staff
Posts: 3305
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: "can_place_entity" question

Post by Bilka »

Does the thing that you try to spawn have the "water_tile" collision mask?
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: "can_place_entity" question

Post by TheSAguy »

Well, it's a small-biter that get's cloned. Units don't have a Collision Mask.
I've added "collision_mask = {"water-tile"}," to the unit, but it still has the same result.

Rseding91
Factorio Staff
Factorio Staff
Posts: 14104
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: "can_place_entity" question

Post by Rseding91 »

You want to use the surface from what ever event you're running your code.

If you just want to do a generic "on the game" then you iterate all surfaces and work on all of them.

If you're doing something say in reaction to a given player you iterate the players and use the surface off the given player.

If you do something in the on-built event you use the surface from built entity.
If you want to get ahold of me I'm almost always on Discord.

TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: "can_place_entity" question

Post by TheSAguy »

Rseding91 wrote:You want to use the surface from what ever event you're running your code.

If you just want to do a generic "on the game" then you iterate all surfaces and work on all of them.

If you're doing something say in reaction to a given player you iterate the players and use the surface off the given player.

If you do something in the on-built event you use the surface from built entity.
Thanks for this summary Rseding, this was very helpful. Should probably be in the Wiki if not already.

I'm still unsure what to use on "on_init", when I create a spawner:
on_inot
All the examples I've seen in other mods use "surface = game.surfaces['nauvis']" or "surface = game.surfaces['1]"
IS that correct?

Any input on "can_place_entity" or "get_tile"
Thanks.

Post Reply

Return to “Modding help”