Page 1 of 1

Help with Crash in code - Solved

Posted: Wed Oct 07, 2015 11:14 pm
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

Re: Help with Crash in code

Posted: Thu Oct 08, 2015 5:29 pm
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.

Re: Help with Crash in code

Posted: Thu Oct 08, 2015 6:02 pm
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.

Re: Help with Crash in code

Posted: Thu Oct 08, 2015 6:39 pm
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.

Re: Help with Crash in code - Solved

Posted: Fri Oct 09, 2015 1:40 am
by TheSAguy
Thanks Adil!
I had an error in my code.
Line 105 I had "return" and should have been "break"