aka Blue Posted August 2, 2015 Share Posted August 2, 2015 (edited) Buenas, estoy haciendo un pequeño script de barreras (bastante simple) y querría saber como puedo asignarle a cada barrera creada un value o ID con el cual, posteriormente poder borrarla. Dejo el código por aquí, el script será posteado en la comunidad de MTA una vez acabado: function ponerbarrera (thePlayer) local x, y, z = getElementPosition (thePlayer) local faccion = exports.factions:isPlayerInFaction (thePlayer, 2) if faccion then --SI ERES DE LA FACCIÓN, PASA ÉSTO barresita = createObject (1228, x + 1, y + 1, z, 0, 0, 50) exports.chat:me (thePlayer, "saca una barrera y la lleva consigo en ambas manos") attachElementToElement ( barresita, thePlayer) outputChatBox ("Creada la barrera", thePlayer, 0, 255, 120) --COMANDO QUE SUELTA LA BARRERA addCommandHandler ("soltarbarrera", function () detachElementFromElement ( barresita, thePlayer ) exports.chat:me (thePlayer, "suelta la barrera en el suelo.") end ) --COMANDO QUE BORRA LA BARRERA addCommandHandler ("quitarb", function () destroyElement (barresita) outputChatBox ("Has borrado la barrera", thePlayer, 255, 0, 0) end ) --SI NO ERES POLICÍA TE PASA ÉSTO else outputChatBox ("No eres policía", thePlayer, 255, 0 ,0) end end addCommandHandler ("barrera", ponerbarrera) Edited August 2, 2015 by Guest Link to comment
aka Blue Posted August 2, 2015 Author Share Posted August 2, 2015 ¿Creando algo parecido a ésto? barreras = {} Eso bien, pero, ¿después? Link to comment
Castillo Posted August 2, 2015 Share Posted August 2, 2015 Exacto. Luego, en lugar de hacer esto: barresita = createObject (1228, x + 1, y + 1, z, 0, 0, 50) Haces asi: barreras [ thePlayer ] = createObject (1228, x + 1, y + 1, z, 0, 0, 50) Link to comment
aka Blue Posted August 2, 2015 Author Share Posted August 2, 2015 Perfecto, muchas gracias. PD: Para que salga x id en el outputChatBox, ¿tiene que ser así? outputChatBox ("Has creado la barrera con la ID: "..barreras, thePlayer, 0, 255, 120) Link to comment
Castillo Posted August 2, 2015 Share Posted August 2, 2015 Ehm, la "ID" es el elemento del jugador. Si queres que sean IDs numericas, entonces tenes que usar table.insert para insertar el objeto en la tabla. Link to comment
aka Blue Posted August 2, 2015 Author Share Posted August 2, 2015 Bien, como editaste pues no pude leer bien jaja. Bueno, hice más o menos ésto, ¿estaría bien? barreras = {} function ponerbarrera (thePlayer) local x, y, z = getElementPosition (thePlayer) local faccion = exports.factions:isPlayerInFaction (thePlayer, 2) if faccion then --SI ERES DE LA FACCIÓN, PASA ÉSTO barreras [ value ] = createObject (1228, x + 1, y + 1, z, 0, 0, 50) table.insert(barreras, value) exports.chat:me (thePlayer, "saca una barrera y la lleva consigo en ambas manos") attachElementToElement ( barreras, thePlayer) outputChatBox ("Creada la barrera "..value, thePlayer, 0, 255, 120) --COMANDO QUE SUELTA LA BARRERA addCommandHandler ("soltarbarrera", function () detachElementFromElement ( barreras, thePlayer ) exports.chat:me (thePlayer, "suelta la barrera en el suelo.") end ) --COMANDO QUE BORRA LA BARRERA addCommandHandler ("quitarb", function () table.insert(barreras, value) destroyElement (barreras, value) outputChatBox ("Has borrado la barrera "..value, thePlayer, 255, 0, 0) end ) --SI NO ERES POLICÍA TE PASA ÉSTO else outputChatBox ("No eres policía", thePlayer, 255, 0 ,0) end end addCommandHandler ("barrera", ponerbarrera) Link to comment
Castillo Posted August 2, 2015 Share Posted August 2, 2015 No, esta mal eso, quieres que un jugador pueda crear una sola barrera o mas de una? Link to comment
aka Blue Posted August 2, 2015 Author Share Posted August 2, 2015 Más de una, exacto y que la pueda borrar usando el value de dicha barrera. Algo como una ID, vamos. Link to comment
Castillo Posted August 2, 2015 Share Posted August 2, 2015 Queres que el jugador elija que ID borrar? o que borre la ultima creada? Link to comment
aka Blue Posted August 2, 2015 Author Share Posted August 2, 2015 Que elija la ID la cual borrar, exacto. Link to comment
Castillo Posted August 2, 2015 Share Posted August 2, 2015 Tendrias que crear una tabla dentro de la tabla, usando el elemento del jugador que utilizo el comando ( thePlayer ), y luego insertas la barrera en esa sub-tabla. El problema seria para soltar la barrera, ya que no sabes cual barrera tiene en sus manos. Link to comment
aka Blue Posted August 2, 2015 Author Share Posted August 2, 2015 El script lo que hace es que al crear la barrera la asigna al jugador, luego pones soltarbarrera para dejarla en el suelo. Eso de meter una tabla dentro de otra, sería usar table.insert ¿cierto? PD: ¿Me podrían dar un pequeño ejemplo? Link to comment
DBY Posted August 2, 2015 Share Posted August 2, 2015 barreras = {} addCommandHandler("ponerbarrera", function(p) barreras[#barreras+1] = createObject() outputChatBox("El ID de la barrera creada es: " .. #barreras, p, 255, 255, 0) end ) addCommandHandler("borrarbarrera", function(p, cmd, id) if tonumber(id) then if barreras[id] ~= nil then destroyElement(barreras[id]) barreras[id] = nil else outputChatBox("El ID no coincide con ninguna barrera.", p, 255, 0, 0) end else outputChatBox("Syntax: /" .. cmd .. " [iD]", p, 255, 255, 255) end end ) Espero haber explicado bien, estoy escribiendo desde móvil. Link to comment
aka Blue Posted August 3, 2015 Author Share Posted August 3, 2015 Ahí voy a probar, muchas gracias por el código Link to comment
DBY Posted August 3, 2015 Share Posted August 3, 2015 Ahí voy a probar, muchas gracias por el código Si hay algún error, avisa. Te mando un MP con mi skype, me puedes agregar si tienes dudas. Link to comment
aka Blue Posted August 3, 2015 Author Share Posted August 3, 2015 Gracias Justo me salió un error. Resulta que no borra la barrera porque no le encuentra la ID. barreras = {} function ponerbarrera (thePlayer) local x, y, z = getElementPosition (thePlayer) local faccion = exports.factions:isPlayerInFaction (thePlayer, 2) if faccion then --SI ERES DE LA FACCIÓN, PASA ÉSTO barreras[#barreras+1] = createObject (1228, x + 1, y + 1, z, 0, 0, 50) exports.chat:me (thePlayer, "saca una barrera y la lleva consigo en ambas manos") outputChatBox ("Creada la barrera con ID "..#barreras, thePlayer, 0, 255, 120) --COMANDO QUE BORRA LA BARRERA addCommandHandler ("quitarb", function (thePlayer, cmd, id) if tonumber(id) then if barreras[id] ~= nil then destroyElement (barreras[id]) barreras[id] = nil else outputChatBox ("La ID no coincide con ninguna barrera.", thePlayer, 255, 0, 0 ) end end end ) --SI NO ERES POLICÍA TE PASA ÉSTO else outputChatBox ("No eres policía", thePlayer, 255, 0 ,0) end end addCommandHandler ("barrera", ponerbarrera) Link to comment
aka Blue Posted August 3, 2015 Author Share Posted August 3, 2015 En la consola nada, simplemente me da el error de: outputChatBox ("La ID no coincide con ninguna barrera.", thePlayer, 255, 0, 0 ) Link to comment
DBY Posted August 3, 2015 Share Posted August 3, 2015 (edited) Si que funciona: Edited August 3, 2015 by Guest Link to comment
DBY Posted August 3, 2015 Share Posted August 3, 2015 barreras = {} tipos = { ["amarilla"] = 3578, ["desvío"] = 1282, ["lateral"] = 1424, ["pequeña"] = 978 } addCommandHandler("ponerbarrera", function(p, cmd, tipo) if tipo and tipos[tipo] ~= nil then barreras[#barreras+1] = createObject(tipos[tipo], getElementPosition(p)) setElementDimension(barreras[#barreras], getElementDimension(p)) setElementInterior(barreras[#barreras], getElementInterior(p)) outputChatBox("El ID de la barrera creada es: " .. #barreras, p, 255, 255, 0) else outputChatBox("Syntax: " .. cmd .. " [amarilla/desvío/lateral/pequeña]", p, 255, 255, 255) end end ) addCommandHandler("borrarbarrera", function(p, cmd, id) local id = tonumber(id) if id then if barreras[id] ~= nil then destroyElement(barreras[id]) barreras[id] = nil outputChatBox("Borraste la barrera con ID: " .. id, p, 255, 255, 0) else outputChatBox("El ID no coincide con ninguna barrera.", p, 255, 0, 0) end else outputChatBox("Syntax: /" .. cmd .. " [iD]", p, 255, 255, 255) end end ) Ya está, no pude darte el código bien porque estaba en móvil y confunde, pero ya vi el error al entrar en el pc. Link to comment
aka Blue Posted August 3, 2015 Author Share Posted August 3, 2015 Si, ahora funciona, gracias Link to comment
Recommended Posts