Page 1 of 1

Need help with Table

Posted: Wed Sep 16, 2015 5:24 pm
by TheSAguy
Hi,

I'm having a little trouble with adding/removing from a table I'm using. What I'm doing is adding Rocket Silo's to a table when built, having biters attack it.
It works to a certain extent, but when I build and then remove a rocket silo and re-build it, I get an error. "Item does not exist"

I think my issue is with the

Here is my code:

Initialization:

Code: Select all

	if not global.RocketSilos then
      global.RocketSilos = {}
	end
On-Built:

Code: Select all

if event.created_entity.name == "rocket-silo" then
--- Add to Table 
 table.insert(global.RocketSilos,event.created_entity)

--- Biters will attack the newly built Rocket Silo
game.get_surface(1).set_multi_command({type=defines.command.attack,target=global.RocketSilos[1],distraction=defines.distraction.none},2000)

end

Remove from Table:

--- On Remove

Code: Select all

if event.entity.name == "AlienControlStation" then
  RS_Remove() -- Call function
end

Code: Select all

function RS_Remove(index)

  if index then
    if global.RocketSilos[index] and not global.RocketSilos[index].valid then
      table.remove(global.RocketSilos, index)
      return -- if an index was found and it's entity was not valid return after removing it
    end
  end
  -- if no index was provided, or an inappropriate index was provided then loop through the table

  for k,RSilo in ipairs(global.RocketSilos) do
    if not RSilo.valid then
      table.remove(global.RocketSilos,k)

    end
  end
end
(I think my removal code is overly complicated?? - how do I streamline it?)

I think my problem is with "RocketSilos[1]" in the set_multi_command function, but not sure how to update.
Thanks.

Re: Need help with Table

Posted: Wed Sep 16, 2015 5:34 pm
by orzelek
I see some problems here - no guarantees tho.

In send command call - new element inserted to table will be on last index (#tableName-1) not on the 1.

In the removal - you did not give the entity to RS_Remove() call which effectively sets index to null in the function.
And I'm not sure what is the idea behind the index parameter in removal. It would need to be number of item in table not the entity itself - would need to search through the table first to discern the index (either in removal or in event). And checking if entity is valid might not work - it depends if entity thats being removed is valid or not during the removal event.

Second problem in removal - table.remove will not work correctly when used in iteration unless you are iterating in reverse order.
More details here: stackoverflow.com

Re: Need help with Table

Posted: Wed Sep 16, 2015 7:18 pm
by Outsider
Why not just use a custom index for the table? when using table.insert and table.remove you have no control over the index which imho isn't good enough when you want to add/remove items based on events.

you can use the entity position as a key like so :

Code: Select all

--- Add to Table 
if event.created_entity.name == "rocket-silo" then
local rocketSilos = global.RocketSilos
local key = event.created_entity.position.x .. "X" .. event.created_entity.position.y

rocketSilos[key] = event.created_entity
global.RocketSilos = rocketSilos

Code: Select all

--- Remove from table
if event.entity.name == "AlienControlStation" then
  local rocketSilos = global.RocketSilos
  local key = event.entity.position.x .. "X" .. event.entity.position.y
  if rocketSilos[key] then
    rocketSilos[key] = nil
    global.RocketSilos = rocketSilos
  end
end

Re: Need help with Table

Posted: Wed Sep 16, 2015 7:46 pm
by TheSAguy
Thanks Orzelek & Outsider.
Outsider - I'll give your code a try tonight. At work :)
Do my attached edits look good?

Orzelek, you need to dumb down your answers to me :oops:
P.S. Good work on the Biters dying from Path Timeout!

Re: Need help with Table

Posted: Wed Sep 16, 2015 10:52 pm
by Outsider
TheSAguy wrote:Thanks Orzelek & Outsider.
Outsider - I'll give your code a try tonight. At work :)
Do my attached edits look good?

Orzelek, you need to dumb down your answers to me :oops:
P.S. Good work on the Biters dying from Path Timeout!
i am not sure what you are trying to accomplish with the code overall.. as it stands there is no reason in your script to store the rocket-silos in a table.. unless you are gonna be doing more processing on them?

but anyway i can see one issue in your Edited Code, in the set_multi_command you are using "global.RockeSilos" as a target, it should be "event.created_entity", unless it actually accept a table of entities as a target and i don't think it does, so you need to use the entity reference there.