Jump to content

[HELP] How can i remove a data from my table?


Turbesz

Recommended Posts

I created a table on server side, and the players can insert data to the table on server side with a command. But i want to make a function which removes the player (who typed the cmd) data from the table, not all players data.

I tried table.remove in many ways but none of them worked... 

My current code, with the table.insert part:

local menteshez = {}

function teszt(thePlayer)
    local object=getElementsByType("object")
    for k, v in ipairs(object) do
        if getElementData(v,"owner") == thePlayer then
            local x,y,z = getElementPosition(v)
            local id = getElementModel(v)
            local int = getElementInterior(v)
            local dim = getElementDimension(v)
            local Rx,Ry,Rz = getElementRotation ( v )
            outputChatBox("saved")
            table.remove(menteshez,k) -- this does not do anything...
            table.insert(menteshez,{x,y,z,id,int,dim,Rx,Ry,Rz}) -- but this works fine
        end
    end   
end

addCommandHandler("save",teszt)

 

Edited by Turbesz
Link to comment
  • Moderators
8 minutes ago, Turbesz said:

I tried table.remove in many ways but none of them worked... 

You could link them together like this:

for k, v in ipairs(object) do
	if getElementData(v,"owner") == thePlayer then
    	--[[ ... ]]
		menteshez[v] = {x,y,z,id,int,dim,Rx,Ry,Rz} --insert/overwrite
	end
end

-- clean up
for element, data in pairs(menteshez) do
	if not isElement(element) then
		menteshez[element] = nil
	end
end

--------
-- or --
--------


menteshez = {} -- reset

for k, v in ipairs(object) do
	if getElementData(v,"owner") == thePlayer then
    	--[[ ... ]]
		table.insert(menteshez, {x,y,z,id,int,dim,Rx,Ry,Rz}) 
	end
end

Both formats have their own benefits.

 

  • Thanks 1
Link to comment
3 minutes ago, IIYAMA said:

You could link them together like this:


for k, v in ipairs(object) do
	if getElementData(v,"owner") == thePlayer then
    	--[[ ... ]]
		menteshez[v] = {x,y,z,id,int,dim,Rx,Ry,Rz} --insert/overwrite
	end
end

-- clean up
for element, data in pairs(menteshez) do
	if not isElement(element) then
		menteshez[element] = nil
	end
end

--------
-- or --
--------


menteshez = {} -- reset

for k, v in ipairs(object) do
	if getElementData(v,"owner") == thePlayer then
    	--[[ ... ]]
		table.insert(menteshez, {x,y,z,id,int,dim,Rx,Ry,Rz}) 
	end
end

Both formats have their own benefits.

 

Huh, thank you! :D

I use Ceeser's xmldata script to save to xml, and now with the new method the xml save does not work
i got this warning: warning:  checkDataType for xmlSaveData - MTA elements should be stored since they have a temporary ID! Got userdata

wth?

this is my code:

local xml = exports.xmldata

function teszt(thePlayer)
    local object=getElementsByType("object")
    for element, data in pairs(menteshez) do
        if not isElement(element) then
            menteshez[element] = nil
        end
    end
    for k, v in ipairs(object) do
        if getElementData(v,"owner") == thePlayer then
            local x,y,z = getElementPosition(v)
            local id = getElementModel(v)
            local int = getElementInterior(v)
            local dim = getElementDimension(v)
            local Rx,Ry,Rz = getElementRotation ( v )
            outputChatBox("saved")
            menteshez[v] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz}
        end
    end

    xml:xmlSaveData("save", menteshez, false, true)        

end

addCommandHandler("save",teszt)

 

  • Like 1
Link to comment
39 minutes ago, Turbesz said:

Huh, thank you! :D

I use Ceeser's xmldata script to save to xml, and now with the new method the xml save does not work
i got this warning: warning:  checkDataType for xmlSaveData - MTA elements should be stored since they have a temporary ID! Got userdata

wth?

this is my code:


local xml = exports.xmldata

function teszt(thePlayer)
    local object=getElementsByType("object")
    for element, data in pairs(menteshez) do
        if not isElement(element) then
            menteshez[element] = nil
        end
    end
    for k, v in ipairs(object) do
        if getElementData(v,"owner") == thePlayer then
            local x,y,z = getElementPosition(v)
            local id = getElementModel(v)
            local int = getElementInterior(v)
            local dim = getElementDimension(v)
            local Rx,Ry,Rz = getElementRotation ( v )
            outputChatBox("saved")
            menteshez[v] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz}
        end
    end

    xml:xmlSaveData("save", menteshez, false, true)        

end

addCommandHandler("save",teszt)

 

now i tried change menteshez[element] to table.remove and menteshez[v] to table.insert in loops so now i don't get any warning from the xmldata script but the remove does not work properly in my script again :| 

Link to comment
  • Moderators
10 minutes ago, Turbesz said:

now i tried change menteshez[element] to table.remove and menteshez[v] to table.insert in loops so now i don't get any warning from the xmldata script but the remove does not work properly in my script again :| 

 

You can reformat the table with an array structure:

function reformatToArray (theTable)
	local array = {}
	for k, data in pairs(theTable) do
		table.insert(array, data)
	end
	return array
end

 

Link to comment
56 minutes ago, IIYAMA said:

 

You can reformat the table with an array structure:




function reformatToArray (theTable)
	local array = {}
	for k, data in pairs(theTable) do
		table.insert(array, data)
	end
	return array
end

 

i tried this: 

            table.insert(menteshez,{x,y,z,id,owner,int,dim,Rx,Ry,Rz})
            reformatToArray(menteshez)

but the remove in this loop:

    for element, data in pairs(menteshez) do
        if not isElement(element) then
            table.remove(menteshez,element)
        end
    end

still not work properly

Edited by Turbesz
Link to comment
  • Moderators
5 minutes ago, Turbesz said:

still not work properly

That is because the top one creates a format like this:

table.insert(menteshez,{x,y,z,id,owner,int,dim,Rx,Ry,Rz})

=

{
	[1] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz},
	[2] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz},
	[3] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz}
}

 

And the bottom one requires a format like this:

{
	[element] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz},
	[element] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz},
	[element] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz}
}

Which is achieved by doing this:

menteshez[v] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz}

 

Link to comment
3 minutes ago, IIYAMA said:

That is because the top one creates a format like this:



table.insert(menteshez,{x,y,z,id,owner,int,dim,Rx,Ry,Rz})

=



{
	[1] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz},
	[2] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz},
	[3] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz}
}

 

And the bottom one requires a format like this:



{
	[element] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz},
	[element] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz},
	[element] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz}
}

Which is achieved by doing this:



menteshez[v] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz}

 

i know buut because i use Ceeser's xmldata script to save the datas to XML, i got this warning:

2 hours ago, Turbesz said:

warning:  checkDataType for xmlSaveData - MTA elements should be stored since they have a temporary ID! Got userdata

with this code:

local xml = exports.xmldata

function teszt(thePlayer)
    local object=getElementsByType("object")
    for element, data in pairs(menteshez) do
        if not isElement(element) then
            menteshez[element] = nil
        end
    end
    for k, v in ipairs(object) do
        if getElementData(v,"owner") == thePlayer then
            local x,y,z = getElementPosition(v)
            local id = getElementModel(v)
            local int = getElementInterior(v)
            local dim = getElementDimension(v)
            local Rx,Ry,Rz = getElementRotation ( v )
            outputChatBox("saved")
            menteshez[v] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz}
        end
    end

    xml:xmlSaveData("save", menteshez, false, true)        

end

addCommandHandler("save",teszt)

 

Edited by Turbesz
Link to comment
  • Moderators
12 minutes ago, Turbesz said:

with this code:

You got that error, because you can't save `elements`   in xml.

{

   [element] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz},

}

 

 

That is why I gave you a function for reformatting:

xml:xmlSaveData("save", reformatToArray(menteshez), false, true) 

 

Or use the second option, then you do not have to reformat:

menteshez = {} -- reset

for k, v in ipairs(object) do
	if getElementData(v,"owner") == thePlayer then
    	--[[ ... ]]
		table.insert(menteshez, {x,y,z,id,int,dim,Rx,Ry,Rz}) 
	end
end
Edited by IIYAMA
Link to comment
5 minutes ago, IIYAMA said:

That is why I gave you a function for reformatting:


xml:xmlSaveData("save", reformatToArray(menteshez), false, true) 

 

Or use the second option, then you do not have to reformat:


menteshez = {} -- reset

for k, v in ipairs(object) do
	if getElementData(v,"owner") == thePlayer then
    	--[[ ... ]]
		table.insert(menteshez, {x,y,z,id,int,dim,Rx,Ry,Rz}) 
	end
end

thanks, now works

but if another player save the data, mine data will also be deleted, how to fix this?

Link to comment
  • Moderators
3 minutes ago, Turbesz said:

thanks, now works

but if another player save the data, mine data will also be deleted, how to fix this?

 

By saving and loading data for each player, there are already solutions out there that do just that.

For example this one (an alternative for element data, but without deep-copy):

setCustomData(player, "menteshez", menteshez, true) -- localized

local menteshez = getCustomData(player, "menteshez", true) -- localized

 

 

 

Link to comment
5 minutes ago, IIYAMA said:

 

By saving and loading data for each player, there are already solutions out there that do just that.

For example this one (an alternative for element data, but without deep-copy):


setCustomData(player, "menteshez", menteshez, true) -- localized

local menteshez = getCustomData(player, "menteshez", true) -- localized

 

 

 

um, where should i put these lines in my code?

setCustomData(player, "menteshez", menteshez, true) -- localized

local menteshez = getCustomData(player, "menteshez", true) -- localized

 

Link to comment

i think this loop cause the problem:

    for element, data in pairs(menteshez) do
        if not isElement(element) then
            menteshez[element] = nil
        end
    end

i tried to get inserted acc name and then remove the objects created by local player, but same problem. remove all player objects, not just local

    for element, data in pairs(menteshez) do
    if data[5] == accname then
        if not isElement(element) then
            menteshez[element] = nil
        end
    end
    end

what wrong?

Link to comment
  • Moderators
3 hours ago, Turbesz said:

um, where should i put these lines in my code?

First add the code from the tutorial.

And then use these lines at the place were you access menteshez.

Which should be a new table for each player.

if not getCustomData(player, "menteshez", true) then
	setCustomData(player, "menteshez", {}, true) 
end

 

Load:

local menteshez = getCustomData(player, "menteshez", true) 

 

Save:

setCustomData(player, "menteshez", menteshez, true)

 

2 hours ago, Turbesz said:

what wrong?

Each player has an unique table, so that modification is no necessary.

 


 

See, how it is saved in xml with the accountName:

local menteshez = getCustomData(player, "menteshez", true) 

local account = getPlayerAccount(player)
local accountName = getAccountName(account)

xml:xmlSaveData("save:" .. accountName, menteshez, false, true)        

 

 

You can also directly save and write, maybe that is easier:

local account = getPlayerAccount(player)
local accountName = getAccountName(account)

local menteshez = xml:xmlLoadData("save:" .. accountName, true)       

xml:xmlSaveData("save:" .. accountName, menteshez, false, true)       

-------------------

-- Account initialization

local account = getPlayerAccount(player)
local accountName = getAccountName(account)

local menteshez = xml:xmlLoadData("save:" .. accountName, true)
if not menteshez then
  xml:xmlSaveData("save:" .. accountName, {}, false, true)  
end

 

 

 

 

 

 

 

 

Edited by IIYAMA
Link to comment
48 minutes ago, IIYAMA said:

First add the code from the tutorial.

And then use these lines at the place were you access menteshez.

Which should be a new table for each player.



if not getCustomData(player, "menteshez", true) then
	setCustomData(player, "menteshez", {}, true) 
end

 

Load:



local menteshez = getCustomData(player, "menteshez", true) 

 

Save:



setCustomData(player, "menteshez", menteshez, true)

 

Each player has an unique table, so that modification is no necessary.

 


 

See, how it is saved in xml with the accountName:



local menteshez = getCustomData(player, "menteshez", true) 

local account = getPlayerAccount(player)
local accountName = getAccountName(account)

xml:xmlSaveData("save:" .. accountName, menteshez, false, true)        

 

 

You can also directly save and write, maybe that is easier:



local account = getPlayerAccount(player)
local accountName = getAccountName(account)

local menteshez = xml:xmlLoadData("save:" .. accountName, true)       

xml:xmlSaveData("save:" .. accountName, menteshez, false, true)       

-------------------

-- Account initialization

local account = getPlayerAccount(player)
local accountName = getAccountName(account)

local menteshez = xml:xmlLoadData("save:" .. accountName, true)
if not menteshez then
  xml:xmlSaveData("save:" .. accountName, {}, false, true)  
end

 

 

 

 

 

 

 

 

i added the code from tutorial, then i tried this: 

local menteshez = {}
local xml = exports.xmldata

function teszt(thePlayer)
    local object=getElementsByType("object")
    local theAcc = getPlayerAccount(thePlayer)
    local accname = getAccountName(theAcc)

    
        if not getCustomData(thePlayer, "menteshez", true) then
            setCustomData(thePlayer, "menteshez", {}, true) 
        end

    for k, v in ipairs(object) do
        if getElementData(v,"owner") == accname then
            local x,y,z = getElementPosition(v)
            local id = getElementModel(v)
            local owner = getElementData(v,"owner") or getAccountName(theAcc)
            local int = getElementInterior(v)
            local dim = getElementDimension(v)
            local Rx,Ry,Rz = getElementRotation ( v )
            local masikid = getElementData(v,"id")
            outputChatBox("mentve")
            menteshez = {x,y,z,id,owner,int,dim,Rx,Ry,Rz}
            setCustomData(thePlayer, "menteshez", menteshez, true)
        end
    end        
    local menteshez = getCustomData(thePlayer, "menteshez", true) 

    local account = getPlayerAccount(thePlayer)
    local accountName = getAccountName(account)
    
    xml:xmlSaveData("save:" .. accountName, menteshez, false, true)
end

addCommandHandler("save",teszt)

and i got this error: Error @xmlSaveData, arg 2 - table for tblData expected, got nil

Edited by Turbesz
Link to comment
  • Moderators
50 minutes ago, Turbesz said:

menteshez = {x,y,z,id,owner,int,dim,Rx,Ry,Rz}

This line is at-least incorrect.

You have to pick one of these methods:

You can't do neither.


1 hour ago, Turbesz said:

and i got this error: Error @xmlSaveData, arg 2 - table for tblData expected, got nil

You will have to check if this is working correctly:

if not getCustomData(thePlayer, "menteshez", true) then
	setCustomData(thePlayer, "menteshez", {}, true) 
end

 

 

 

Link to comment
7 minutes ago, IIYAMA said:

You have to pick one of these methods:

now i changed this line

menteshez = {x,y,z,id,owner,int,dim,Rx,Ry,Rz}

to this:

menteshez[v] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz}

now i don't get any errors, warnings but the xml:xmlSaveData("save:" .. accountName, menteshez, false, true) line does not work, does not create the file 

and when i want to load with this code:

function teszt2(thePlayer)
    local account = getPlayerAccount(thePlayer)
    local accname = getAccountName(account)
    
    local menteshez = xml:xmlLoadData("save:" .. accname, true)
    if not menteshez then
      xml:xmlSaveData("save:" .. accname, {}, false, true)  
    end
    local object=getElementsByType("object")


    for i,v in ipairs(getElementsByType("object")) do 
        if getElementData(v,"owner") == accname then
            destroyElement(v)
        end
    end
    for k, v in ipairs(menteshez) do
        if v[5] == accname then
        
    
        targy = createObject(v[4], v[1], v[2], v[3])
        
        setElementData(targy, "owner", accname)
        setElementInterior(targy,v[6])
        setElementDimension(targy,v[7])
        setElementRotation(targy,v[8],v[9],v[10])
        outputChatBox("betöltve")

        end
    end
end

then i got this error: bad argument #1 to ipairs (table expected got nil) >> line 17

Link to comment
  • Moderators
1 hour ago, Turbesz said:

now i don't get any errors, warnings but the xml:xmlSaveData("save:" .. accountName, menteshez, false, true) line does not work, does not create the file 

Are you sure it is not located at a different location?

 

If there is no data, there is nothing to load. So everything beyond this point should not be executed.

local menteshez = xml:xmlLoadData("save:" .. accname, true)
if not menteshez then
	xml:xmlSaveData("save:" .. accname, {}, false, true)  
	return -- stop the function.
end

 

 

 

Link to comment
3 minutes ago, IIYAMA said:

Are you sure it is not located at a different location?

The xml file? Im sure, because this is a server sided script, need it should be there in the xmldata folder in the script, and give a feedback in the debugscript (it doesn't happen too). :/ 

What wrong with that line?

 

 

Link to comment
  • Moderators
5 minutes ago, Turbesz said:

Yes, i checked it already, so this is not the problem :/ 

 

The double dots might be the problem:

xml:xmlLoadData("save:" .. accname, true)

Try to replace them with something else.

Link to comment
5 minutes ago, IIYAMA said:

 

The double dots might be the problem:

xml:xmlLoadData("save:" .. accname, true)

Try to replace them with something else.

now without double dots i got this error in save func: Error @xmlSaveData, arg 2 - table for tblData expected, got nil

local menteshez = {}
local xml = exports.xmldata

function teszt(thePlayer)
    local object=getElementsByType("object")
    local theAcc = getPlayerAccount(thePlayer)
    local accname = getAccountName(theAcc)

    
        if not getCustomData(thePlayer, "menteshez", true) then
            setCustomData(thePlayer, "menteshez", {}, true) 
        end

    for k, v in ipairs(object) do
        if getElementData(v,"owner") == accname then
            local x,y,z = getElementPosition(v)
            local id = getElementModel(v)
            local owner = getElementData(v,"owner") or getAccountName(theAcc)
            local int = getElementInterior(v)
            local dim = getElementDimension(v)
            local Rx,Ry,Rz = getElementRotation ( v )
            outputChatBox("mentve")
            menteshez[v] = {x,y,z,id,owner,int,dim,Rx,Ry,Rz}
            setCustomData(thePlayer, "menteshez", menteshez, true)
        end
    end    
    local menteshez = getCustomData(thePlayer, "menteshez", true) 
    
    xml:xmlSaveData("save" .. accname, menteshez, false, true)

end

addCommandHandler("save",teszt)

 

Link to comment
  • Moderators
3 minutes ago, Turbesz said:

now without double dots i got this error in save func: Error @xmlSaveData, arg 2 - table for tblData expected, got nil

You didn't apply my previous feedback, that is why you got that error:

 

Link to comment
7 minutes ago, IIYAMA said:

You didn't apply my previous feedback, that is why you got that error:

 

uh that's true, my bad!

now i don't get any errors or warnings, and the file was created, but does not insert the script any data to that file, i just get only "<root></root>" as a result in my .xml file

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