Jump to content

[HELP] How to get data from my table?


Turbesz

Recommended Posts

I want get data from table, to use a object (move, etc.), but i don't know how to do this.

I tried this method:
 

local targy = {}
local cp = {}

function lerakja(thePlayer)
    local x, y, z = getElementPosition(thePlayer)
targy[thePlayer] = createObject(2167, x+1, y+1, z-1)
setElementData(targy[thePlayer], "owner", thePlayer)
end
addCommandHandler("obj",lerakja)

function  clickfunc ( button, state, player )
    if button == "left" and state == "down" then
        if getElementType ( targy[player] ) == "object" and getElementData(targy[player],"owner") == player then
            local x, y, z = getElementPosition(targy[player])

            outputChatBox("you are use now this object: ID: "..#targy[player])

            cp[player]= createMarker(x, y-0.2, z+2.5, "arrow", 0.7, 255, 181, 64, 250)
        end
    end
end
addEventHandler ( "onElementClicked", root, clickfunc ) 

The object spawn fine, and the cp spawn fine too above my object after i click. But if i spawn another object, the cp spawn above my object too again. This is fine, but i want to get data from that object which one I clicked to remove the previous cp from the previous object. 

How can i get data from my table to know how to do it this? (Sorry for my very bad english:/)

Link to comment
3 minutes ago, Tekken said:

Well count the id’s 


targy[player][ tonumber(#targy[player]) + 1 ] = element;

tonumber not needed normally but just to be sure

attempt to get length of field '?' (a nil value)

targy[player][tonumber(#targy[player]) + 1] = createObject(2167, x+1, y+1, z-1)

if i add this line without tonumber, i got the same error

Link to comment
2 minutes ago, Tekken said:

You define the table with the player first  targy[player] = {}

i got the same error with this, but if it had worked, then the table would be emptied, and if i try change "local targy = {}" to "local targy[source] = {}" out of the function, i got another error

Link to comment

Wait just do table[player][1] = object

And create a table.count function like this 

function tablecount(table)

    if type(table) == "table" then

        local count = 0;

        for i,v in ipairs(table) do

            count = count +1;

        end

        return count;

    end

    return false;

end

and do

table[player][(tablecount(table[player]) or 0) + 1]

Link to comment
6 minutes ago, Tekken said:

Wait just do table[player][1] = object

Like this?

local targy = {}

function lerakja(thePlayer)
    local x, y, z = getElementPosition(thePlayer)
    table[thePlayer] = createObject(2167, x+1, y+1, z-1)
setElementData(table[thePlayer], "owner", thePlayer)
end
addCommandHandler("obj",lerakja)

function clickfunc ( button, state, player )
    if button == "left" and state == "down" then
        if table[player][1] == source then
            local x, y, z = getElementPosition(table[player][1])
            outputChatBox(x.." | "..y.." | "..z)
        end
    end
end
addEventHandler ( "onElementClicked", root, clickfunc ) 

 

Link to comment

Just realized a thing 

function lerakja(thePlayer)
    local x, y, z = getElementPosition(thePlayer)
targy = createObject(2167, x+1, y+1, z-1)
setElementData(targy, "owner", thePlayer)
  addEventHandler ( "onElementClicked", targy, clickfunc ) 
end
addCommandHandler("obj",lerakja)

function  clickfunc ( button, state, player )
    if button == "left" and state == "down" then
        if getElementType (source ) == "object" and getElementData(source,"owner") == player then
            local x, y, z = getElementPosition(source)
      local model = getElementModel(source)

            outputChatBox("you are use now this object: ID: "..model)

            cp = createMarker(x, y-0.2, z+2.5, "arrow", 0.7, 255, 181, 64, 250)
        end
    end
end

This should work

May I know what you are exactly trying to archive? Didn’t quite understand yet

Link to comment
6 minutes ago, Tekken said:

Just realized a thing 


function lerakja(thePlayer)
    local x, y, z = getElementPosition(thePlayer)
targy = createObject(2167, x+1, y+1, z-1)
setElementData(targy, "owner", thePlayer)
  addEventHandler ( "onElementClicked", targy, clickfunc ) 
end
addCommandHandler("obj",lerakja)

function  clickfunc ( button, state, player )
    if button == "left" and state == "down" then
        if getElementType (source ) == "object" and getElementData(source,"owner") == player then
            local x, y, z = getElementPosition(source)
      local model = getElementModel(source)

            outputChatBox("you are use now this object: ID: "..model)

            cp = createMarker(x, y-0.2, z+2.5, "arrow", 0.7, 255, 181, 64, 250)
        end
    end
end

This should work

this works, but i need the table thing, because i want that if a player create more than 1 object, then he can interact with just one of them, if he click on the object

Link to comment
1 minute ago, Turbesz said:

this works, but i need the table thing, because i want that if a player create more than 1 object, then he can interact with just one of them, if he click on the object

Well the example I give you let’s you interact with only one object while creating many,doesn’t it?

Link to comment

Using the example I gave you? you sure? This one:

function lerakja(thePlayer)
	local x, y, z = getElementPosition(thePlayer);
	local targy = createObject(2167, x+1, y+1, z-1);
	setElementData(targy, "owner", thePlayer);
	addEventHandler("onElementClicked", targy, clickfunc);
end
addCommandHandler("obj", lerakja);

function clickfunc(button, state, player)
	if button == "left" and state == "down" then
		if getElementType (source ) == "object" and getElementData(source, "owner") == player then
			local x, y, z = getElementPosition(source);
			local model = getElementModel(source);
			outputChatBox("you are use now this object: ID: "..model);
			local cp = createMarker(x, y-0.2, z+2.5, "arrow", 0.7, 255, 181, 64, 250);
		end
	end
end

 

Link to comment
14 minutes ago, Tekken said:

Using the example I gave you? you sure? This one:


function lerakja(thePlayer)
	local x, y, z = getElementPosition(thePlayer);
	local targy = createObject(2167, x+1, y+1, z-1);
	setElementData(targy, "owner", thePlayer);
	addEventHandler("onElementClicked", targy, clickfunc);
end
addCommandHandler("obj", lerakja);

function clickfunc(button, state, player)
	if button == "left" and state == "down" then
		if getElementType (source ) == "object" and getElementData(source, "owner") == player then
			local x, y, z = getElementPosition(source);
			local model = getElementModel(source);
			outputChatBox("you are use now this object: ID: "..model);
			local cp = createMarker(x, y-0.2, z+2.5, "arrow", 0.7, 255, 181, 64, 250);
		end
	end
end

 

Yes, but i tried add the move object function:

function  clickfunc ( button, state, player )
    if button == "left" and state == "down" then
        if getElementType (source ) == "object" and getElementData(source,"owner") == player then
            local x, y, z = getElementPosition(source)
            local model = getElementModel(source)
            bindKey(player,"arrow_u","down",mozgatas)
            setElementData(source,"mozgathato",true)
        end
    end
end

function mozgatas(player)
if getElementData(targy,"owner") == player and getElementData(targy,"mozgathato") == true then
    local x,y,z = getElementPosition(targy)
setElementPosition(targy,x,y-0.5,z) 
end
end

i always can move only the new one with this, no matter if i click the old one

Link to comment
8 minutes ago, Tekken said:

use moveObject function to move it. just add it into clickfunc function 

The "onElementClicked" event should check if source has "mozgathato" elemetData 

if i add moveObject func in clickfunc then i can't move the object with my bindKey, no?

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