Help with Crash in code - Solved

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

Help with Crash in code - Solved

Post by TheSAguy »

I'm getting the following crash, when I mine my Atrifact Collectors.
Image

I'm not sure how to get this resolved! Can someone please take a look and see if they know why.
Code below and mod attached.

Line 143 is the "items = collector.surface.find_entities_"

Code: Select all

function ProcessCollector(collector)
	--This makes collectors collect items.
     writeDebug("mod looking for items")
	local items
	local inventory
	
	items = collector.surface.find_entities_filtered({area = {{x = collector.position.x - radius, y = collector.position.y - radius}, {x = collector.position.x + radius, y = collector.position.y + radius}}, name = "item-on-ground"})
	if #items > 0 then
		inventory = collector.get_inventory(chestInventoryIndex)
		for i=1,#items do
			local stack = items[i].stack
            --print(stack.name)
			if filters[stack.name] == 1 and inventory.can_insert(stack) then
				 inventory.insert(stack)
				 items[i].destroy()
				 break
			end
		end
	end
end
Attachments
control.lua
Control file
(5.4 KiB) Downloaded 247 times
Natural_Evolution_Enemies_5.0.0.zip
Mod
(206.96 KiB) Downloaded 117 times
Last edited by TheSAguy on Fri Oct 09, 2015 1:39 am, edited 1 time in total.

Outsider
Fast Inserter
Fast Inserter
Posts: 115
Joined: Sat Jan 10, 2015 12:23 am
Contact:

Re: Help with Crash in code

Post by Outsider »

You could do a check on collector before using it, also you don't really need to use it's specific surface in your find_entities_filtered call, you could just use the default game surface, using game.get_surface(1).

Can you however explain why are you using 2 entities for the Artifact Collectors? i noticed you are using 2 logistic-container entites one that is placeable and the other is not, and then you delete the placeable one and add the other as proxy in the same poisition?

people usually do that when they are using a logistic-container as an invisible proxy for a different entity, in your case it kinda doesn't make sense to me.
Advanced Logistics System - Provides a detailed view of your logistics network and the items within it

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

Re: Help with Crash in code

Post by TheSAguy »

Outsider wrote:You could do a check on collector before using it, also you don't really need to use it's specific surface in your find_entities_filtered call, you could just use the default game surface, using game.get_surface(1).

Can you however explain why are you using 2 entities for the Artifact Collectors? i noticed you are using 2 logistic-container entites one that is placeable and the other is not, and then you delete the placeable one and add the other as proxy in the same poisition?

people usually do that when they are using a logistic-container as an invisible proxy for a different entity, in your case it kinda doesn't make sense to me.
The one container has a graphic that shows the area it covers. Once you place it, it will be replaced with the normal version. That's the only why I know to get a graphical overlay, like the robo-ports have.

I'm going to try the below tonight:
local Count_Check=global.ArtifactCollectors
if Count_Check > 0 then

Code: Select all

function ProcessCollector(collector)
	--This makes collectors collect items.
     writeDebug("mod looking for items")
	local items
	local inventory
	local Count_Check=global.ArtifactCollectors
	--if Count_Check ~= nil then
	if Count_Check > 0 then
		items = collector.surface.find_entities_filtered({area = {{x = collector.position.x - radius, y = collector.position.y - radius}, {x = collector.position.x + radius, y = collector.position.y + radius}}, name = "item-on-ground"})
		if #items > 0 then
			inventory = collector.get_inventory(chestInventoryIndex)
			for i=1,#items do
				local stack = items[i].stack
				--print(stack.name)
				if filters[stack.name] == 1 and inventory.can_insert(stack) then
					 inventory.insert(stack)
					 items[i].destroy()
					 break
				end
			end
		end
	end
end
Thanks.

orzelek
Smart Inserter
Smart Inserter
Posts: 3911
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Help with Crash in code

Post by orzelek »

Assuming collector is an entity you really should check it's valid property.
There are a lot of things that can happen to it and valid property will make sure you won't do something strange on it.

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

Re: Help with Crash in code - Solved

Post by TheSAguy »

Thanks Adil!
I had an error in my code.
Line 105 I had "return" and should have been "break"

Post Reply

Return to “Modding help”