Jump to content

Asignar una ID o value a un objeto


aka Blue

Recommended Posts

Posted (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 by Guest

yDORrdn.png

Posted

Usa una tabla.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

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) 

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

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) 

yDORrdn.png

Posted

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.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

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) 

yDORrdn.png

Posted

No, esta mal eso, quieres que un jugador pueda crear una sola barrera o mas de una?

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

Queres que el jugador elija que ID borrar? o que borre la ultima creada?

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

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.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

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?

yDORrdn.png

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

Posted

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) 

yDORrdn.png

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...