Coding Help Needed

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

Coding Help Needed

Post by TheSAguy »

Hi,

I have created a new enemy unit, with it's own force. Vampire unit & force.

I'm trying to accomplish the following. When a Vampire unit kills another vanilla enemy, a new vampire unit will spawn.
This was my approach, when a small biter dies, look in the immediate area and see if there is a vampire biter. if there is one, assume that it killed the biter and spawn a new vampire biter.

I'm not 100% sure how to search the area around the dead biter and if I need to use "find_non_colliding_position" when spawning the new unit.
Below is my code attempt, with some alternative possibilities commented out. Could I please have someone review this and give me pointers?

Code: Select all


---------------------------------------------
script.on_event({defines.events.on_entity_died,},function(event) On_Remove(event) end)
---------------------------------------------

function On_Remove(event)

	-- When a small biter gets killed.

	if event.entity.name == "small-biter" then
		local surface = game.get_surface(0)
		local radius = 1
		local pos = event.entity.position
		local area = {{pos.x - radius, pos.y - radius}, {pos.x + radius, pos.y + radius}}
       
	   -- find nearby Vampire biters
       local V_Biters = surface.find_entities_filtered{area=area, name="small-vampire"}
	   --local V_Biters = surface.find_enemy_units(pos, radius, vampire) -- Alternative possibility?
	   --local V_Biter = surface.find_entities_filtered{area=area, force="vampire"} -- Alternative possibility?

		if V_Biters then   
			player.surface.create_entity{name="small-vampire", position={pos}, force=game.forces.vampire}
			-- player.surface.create_entity{name="small-vampire", position={PosX,PosY}, force=game.forces.vampire} -- Alternative possibility?
			--SpawnVampire(event.entity) -- Alternative possibility using below function	
		end
			
	end
	
end

function SpawnVampire(enemy)
		
		local SpawnPosition = enemy.surface.find_non_colliding_position(enemy, enemy.position, 2, 0.5)
		if SpawnPosition then
			enemy.surface.create_entity({name = "small-vampire", position = SpawnPosition, force = game.forces.enemy})
		end
end

Thanks!
Attachments
control.lua
Control file
(2.19 KiB) Downloaded 53 times
User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Coding Help Needed

Post by Adil »

I'm not good at C++ so no pointers :P
The code you've made looks like it could work. At least for default surface and a single type of vampires.
Though I'm not sure if

Code: Select all

game.get_surface(0) 
is correct syntax at all. (I believe a name of surface should be instead of zero there.) But generally, you don't need that function, you can obtain relevant surface from the entity which is given to handler. (See example in my code below.)
Also, if there's more than one type of vampires it'd be indeed better to use the third variant of find_entities:

Code: Select all

local V_Biter = surface.find_entities_filtered{area=area, force="vampire"}
As I said, I'm not good with pointers and reviewing. When I see code, alternatives of how would I do the same keep popping out in my head. So below under spoiler my untested implementation is provided. Feel free to examine or use it.
My code
edit: code bugfix
Last edited by Adil on Fri Apr 15, 2016 7:36 am, edited 2 times in total.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1455
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Coding Help Needed

Post by TheSAguy »

OMG Adil!
This looks great, can't wait to give it a try.

Thanks.
Post Reply

Return to “Modding help”