[Resolved] Table Question

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

[Resolved] Table Question

Post 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.
Last edited by TheSAguy on Mon Jul 02, 2018 6:33 pm, edited 1 time in total.
Bilka
Factorio Staff
Factorio Staff
Posts: 3470
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Table Question

Post 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.
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: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Table Question

Post by TheSAguy »

Ah, thanks! Learned something now :)
Post Reply

Return to “Modding help”