Page 1 of 1
2D array help
Posted: Sun Apr 10, 2016 4:44 pm
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:
Re: 2D array help
Posted: Sun Apr 10, 2016 4:58 pm
by prg
You first need to create QDS_positions[event.player_index] as a table before you can index it with x and y.
Re: 2D array help
Posted: Sun Apr 10, 2016 5:01 pm
by Lutra
how?
Re: 2D array help
Posted: Sun Apr 10, 2016 5:11 pm
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})
Re: 2D array help
Posted: Sun Apr 10, 2016 5:15 pm
by Lutra
Thanks! works perfectly now.

Re: 2D array help
Posted: Sun Apr 10, 2016 5:55 pm
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?
Re: 2D array help
Posted: Sun Apr 10, 2016 7:22 pm
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
Re: 2D array help
Posted: Sun Apr 10, 2016 7:38 pm
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

. also what is the correct syntax for entity.remove_item?
Re: 2D array help
Posted: Sun Apr 10, 2016 7:49 pm
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
Re: 2D array help
Posted: Sun Apr 10, 2016 8:28 pm
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
Re: 2D array help
Posted: Sun Apr 10, 2016 9:03 pm
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.
Re: 2D array help
Posted: Sun Apr 10, 2016 9:17 pm
by Lutra
hmm. same here now. not sure what i did there...

Re: 2D array help
Posted: Tue Apr 12, 2016 8:32 pm
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?
Re: 2D array help
Posted: Tue Apr 12, 2016 8:38 pm
by prg
Don't think there's an event for that, but maybe you can check
player.opened_self in on_tick.
Re: 2D array help
Posted: Wed Apr 13, 2016 6:23 pm
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