Page 1 of 1

[Resolved] Table Question

Posted: Mon Jul 02, 2018 5:53 pm
by TheSAguy
Hi,

I'm not understaning my table return value.

Under initiate I have:

Code: Select all

	if global.deployed_mine == nil then
		global.deployed_mine = {} 
	end
When I create the entity, via a trigger read, I add it to my table like this:

Code: Select all

script.on_event(defines.events.on_trigger_created_entity, function(event)
	
	local entity = event.entity	

    if entity.valid and NELandmine(entity)  == "landmine" then

		global.deployed_mine[entity.unit_number] = {mine=entity, time=event.tick}
		writeDebug(#global.deployed_mine) -- print value
    end
end
But the values I get from when I print the number of entries in the table start at 0 and the second one is "4" and then it goes to 5, 6, 7...

Why dies it not start at "1" on the first read, since it happens after the first add and then goes to 2, 3, 4...?
Image

I guess the next question is, how do I add to the table each time the conditions are true and how do I remove.
With remove I was using:

Code: Select all

local function On_Death(event)


	local entity = event.entity	

    if entity.valid and NELandmine(entity)  == "landmine" then

		if global.deployed_mine[entity.unit_number] then	
			global.deployed_mine[entity.unit_number] = nil
		end
		writeDebug(#global.deployed_mine) -- print table count

    end
end
But I get "6", then "0", "0", "0". Here I thought it would go from 4 to 0

Image
Not sure what I'm doing wrong here.
Thanks.

Re: Table Question

Posted: Mon Jul 02, 2018 6:11 pm
by Bilka
TheSAguy wrote:writeDebug(#global.deployed_mine) -- print table count
# does not work on tables with non-sequential indexes. Your table has "holes" because you are using the unit number. Use table_size(global.deployed_mine) instead.

Re: Table Question

Posted: Mon Jul 02, 2018 6:33 pm
by TheSAguy
Ah, thanks! Learned something now :)