Jump to content

Random object spawn


Protagonist95

Recommended Posts

Hello guys,stuck on a problem.Heres code i wrote:

  
items = { 
    {1337,1356.69360,1546.84326,10.82031}, 
    {1337,1356.59802,1536.86560,10.8203}, 
    {1337,1368.62585,1547.30847,10.82031} 
} 
  
function spawnLoot() 
local object ,x, y, z = unpack ( items [ math.random ( #items ) ] ) 
createObject ( object,x,y,z,0,0,0) 
end 
addEventHandler ( "onResourceStart", getRootElement(), spawnLoot) 
  

problem is that script spawns only one object.What i want to do,is spawn multiple of them(for example spawn object at first and third point)

Thank you)

Link to comment
  
Object = {} 
X = {} 
Y = {} 
Z = {} 
  
Object[1] =  1 
X[1] =  1356.69360 
Y[1] =  1546.84326,10.82031 
Z[1] =  10.82031 
-------- 
Object[2] =  3 
X[2] =  1356.69360 
Y[2] =  1546.84326,10.82031 
Z[2] =  10.82031 
------- 
Object[3] =  1 
X[3] =  1356.69360 
Y[3] =  1546.84326,10.82031 
Z[3] =  10.82031 
 ----- 
ItemCount = 3 
  
  
function spawnLoot() 
randomItem = math.random (1, ItemCount) 
local object ,x, y, z = object[randomItem],X[randomItem],Y[randomItem],Z[randomItem] 
createObject ( object,x,y,z,0,0,0) 
end 
addEventHandler ( "onResourceStart", getRootElement(), spawnLoot) 
  

-- Put in random cords and object id, fill in your own --

As you add more objects you increase item account, and increase the number in [ ]

Link to comment
  • Moderators
function deepcopy(orig) 
    local orig_type = type(orig) 
    local copy 
    if orig_type == 'table' then 
        copy = {} 
        for orig_key, orig_value in next, orig, nil do 
            copy[deepcopy(orig_key)] = deepcopy(orig_value) 
        end 
        setmetatable(copy, deepcopy(getmetatable(orig))) 
    else -- number, string, boolean, etc 
        copy = orig 
    end 
    return copy 
end 

http://lua-users.org/wiki/CopyTable

items = { 
    {1337,1356.69360,1546.84326,10.82031}, 
    {1337,1356.59802,1536.86560,10.8203}, 
    {1337,1368.62585,1547.30847,10.82031} 
} 
  
function spawnLoot() 
    local items = deepcopy(items) -- copy the table, so we can edit it without editing the original. 
  
    for i=1, math.max(math.random(#items), math.ceil(#items*0.3)) do -- minimal 30% of the items count = 0.3. 
        local randomItemIndex = math.random ( #items ) 
        table.remove(items, randomItemIndex) -- remove it, so you don't duplicate item/spawnpoint. 
        local objectID, x, y, z = unpack ( items[randomItemIndex]) 
        createObject ( objectID,x,y,z,0,0,0) 
    end 
end 
addEventHandler ( "onResourceStart", resourceRoot, spawnLoot) -- don't use the ROOTELEMENT! But resourceRoot! 

Link to comment
function deepcopy(orig) 
    local orig_type = type(orig) 
    local copy 
    if orig_type == 'table' then 
        copy = {} 
        for orig_key, orig_value in next, orig, nil do 
            copy[deepcopy(orig_key)] = deepcopy(orig_value) 
        end 
        setmetatable(copy, deepcopy(getmetatable(orig))) 
    else -- number, string, boolean, etc 
        copy = orig 
    end 
    return copy 
end 

http://lua-users.org/wiki/CopyTable

items = { 
    {1337,1356.69360,1546.84326,10.82031}, 
    {1337,1356.59802,1536.86560,10.8203}, 
    {1337,1368.62585,1547.30847,10.82031} 
} 
  
function spawnLoot() 
    local items = deepcopy(items) -- copy the table, so we can edit it without editing the original. 
  
    for i=1, math.max(math.random(#items), math.ceil(#items*0.3)) do -- minimal 30% of the items count = 0.3. 
        local randomItemIndex = math.random ( #items ) 
        table.remove(items, randomItemIndex) -- remove it, so you don't duplicate item/spawnpoint. 
        local objectID, x, y, z = unpack ( items[randomItemIndex]) 
        createObject ( objectID,x,y,z,0,0,0) 
    end 
end 
addEventHandler ( "onResourceStart", resourceRoot, spawnLoot) -- don't use the ROOTELEMENT! But resourceRoot! 

local objectID, x, y, z = unpack(items[randomItemIndex])---------table expected got nil

Link to comment
  • Moderators

This should do it.

    items = { 
        {1337,1356.69360,1546.84326,10.82031}, 
        {1337,1356.59802,1536.86560,10.8203}, 
        {1337,1368.62585,1547.30847,10.82031} 
    } 
      
    function spawnLoot() 
        local items = deepcopy(items) -- copy the table, so we can edit it without editing the original. 
      
        for i=1, math.max(math.random(#items), math.ceil(#items*0.3)) do -- minimal 30% of the items count = 0.3. 
            local randomItemIndex = math.random ( #items ) 
            local objectID, x, y, z = unpack ( items[randomItemIndex]) 
            table.remove(items, randomItemIndex) -- remove it, so you don't duplicate item/spawnpoint. 
            createObject ( objectID,x,y,z,0,0,0) 
        end 
    end 
    addEventHandler ( "onResourceStart", resourceRoot, spawnLoot) -- don't use the ROOTELEMENT! But resourceRoot! 

Swapped the table.remove line, with the table unpack line.

Link to comment

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