Need help with Table

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

Need help with Table

Post 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.
orzelek
Smart Inserter
Smart Inserter
Posts: 3928
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Need help with Table

Post 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
Outsider
Fast Inserter
Fast Inserter
Posts: 115
Joined: Sat Jan 10, 2015 12:23 am
Contact:

Re: Need help with Table

Post 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
Advanced Logistics System - Provides a detailed view of your logistics network and the items within it
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1455
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Need help with Table

Post 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!
Attachments
control_Edit.lua
Control Edits
(1.71 KiB) Downloaded 129 times
Outsider
Fast Inserter
Fast Inserter
Posts: 115
Joined: Sat Jan 10, 2015 12:23 am
Contact:

Re: Need help with Table

Post 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.
Advanced Logistics System - Provides a detailed view of your logistics network and the items within it
Post Reply

Return to “Modding help”