2D array help
2D array help
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:
--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 (699.45 KiB) Viewed 5683 times
Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn
Re: 2D array help
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!
Re: 2D array help
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})
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!
Re: 2D array help
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
Re: 2D array help
It seems to be that you're better of storing the entity itself in an array.
Instead of your code:
you might want to try this:
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:
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
Code: Select all
list_of_QDS={}
--ln80
if entity.name == "quest-delivery-system" then
table.insert(list_of_QDS[event.player_index],entity)
end
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
Re: 2D array help
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
. also what is the correct syntax for entity.remove_item?

Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn
Re: 2D array help
Check the documentation? http://lua-api.factorio.com/0.12.30/Lua ... emove_itemLutra wrote: also what is the correct syntax for entity.remove_item?
Re: 2D array help
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
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
Re: 2D array help
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
>
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
Re: 2D array help
hmm. same here now. not sure what i did there... 

Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn
Re: 2D array help
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
Re: 2D array help
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!
Re: 2D array help
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
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