2D array help

Place to get help with not working mods / modding interface.
User avatar
Lutra
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Fri Apr 01, 2016 9:22 pm
Contact:

2D array help

Post by Lutra »

QDS_positions={}
--ln80
if entity.name == "quest-delivery-system" then
QDS_positions[event.player_index][x] = x
QDS_positions[event.player_index][y] = y
player.print(QDS_positions[event.player_index][x])
player.print(QDS_positions[event.player_index][y])
end

i have this code to detect when you place a certain entity(the QDS) and save the x/y coordinates to a global array QDS_positions along with your player index so the structure of the array will be (player index,XorY) player_index is used to index the first dimension, "x" or "y" for the second. however this happens:
Attachments
error.png
error.png (699.45 KiB) Viewed 5680 times
Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: 2D array help

Post by prg »

You first need to create QDS_positions[event.player_index] as a table before you can index it with x and y.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
User avatar
Lutra
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Fri Apr 01, 2016 9:22 pm
Contact:

Re: 2D array help

Post by Lutra »

how?
Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: 2D array help

Post by prg »

QDS_positions[event.player_index] = {}

Also you probably want to use ["x"] and ["y"] or simply .x and .y as indices, not the variables x and y. You can do that in a single line with QDS_positions[event.player_index] = {x = x, y = y} ({a = b} is short for {["a"] = b})
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
User avatar
Lutra
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Fri Apr 01, 2016 9:22 pm
Contact:

Re: 2D array help

Post by Lutra »

Thanks! works perfectly now. :D
Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn
User avatar
Lutra
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Fri Apr 01, 2016 9:22 pm
Contact:

Re: 2D array help

Post by Lutra »

same mod now. different problem. i have the x,y coordinates of an entity stored in a global array. how do i find the entity using those coordinates i.e. to check it's inventory?
Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn
MrDoomah
Fast Inserter
Fast Inserter
Posts: 196
Joined: Mon Jun 01, 2015 1:11 pm
Contact:

Re: 2D array help

Post by MrDoomah »

It seems to be that you're better of storing the entity itself in an array.

Instead of your code:

Code: Select all

QDS_positions={}
--ln80
if entity.name == "quest-delivery-system" then
QDS_positions[event.player_index][x] = x
QDS_positions[event.player_index][y] = y
player.print(QDS_positions[event.player_index][x])
player.print(QDS_positions[event.player_index][y])
end
you might want to try this:

Code: Select all

list_of_QDS={}
--ln80
if entity.name == "quest-delivery-system" then
  table.insert(list_of_QDS[event.player_index],entity)
end
you might want to catch the on_player_created event and do a list_of_QDS[event.player_index] = {}.

If you now want to access a specific QDS you can just iterate over that list and check the coordinates:

Code: Select all

for _, QDS in pairs(list_of_QDS[player_index}) do
  if QDS.valid and QDS.position.x == x and QDS.position.y == y then
    inv = QDS.inventory
  end
end
User avatar
Lutra
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Fri Apr 01, 2016 9:22 pm
Contact:

Re: 2D array help

Post by Lutra »

thanks again. the mod is nearly finished now(i will upload it to the main mod topic). the way i have it set up is when a player places a QDS, the x/y of the QDS are stored in the QDS_positions array along with the player's index, so if i want to access the location of a certain player's QDS, i just put the numbers in the array :D. also what is the correct syntax for entity.remove_item?
Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn
MrDoomah
Fast Inserter
Fast Inserter
Posts: 196
Joined: Mon Jun 01, 2015 1:11 pm
Contact:

Re: 2D array help

Post by MrDoomah »

Lutra wrote: also what is the correct syntax for entity.remove_item?
Check the documentation? http://lua-api.factorio.com/0.12.30/Lua ... emove_item
User avatar
Lutra
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Fri Apr 01, 2016 9:22 pm
Contact:

Re: 2D array help

Post by Lutra »

function GetNoOfRequirements(QuestName)
local number = 0
if QuestName == "stone quest(x50 stone/x15 iron ore)" then
number = 1
elseif QuestName == "coal quest(x30 coal/x1 stone furnace)" then
number = 1
elseif QuestName == "burner drills quest(x2 burner drill/x25 basic belt)" then --line 163
number = 1
elseif QuestName == "plates quest(x50 iron plate, x50 copper plate/x8 inserter)" then
number = 2
elseif QuestName == "power quest(x1 offshore pump, x7 boiler, x5 steam engine/x50 small electric pole)" then
number = 3
end
return number
end

i get an error message saying there's a syntax error at line 163 near QuestName
Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: 2D array help

Post by prg »

Code: Select all

$ lua
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> function GetNoOfRequirements(QuestName)
>> local number = 0
>> if QuestName == "stone quest(x50 stone/x15 iron ore)" then
>> number = 1
>> elseif QuestName == "coal quest(x30 coal/x1 stone furnace)" then
>> number = 1
>> elseif QuestName == "burner drills quest(x2 burner drill/x25 basic belt)" then --line 163
>> number = 1
>> elseif QuestName == "plates quest(x50 iron plate, x50 copper plate/x8 inserter)" then
>> number = 2
>> elseif QuestName == "power quest(x1 offshore pump, x7 boiler, x5 steam engine/x50 small electric pole)" then
>> number = 3
>> end
>> return number
>> end
> 
> print(GetNoOfRequirements("burner drills quest(x2 burner drill/x25 basic belt)"))
1
>
Works for me.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
User avatar
Lutra
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Fri Apr 01, 2016 9:22 pm
Contact:

Re: 2D array help

Post by Lutra »

hmm. same here now. not sure what i did there... :oops:
Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn
User avatar
Lutra
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Fri Apr 01, 2016 9:22 pm
Contact:

Re: 2D array help

Post by Lutra »

How can you tell when the player's inventory is open. i would use a custom event when the 'e' button is pressed, but people might change the key, and i know i use 'e' for closing other GUIs. so is there a specific event that is called when an inventory is opened?
Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: 2D array help

Post by prg »

Don't think there's an event for that, but maybe you can check player.opened_self in on_tick.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
User avatar
Lutra
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Fri Apr 01, 2016 9:22 pm
Contact:

Re: 2D array help

Post by Lutra »

is it called when you open chests, storage etc?

edit: there is player.opened which returns the entity that the player has open. that could be useful
Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn
Post Reply

Return to “Modding help”