Cocodrilo Posted January 24, 2014 Share Posted January 24, 2014 (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 Edited January 24, 2014 by Guest Link to comment
Anubhav Posted January 24, 2014 Share Posted January 24, 2014 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 Link to comment
Cocodrilo Posted January 24, 2014 Author Share Posted January 24, 2014 I don't think so I want to get the object to then destroy it and use outputChatBox at player. Link to comment
DiSaMe Posted January 24, 2014 Share Posted January 24, 2014 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 Link to comment
Anubhav Posted January 24, 2014 Share Posted January 24, 2014 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) Link to comment
DiSaMe Posted January 24, 2014 Share Posted January 24, 2014 Anubhav, please, stop, that doesn't make any sense... Link to comment
Cocodrilo Posted January 24, 2014 Author Share Posted January 24, 2014 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 Link to comment
Spajk Posted January 24, 2014 Share Posted January 24, 2014 Could you explain a bit more what you are trying to do? Link to comment
Cocodrilo Posted January 24, 2014 Author Share Posted January 24, 2014 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. Link to comment
Gallardo9944 Posted January 24, 2014 Share Posted January 24, 2014 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 } Link to comment
Cocodrilo Posted January 24, 2014 Author Share Posted January 24, 2014 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? Link to comment
Gallardo9944 Posted January 24, 2014 Share Posted January 24, 2014 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 Link to comment
Cocodrilo Posted January 24, 2014 Author Share Posted January 24, 2014 Thank you so much @ Gallardo9944 ! Link to comment
Smart. Posted January 24, 2014 Share Posted January 24, 2014 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 Link to comment
Gallardo9944 Posted January 24, 2014 Share Posted January 24, 2014 @Smart, everyone has his own style and way. Link to comment
Cocodrilo Posted January 24, 2014 Author Share Posted January 24, 2014 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 Link to comment
Moderators IIYAMA Posted January 24, 2014 Moderators Share Posted January 24, 2014 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 Link to comment
Cocodrilo Posted January 24, 2014 Author Share Posted January 24, 2014 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 Link to comment
DNL291 Posted January 25, 2014 Share Posted January 25, 2014 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? Link to comment
Cocodrilo Posted January 25, 2014 Author Share Posted January 25, 2014 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 Link to comment
DNL291 Posted January 25, 2014 Share Posted January 25, 2014 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. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now