Jump to content

[QUESTION] Please explain to me how tables work


Recommended Posts

So... I am used to samp enums that looks like this:

enum ePlayerInfo  {
    name,
    type,
    Float:lastX,
    Float:lastY,
    Float:lastX,
};
 
// to store the player info
new gPlayerInfo[MAX_PLAYERS][ePlayerInfo];
 
public function onPlayerConnect(player) {
    gPlayerInfo[player][name] = getPlayerName(player);
    gPlayerInfo[player][type] = 0;
}

Simple

But how do I do that with tables?

 

For example

table = {}

table["name"] = MyName -- this will assign MyName to key name, right?

But how do I set an ID to that key so I can identify it later... 

for example if I have this loop

for _, row in ipairs (result) do
		for column, value in pairs ( row ) do
		end
		payphones[_]["PosX"] = row["PosX"];
		payphones[_]["PosY"] = row["PosY"];
		payphones[_]["PosZ"] = row["PosZ"];
		outputChatBox(payphones[_]["PosX"])
	end

_ - is the loop increment
first loop should assign PosX, PosY and PosZ to ID 1 and so on... 2, 3...

payphones[1]["PosX"] should return the posx of the first item...

payphones[2]["PosX"] should return the posx of the second item...

 

how do I do this... because it doesn't work this way.. I get this error:

"attempt to index field '?' (a nil value)"

Edited by Dutchman101
Fix topic title for section requirements
Link to comment

Maybe this

payphones[_]["PosX"] = row[column]["PosX"]

-- or

payphones[_]["PosX"] = value["PosX"]

Depend on table structure.

This error "attempt to index field '?' (a nil value)" means that the index or the table doesnt exist.

You can try this.

for _, row in ipairs (result) do
		payphones[_] = {["PosX"] = row[_]["PosX"], ["PosY"] = row[_]["PosY"], ["PosZ"] = row[_]["PosZ"]}
		outputChatBox(inspect(payphones[_]))
end

Not sure what are you trying to do with that.

Link to comment

I'm not sure what you're trying to achieve, but I will tell you how I would do it. 

local payphones = {}
for _, row in ipairs (result) do
	local newTable = {}
	table.insert(newTable, row["PosX"])
	table.insert(newTable, row["PosY"];
	table.insert(newTable, row["PosZ"];
	table.insert(payphones, newTable)
end
    
--[[
  
    payPhones = {
  		[1] = {
    		1000, 500, 50
  		}
  	}
    
]]

table.insert( intoTable, data) -> this will insert the data in the table called intoTable. You don't have to specify the key, it will be automatically assigned. If you could tell me what exactly you want to achieve, I can explain it to you further.

Link to comment
2 hours ago, pa3ck said:

I'm not sure what you're trying to achieve, but I will tell you how I would do it. 


local payphones = {}
for _, row in ipairs (result) do
	local newTable = {}
	table.insert(newTable, row["PosX"])
	table.insert(newTable, row["PosY"];
	table.insert(newTable, row["PosZ"];
	table.insert(payphones, newTable)
end
    
--[[
  
    payPhones = {
  		[1] = {
    		1000, 500, 50
  		}
  	}
    
]]

table.insert( intoTable, data) -> this will insert the data in the table called intoTable. You don't have to specify the key, it will be automatically assigned. If you could tell me what exactly you want to achieve, I can explain it to you further.

I want to save the data of every payphone so I can use it later, for example to get the pos of a payphone to see if the player is near any payphone..

Link to comment
1 hour ago, Tails said:

I'm not sure how that loop correlates with the first example, maybe you could show us how the table you're looping looks like?

Also, I recommend you to check out this page https://en.wikibooks.org/wiki/Lua_Programming/Tables to learn about Lua tables.

I've done this

 

function assignPayPhones (res)
	local checkNumberQuery = dbQuery(handler, "SELECT * FROM PayPhoneOW")
	local result, num_affected_rows, last_insert_id = dbPoll ( checkNumberQuery, -1 )

	payphones = {}
	payLength = 0
	for _, row in ipairs (result) do
		for column, value in pairs ( row ) do
		end
		payphones["ID".._] = row["ID"];
		payphones["PosX".._] = row["PosX"];
		payphones["PosY".._] = row["PosY"];
		payphones["PosZ".._] = row["PosZ"];
		payphones["Number".._] = row["Number"];
		payLength = payLength + 1

	end
end
addEventHandler("onResourceStart", getRootElement(), assignPayPhones)

and it works

I just want to have the values of each payphone saved so I can use them later without doing a query.

Link to comment

I don't exactly know how your database looks but just try storing them like this:

payphones = {}

for i,row in ipairs(result) do
	payphones[i] = {pos = {row.PosX, row.PosY, row.PosZ}, number = row.Number}
end

You could also use your row id and put that as the index/key

for i,row in ipairs(result) do
	payphones[row.id] = {pos = {row.PosX, row.PosY, row.PosZ}, number = row.Number}
end

Later you can get them by their index

phone = payphones[4]
blip = createBlip(phone.pos[1], phone.pos[2], phone.pos[3], 0)
number = phone.number

Hope this helps.

Edited by Tails
  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...