Salem

nested table problem

Recommended Posts

Hi. I have some issue and can't really deal with it. I have a table: declaration, initialization...
 

local taxidf = { { } };

taxidf[player] = { player = getPlayerName(player), ped = {}, blip = {}, mark = {}, endblip = {}, endmark = {} };   

Then to this table I put some values (one of them is below):
 

local ped = createPed( tonumber( id ), DTM_PTS[startPoint][2], DTM_PTS[startPoint][3], DTM_PTS[startPoint][4] + 0.5 )

table.insert( taxidf[player].ped, { ped } )

No matter what's in these DMTs - I need to sketch the problem... Ped is created indeed.

And when I want to receive the data later:

I get

local ped = taxidf[player].ped;

I got a message the table length is 1 but table is nil.

So I have a question: how should I take the object from this table? What am I doing wrong?

To visualize the output I :

outputDebugString("ped : "..tostring(ped)..", len: "..tostring(#ped)..", "..tostring(ped[0])..", "..tostring(ped[1]) );

results:

`ped: table : 0af4gf3t len : 0, nil, nil`

So ped[0] is null, ped is a table.... what to deal with it?? I just want to have back the object in the exact way I do this. Please some suggestions.

My target is to:

setTimer( killPed, 60, 1, ped )

  ---  error: [expected element at arg.1, got table]

or

setTimer( killPed, 60, 1, ped[1] )

  ---  error: [expected element at arg.1, got nil]

you understand... I can't kill this ped!

Edited by Salem
more xplanations

Share this post


Link to post

1. Indexing starts at 1 in Lua, not 0.

2. Can you post your full code?
ped[1] should be a table, not nil.

Share this post


Link to post

Thanks for reply. It's no point to paste 400+ lines of code, I guess I was clear with this snippets. As you say, ped[1] should be something - I got null. If you can help me please reproduce this and say have you got back the element.

Share this post


Link to post

(edited answer)

You are inserting the ped by doing

table.insert( taxidf[player].ped, { ped } )

which makes a table inside the ped table.

To retrieve the ped, you'd need to do:

local ped = taxidf[player].ped[1][1];

But really, unless you need it for a specific reason, you should just change the insert line to:

table.insert( taxidf[player].ped, ped )

 

Edited by LopSided_

Share this post


Link to post

Haha, You haven't read carefully Lopsided_ - ped[0] is null. But I've found partially the solution: how to kill the ped

ped = createPed( tonumber( id ), x, y, z)
table.insert( taxidf[player].ped, { ped } )
x = taxidf[player].ped;
setTimer( killPed, 6000, 1, x[1][1] )

Couldn't believe ped[1][1] was the solution.  So it works if its in one place. Not, if it's in other functions.

It didn't help why I had null in further stage of my script but it's out of scope here, I mean it's not connected with tables themselves.

 

Edited by Salem
I was wrong

Share this post


Link to post

Why do you want to double  index,  when you are only save one ped at the time?

With other words, 2x indexing for nothing.

 

If you want to save multiple peds, then you still have to index 1x for nothing.

 

Share this post


Link to post

I don't understand why ped[1][1] works if it's in one place (like in code above) but doesn't when calling from other function (nor ped[1] doesn't). the table is global variable in server-side script and I've used such approach earlier in my scripts and it was ok, but here is not. I really don't know what gremlin is in my code. It looks like initializing this table ( taxidf = { { } } ) in one function doesn't save it on global scope. But in all previous scripts it was ok. The only difference here is that I put objects inside not values. Can it be a bug? What kind? Maybe I have some baby mistake?

Share this post


Link to post
--store
taxidf[player].ped = ped 

--load
local ped = taxidf[player].ped

--validate 
if isElement(ped) then
  
end

No need for more code.

Share this post


Link to post

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.