Jump to content

Como escolher um index aleatorio da tabela


Recommended Posts

routes = {
	{1345.973, -1243.223, 13.488},
	{1371.559, -1234.667, 13.547},
	{1389.106, -1246.985, 13.547},
}

Como posso fazer pra escolher um index aleatório da tabela e adicionar ele para:
 

createMarker(x, y, z, "cylinder", 2.5, 255, 0, 0, 255, player)
createBlip(x, y, z, 43, player)

x, y e z do marker e do blip?

Não faço ideia de como usar o match.random nem como colocar o valor do index da tabela sem ser por:

for i,v in pairs (Tabela) do
    createObject ( 1210, v[1], v[2], v[3], 1, 0, 0, true)
    createMarker( v[1], v[2], v[3]-1, "cylinder", 1, 255, 0, 0, 35 )
    createBlip( v[1], v[2], v[3], 42, 2, 255, 255, 255, 255,  0, 1000 )
end



Preciso de ajuda/explicação de:

Como usar o valor de uma tabela em outro lugar por exemplo no XYZ de um marker
table{{1,2,3}}
createMarker (x, y, z, .......)

e como escolher o valor aleatório de uma tabela.

Enquanto aguardo resposta vou ficar pesquisando sobre o assunto ?
 

Como deleta um post? PQP vou começar a usar a wiki antes de abrir coisa aqui no forum

function table.random ( theTable )
    return theTable[math.random ( #theTable )]
end

https://wiki.multitheftauto.com/wiki/Table.random

Que burrice minha vei

Agora como eu posso arrumar o valor que retorna em algum lugar tipo o XYZ do createMarker?

Link to comment

Tá ainda não consegui fazer o que eu queria AHHHHHHHHHHHHHHHHH

    function table.random ( theTable )
        return theTable[math.random ( #theTable )]
    end

Essa parada não retorna o que eu quero, se eu colocar uma tabela assim:

name{1, 2, 3 , "x", "TOPUTOD+"}
Ela retorna certo, mas assim ela não retorna:
name{

    {1, 2, 3},
   {"x", 2, 4},
}

Como eu posso fazer ?

Link to comment
routes = {
	[1] = {1345.973, -1243.223, 13.488},
	[2] = {1371.559, -1234.667, 13.547},
	[3] = {1389.106, -1246.985, 13.547},
}


local RR = math.random(#routes)
createObject ( 1210, routes[RR][1], routes[RR][2], routes[RR][3], 1, 0, 0, true)
createMarker(routes[RR][1], routes[RR][2], routes[RR][3], "cylinder", 2.5, 255, 0, 0, 255, player)
createBlip(routes[RR][1], routes[RR][2], routes[RR][3], 43, player)

 

 

Edited by Tommy.
Link to comment
table = {}
markerJob = {
    {1366.15, -1275.507, 13.5},
}

routes = {
    [1] = {1345.973, -1243.223, 13.488},
    [2] = {1371.559, -1234.667, 13.547},
    [3] = {1389.106, -1246.985, 13.547},
}

function table.random ( theTable )
    return theTable[math.random ( #theTable )]
end

for i,v in pairs (markerJob) do
       createObject ( 1210, v[1], v[2], v[3], 1, 0, 0, true)
    createMarker( v[1], v[2], v[3]-1, "cylinder", 1, 255, 0, 0, 35 )
    createBlip( v[1], v[2], v[3], 42, 2, 255, 255, 255, 255,  0, 1000 )
    local c = createColSphere( v[1], v[2], v[3]+1, 1 )
    c.id = "showPanel"
end

addEventHandler( "onColShapeHit", root, function( player, dim )
    if source.id == "showPanel" and  player:getType() == "player" and dim and not table.player then
        triggerClientEvent(root, "showPanel", root)
    elseif table.player then
        outputChatBox("Você já está trabalhando.")
    end
end)

function startJob()
    local player = client
    if not player then return end
    if not table.player then
        table.player = {}
    end

    local RR = math.random(#routes)  -- deu erro  >  bad argument #1 to 'random' (interval is empty)

    table.player.car = createVehicle(482, 1360.201, -1274.34, 13.383)
    table.player.marker = createMarker(RR[1], RR[2], RR[3], "cylinder", 2.5, 255, 0, 0, 255, player)
    table.player.car = createBlip(RR[1], RR[2], RR[3], 43, 2, 0, 0, 0, 255, 0, 400, player)
    addEventHandler("onMarkerHit", table.player.marker, job)
    
end
addEvent("start", true)
addEventHandler("start", root, startJob)

function job()
    outputChatBox("AHHHHHHHHHHHHHHH")
end

addEventHandler( "onPlayerQuit", root,
    function()
        if table[source] then
            for k,v in pairs(table[source]) do
                if isElement(v) then destroyElement(v) end
            end
            table[source] = nil
        end
    end
)

bad argument #1 to 'random' (interval is empty)

Deu esse erro :/

Edited by Gaimo
Link to comment

Evite abrir outros tópicos, esse código já faz parte de um script que pediu ajuda em outro tópico, nesse caso você poderia ter postado lá.

Quanto à sua dúvida, para indexar a tabela:

routes = {
	{1345.973, -1243.223, 13.488}, -- index 1
	{1371.559, -1234.667, 13.547}, -- index 2
	{1389.106, -1246.985, 13.547}, -- index 3
}

-- pegando as posições do index 2:
local x, y, z = routes[2][1], routes[2][2], routes[2][3]

-- também pode usar a função unpack:
local x, y, z = unpack( routes[2] )

Digamos que você queira obter um índice aleatório, então você vai usar a função math.random na faixa dos índices da tabela (que é de 1 a 3):

local randomIndex = math.random(1, 3)

Mas e se você for modificar essa tabela a adicionar mais sub-tabelas? Você pode obter o último índice da tabela usando o símbolo #:

local comprimento_da_tabela = #routes

math.random com o valor do comprimento da tabela:

math.random(1, #routes)
--[[
	Você também pode fazer desta forma:
	math.random(#routes)

	o valor 1 já será utilizado por padrão nesse caso então vai dar no mesmo
]]

 

33 minutes ago, Gaimo said:

bad argument #1 to 'random' (interval is empty)

Sobre esse erro, parece ser com a tabela 'routes', outro erro no seu código é o valor 'RR' sendo indexado:

table.player.marker = createMarker(RR[1], RR[2], RR[3], "cylinder", 2.5, 255, 0, 0, 255, player)

table.player.car = createBlip(RR[1], RR[2], RR[3], 43, 2, 0, 0, 0, 255, 0, 400, player)

Sendo que não é uma tabela. O correto seria:

local rndLoc = routes[ math.random( #routes ) ]

table.player.marker = createMarker(rndLoc[1], rndLoc[2], rndLoc[3], "cylinder", 2.5, 255, 0, 0, 255, player)
table.player.car = createBlip(rndLoc[1], rndLoc[2], rndLoc[3], 43, 2, 0, 0, 0, 255, 0, 400, player)

 

  • Thanks 1
Link to comment
  • DNL291 locked this topic
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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