Jump to content

get an element from a table in a function.


Cocodrilo

Recommended Posts

Posted (edited)

How to get an element from a table in a función?

for example:

table1 = { 
{-1434, 2668, 55.68 }, 
{-1460, 2669, 55.66 }, 
} 
  
for_,v in pairs ( table1 ) do 
     object = createObject ( 929, v[1], v[2], v[3] ) 
end 
  
function () 
   --get the object created from the table 
end 

anyone can help me please :cry:

Edited by Guest
560x95_FFFFFF_FF9900_000000_000000.png
Posted
  
table1 = { 
{-1434, 2668, 55.68 }, 
{-1460, 2669, 55.66 }, 
} 
  
for_,v in pairs ( table1 ) do 
     object = createObject ( 929, v[1], v[2], v[3] ) 
end 
  
function () 
getElementData(thePlayer,table1)  
end 
  
  

Posted

If you want to get the value from the table, then you must store it into the table first

object_table = {} 
  
--creating and storing the object 
object_table[some_index] = createObject(...) 
  
--destroying the object 
destroyElement(object_table[some_index]) 
object_table[some_index] = nil -- although object no longer exists, its identifier value is still there, so we assign nil to remove that value and free the memory 

-

Posted

Here is it:

I Dint tested it.

  
table1 = { 
{-1434, 2668, 55.68 }, 
{-1460, 2669, 55.66 }, 
} 
  
for_,v in pairs ( table1 ) do 
     object = createObject ( 929, v[1], v[2], v[3] ) 
  
  
     function destroyElement() 
getElementData(thePlayer,table1) 
destroyElement(object) 
outputChatBox("Your object is destroyed.",thePlayer,255,255,255) 
end 
  
  

NOTE: if your object is table1 it will destroy it or change the destroyElement(element name you want to destroy)

Posted

something like this?

table1 = { 
{-1434, 2668 , 55.68 }, 
{-1460, 2669, 55.66 }, 
} 
  
local obj = {} 
  
addEventHandler ( "onResourceStart", resourceRoot, 
      function()  
           for_,v in pairs ( table1 ) do 
                 local object = createObject ( 929, v[1], v[2], v[3] ) 
                 obj [ooob] = object 
                  destroy() 
           end 
      end 
) 
  
function destroy () 
           setTimer(function() 
                   destroyElement(obj[ooob]) 
                   obj [ooob] = nil 
           end, 5000, 1) 
end 
    

i do not understand at all yet

560x95_FFFFFF_FF9900_000000_000000.png
Posted
Could you explain a bit more what you are trying to do?

That isn't really i want to do.. I just want to know how to get an object created from a table to then call such object in a function..

for this example.. create the object onResourceStart and call the function ' destroy() ' to destroy it after 5 seconds.

560x95_FFFFFF_FF9900_000000_000000.png
Posted
local table1 = { 
{-1434, 2668, 55.68 }, 
{-1460, 2669, 55.66 }, 
} 
  
local table2 = { }  
  
for i,v in ipairs (table1) do 
     local object = createObject(929,unpack(v)) -- create object with your info 
     table.insert(table2,object) 
end 
  
function getObjectFromTable(num) 
  return table2[num] --get the object created from the table 
end 
  
local object1 = getObjectFromTable(1) -- {-1434, 2668, 55.68 } 
local object2 = getObjectFromTable(2) -- {-1460, 2669, 55.66 } 
  

Posted

you don't understand me, or maybe i didn't explain the right way..

i want to do this..

items = { 
{-1434, 2668 , 55.68 }, 
{-1460, 2669, 55.66 }, 
} 
  
addEventandler ( "onResourceStart", resourceRoot, 
      function()  
           for_,v in pairs ( items ) do 
                 local object = createObject ( 929, v[1], v[2], v[3] ) 
                    setTimer(destroy, 5000, 1)            
           end 
      end 
) 
  
function destroy () 
          destroyElement(object) 
end 
   

but that's not possible cuz destroyElement(object) got a wrong element.

I just want to destroy the object from the table 5 seconds after has been created with a function! how to get the object element?

560x95_FFFFFF_FF9900_000000_000000.png
Posted
you don't understand me, or maybe i didn't explain the right way..

i want to do this..

items = { 
{-1434, 2668 , 55.68 }, 
{-1460, 2669, 55.66 }, 
} 
  
addEventandler ( "onResourceStart", resourceRoot, 
      function()  
           for_,v in pairs ( items ) do 
                 local object = createObject ( 929, v[1], v[2], v[3] ) 
                    setTimer(destroy, 5000, 1)            
           end 
      end 
) 
  
function destroy () 
          destroyElement(object) 
end 
   

but that's not possible cuz destroyElement(object) got a wrong element.

I just want to destroy the object from the table 5 seconds after has been created with a function! how to get the object element?

local table1 = { 
{-1434, 2668, 55.68 }, 
{-1460, 2669, 55.66 }, 
} 
  
local table2 = { }  
  
for i,v in ipairs(table1) do 
  local object = createObject(929,unpack(v)) -- create object with your info 
  table.insert(table2,object) 
end 
  
function onStart() 
  setTimer(doDestroyObjects,5000,1) -- set timer when the resource starts 
end 
addEventandler("onResourceStart",resourceRoot,onStart) 
  
function doDestroyObjects() 
  for i,v in ipairs(table2) do 
    if isElement(v) then 
      destroyElement(v) 
    end 
    table2[i] = nil 
  end 
end 
  

Posted

Wouldn't it be easier to just pass the object element in setTimer as an argument?

items = { 
{-1434, 2668 , 55.68 }, 
{-1460, 2669, 55.66 }, 
} 
  
addEventHandler ( "onResourceStart", resourceRoot, 
    function()  
        for_,v in pairs ( items ) do 
            local object = createObject ( 929, v[1], v[2], v[3] ) 
            setTimer(destroy, 5000, 1, object)           
        end 
    end 
) 
  
function destroy(element) 
    if (not isElement(element)) then return end 
    destroyElement(element) 
end 

lol

Posted

and if i want to change to bindKey instead of setTimer?

items = { 
{-1434, 2668 , 55.68 }, 
{-1460, 2669, 55.66 }, 
} 
  
addEventHandler ( "onResourceStart", resourceRoot, 
    function() 
        for _,v in pairs ( items ) do 
            local object = createObject ( 929, v[1], v[2], v[3] ) 
            --[[ Change to bindKey instead of 
                setTimer(destroy, 5000, 1, object)     
                    ]] 
        end 
    end 
) 
  
function destroy(element) 
    if (not isElement(element)) then return end 
    destroyElement(element) 
end 

560x95_FFFFFF_FF9900_000000_000000.png
  • Moderators
Posted
bindKey("x","down",function () 
        for _,v in pairs ( items ) do 
            local object = createObject ( 929, v[1], v[2], v[3] ) 
        end 
  
end) 

Btw, this is lagg, creating for every object a timer...... :?
        for _,v in pairs ( items ) do 
            local object = createObject ( 929, v[1], v[2], v[3] ) 
            --[[ Change to bindKey instead of 
                setTimer(destroy, 5000, 1, object)     
                    ]] 
        end 

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted
bindKey("x","down",function () 
        for _,v in pairs ( items ) do 
            local object = createObject ( 929, v[1], v[2], v[3] ) 
        end 
  
end) 

Btw, this is lagg, creating for every object a timer......
        for _,v in pairs ( items ) do 
            local object = createObject ( 929, v[1], v[2], v[3] ) 
            --[[ Change to bindKey instead of 
                setTimer(destroy, 5000, 1, object)     
                    ]] 
        end 

mm.. i don't want to create object with a timer, the objects are created when the resource starts. So I wanna destroy them after have been created with a bindKey.

This works:

items = {  
  
{ -1432.12890625, 2672.3349609375, 55.691806793213 }, 
{-1428.2822265625, 2665.9150390625, 55.6875 }, 
  
} 
  
addEventHandler ( "onResourceStart", resourceRoot, 
    function() 
        for _,v in pairs ( items ) do 
            local object = createObject ( 929, v[1], v[2], v[3] ) 
            setTimer(destroy, 5000, 1, object)           
        end 
    end 
) 
  
function destroy(element) 
    if (not isElement(element)) then return end 
    destroyElement(element) 
end 

But i want to do something like this:

items = {  
  
{ -1432.12890625, 2672.3349609375, 55.691806793213 }, 
{-1428.2822265625, 2665.9150390625, 55.6875 }, 
  
} 
  
addEventHandler ( "onResourceStart", resourceRoot, 
    function() 
        for _,v in pairs ( items ) do 
            local object = createObject ( 929, v[1], v[2], v[3] ) 
            setTimer(destroy, 5000, 1, object)           
        end 
    end 
) 
  
function destroy(element) 
    if (not isElement(element)) then return end 
        bindkey ( player, "x", "down", function() 
        destroyElement(element) 
        end) 
end 

560x95_FFFFFF_FF9900_000000_000000.png
Posted
  
items = { 
  
{ -1432.12890625, 2672.3349609375, 55.691806793213 }, 
{-1428.2822265625, 2665.9150390625, 55.6875 }, 
  
} 
  
addEventHandler ( "onResourceStart", resourceRoot, 
    function() 
        for _,v in ipairs ( items ) do 
            object = createObject ( 929, unpack(v) ) 
            setTimer(destroy, 5000, 1, object) 
        end 
    end 
) 
  
function destroy(element) 
    if isElement(element) then 
        for _,player in ipairs(getElementsByType("player")) do 
            bindKey(player, "x", "down", function() destroyElement(element) end) 
        end 
    end 
end 
  

Do you mean that?

Please do not PM me with scripting related question nor support, use the forums instead.

Posted
  
items = { 
  
{ -1432.12890625, 2672.3349609375, 55.691806793213 }, 
{-1428.2822265625, 2665.9150390625, 55.6875 }, 
  
} 
  
addEventHandler ( "onResourceStart", resourceRoot, 
    function() 
        for _,v in ipairs ( items ) do 
            object = createObject ( 929, unpack(v) ) 
            setTimer(destroy, 5000, 1, object) 
        end 
    end 
) 
  
function destroy(element) 
    if isElement(element) then 
        for _,player in ipairs(getElementsByType("player")) do 
            bindKey(player, "x", "down", function() destroyElement(element) end) 
        end 
    end 
end 
  

Do you mean that?

Yes! Thank you so much :)

560x95_FFFFFF_FF9900_000000_000000.png
Posted

You're welcome.

But, keep in mind that the onResourceStart event is called only when a resource is started. Therefore, it will not be called when a player join the server, in that case, you can use onClientResourceStart instead and change the code to the client-side. Or you can use the onPlayerJoin event.

Please do not PM me with scripting related question nor support, use the forums instead.

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