Jump to content

Question about indexing arrays with enums


xxmitsu

Recommended Posts

Hi,

I recently began to script in LUA, and I have some experience with PAWN.

Now, I'm trying to convert some parts of my pawn scripts to lua.

Then problem is that somehow I got stucked.

I'm trying to make an array structure.

something like this in pawn:

#define MAX_PLAYERS 100
enum pInfo
{
pSQLID,
pAdmin,
pLevel,
}
 
new PlayerInfo[MAX_PLAYERS][pInfo];

I've translated into this:

-- structura
pInfo = { pSQLID, pAdmin, pLevel,}
gPlayerInfo = {}
for i=1,100 do 
    gPlayerInfo[i] = pInfo
end

Then, I'm trying to assign a value when a player joins.

function joinHandler()
gPlayerInfo[source][pLevel] =1; 
end
 
-- eventuri
addEventHandler("onPlayerJoin", getRootElement(), joinHandler)

After this, I've tried to make an command to see if anything changed..

function DebugFunction()
outputChatBox ( "Variabila are " .. gPlayerInfo[source][pLevel] .. " valoarea ",thePlayer )
end
 
-- comenzi
addCommandHandler("debugf", DebugFunction)

When I type /debugf in the game, nothing happens, but when I look into the server's console I get this:

[02:00:46] ERROR: ...server/mods/deathmatch/resources/eGaming/egaming.lua:36: at
tempt to index field '?' (a nil value)
[02:00:49] ERROR: ...server/mods/deathmatch/resources/eGaming/egaming.lua:68: at
tempt to index field '?' (a nil value)

Line 36 is:

gPlayerInfo[source][pLevel] = 1;

Line 68 is:

outputChatBox ( "Variabila are " .. gPlayerInfo[source][pLevel] .. " valoarea ",thePlayer )

How can I index those arrays ? What am I doing wrong ?

Any help is apreciated.

Thank you in advance.

Mike.

Link to comment

use [ lua] tag for your code, it highlights the syntax/functions *)

looks like you're passing undefined variables (like pLevel) as a key, try using quotes:

gPlayerInfo[source]['pLevel'] = 1

or

gPlayerInfo[source].pLevel = 1

and why create 1-100 indexed table (you probably not going to need it anyway), if your script uses player element as a key anyway?

in Lua almost anything can be a table key. and defining the whole table for 100 players isnt needed really.

i'd make it like this:

gPlayerInfo = {}
 
function joinHandler()
  gPlayerInfo[source] = {}
  gPlayerInfo[source].pSQLID = 1
  gPlayerInfo[source].pAdmin = "some string value"
  gPlayerInfo[source].pLevel = 1
end
-- eventuri
addEventHandler("onPlayerJoin", getRootElement(), joinHandler)
 
function DebugFunction(thePlayer)
outputChatBox ( "Variabila are " .. gPlayerInfo[thePlayer].pLevel .. " valoarea ",thePlayer )
end
-- comenzi
addCommandHandler("debugf", DebugFunction)

let pro-scripters correct me if im wrong *)

Edited by Guest
Link to comment

I don't know exactly how Pawn works, but you don't need to predefine the keys you want to use in a table.

-- structura
pInfo = { pSQLID, pAdmin, pLevel,}
gPlayerInfo = {}
for i=1,100 do 
    gPlayerInfo[i] = pInfo
end

What you do here is create a table that contains no element at all, because you add 3 variables to it (pSQLID, pAdmin, pLevel) that will have the value "nil" if you haven't assigned a value to them before. This is basicially the same as:

pInfo = {}
pInfo[1] = nil
pInfo[2] = nil
pInfo[3] = nil

This obviously doesn't do you any good.

Then you add the empty table 100 times to the gPlayerInfo table.

function joinHandler()
  gPlayerInfo[source][pLevel] =1;
end

This doesn't work because gPlayerInfo[source] is not a table, so you can't access it. And again, pLevel is used as a variable, and is probably nil, unless you defined it as a global (at least to the file) variable.

What you want to do here is:

function joinHandler()
 gPlayerInfo[source] = {} -- create an empty table for this player element
 gPlayerInfo[source].pLevel = 1 -- Add the number '1' to the table element with the key 'pLevel' (this is the same as gPlayerInfo[source]["pLevel"] = 1
end

function DebugFunction()
outputChatBox ( "Variabila are " .. gPlayerInfo[source][pLevel] .. " valoarea ",thePlayer )
end

This won't work for the same reason, gPlayerInfo[source] doesn't exist and pLevel is expected to be a variable. Also, I don't think that "source" exists in this context. Also you haven't defined "thePlayer", so it will be nil and thus displayed for all players (since the default will be used then, which is getRootElement()). You can access the value like that:

function DebugFunction()
local player = ...
outputChatBox("Value: "..tostring(gPlayerInfo[player].pLevel))
end

You have to define "player" somehow though. You could use getPlayerFromName() or just loop through the whole table, for example:

function DebugFunction()
for k,v in pairs(gPlayerInfo) do
outputChatBox("Value for player "..getPlayerName(k).." is "..tostring(v.pLevel))
end
end

Link to comment

Thank you very much!

I have been caught in a pleasant way, didn't tought that anyone would have so much patience to explain to me in such detailed way.

I find lua pretty difficult due to the fact that until now I had to do only with languages based on C syntax

Thanks again both of you! That really helped me

PS: didn't knew about [lua] tag

Mike.

Link to comment

Doing things like this:

pInfo = { pSQLID, pAdmin, pLevel,}
gPlayerInfo = {}
for i=1,100 do
    gPlayerInfo[i] = pInfo
end

Is not ideal, even if pSQLID, pAdmin and pLevel had values assigned to them. It's simply because when you assign a table to a variable you don't make a copy of that table but use instance of that table.

Have a look at this example:

table1 = { 123, 456, 789 };
table2 = table1;
table2[ 1 ] = 0; -- change 123 to 0
print( table1[ 1 ] ); -- This will print "0", not 123 as you'd expect

As you can see, I changed value of first index of table2 to 0 but it also changed table1. That's because table2 was assigned an instance of table1.

not sure if "instance" is the correct name though

Also, xxmistu, 1 think to remember about Lua is that it's typeless language. That means you don't have to declare variables with type of variable like it's in C-like languages (int iInteger, char cChar, etc.). Lua is dynamic.

Link to comment

I find lua pretty difficult due to the fact that until now I had to do only with languages based on C syntax

I think lua is pretty easy and flexible to use. The tables are extremely versatile, e.g. you can use anything as a key, even another table. :) You don't have to predefine the size or somehting. Functions can return multiple values, which can come in very handy, e.g. you could return a boolean to say if a function succeded, as well as a message that contains the reason. And of course there are functions like getElementPosition() which you can use like that:

local x,y,z = getElementPosition()

Instead of:

local pos = getElementPosision() -- would have to return a table to return several values
local x = pos.x
local y = pos.y
local z = pos.z

Of course you first have to get used to a new language.

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...