Jump to content

Random object spawn


Anubhav

Recommended Posts

Posted

Hey everyone!

I was wondering, if it's possible to spawn random on ground or buildings, without giving X,Y,Z it will keep spawning objects on grounds.

I hope so a solution is there.

Posted

The method din't work.

It has bugs, it's just making objects too high in the air

  
items = { 
    "Water Bottle", 
    "Tea", 
    "Fence", 
    'Morphine', 
    "Pizza", 
    "Chicken Burger", 
    "Pepsi", 
    "Fanta", 
    "Engine", 
    "Wheel" 
} 
function getXY( ) 
    return math.random(9999), math.random(9999), math.random(9999) 
end 
  
function spawnObjects( ) 
    local x, y, z = getXY( ) 
    z = getGroundPosition( x, y, z ) 
--  local x, y, z =  
    local object = createObject( 1271, x, y, z + 0.3 ) 
    setElementData( object, "item", items[ math.random( #items ) ] ) 
    createBlip( 1, x, y, z ) 
    setTimer( spawnObjects, 3000, 1 ) 
end 
spawnObjects( ) 
  

Posted

Because minimal x and y is -1500 and maximum 1500. Try this

items = { 
    "Water Bottle", 
    "Tea", 
    "Fence", 
    'Morphine', 
    "Pizza", 
    "Chicken Burger", 
    "Pepsi", 
    "Fanta", 
    "Engine", 
    "Wheel" 
} 
function getXY( ) 
    return math.random(-1500, 1500), math.random(-1500, 1500), 0 
end 
  
function spawnObjects( ) 
    local x, y, z = getXY( ) 
    z = getGroundPosition( x, y, z ) 
    local object = createObject( 1271, x, y, z + 0.3 ) 
    setElementData( object, "item", items[ math.random( #items ) ] ) 
    createBlip( 1, x, y, z ) 
    setTimer( spawnObjects, 3000, 1 ) 
end 
spawnObjects( ) 

Posted
Because minimal x and y is -1500 and maximum 1500. Try this
items = { 
    "Water Bottle", 
    "Tea", 
    "Fence", 
    'Morphine', 
    "Pizza", 
    "Chicken Burger", 
    "Pepsi", 
    "Fanta", 
    "Engine", 
    "Wheel" 
} 
function getXY( ) 
    return math.random(-1500, 1500), math.random(-1500, 1500), 0 
end 
  
function spawnObjects( ) 
    local x, y, z = getXY( ) 
    z = getGroundPosition( x, y, z ) 
    local object = createObject( 1271, x, y, z + 0.3 ) 
    setElementData( object, "item", items[ math.random( #items ) ] ) 
    createBlip( 1, x, y, z ) 
    setTimer( spawnObjects, 3000, 1 ) 
end 
spawnObjects( ) 

it's -3000,3000 not -1500,1500

Posted
It doesn't matter for now, the problem is Z. It's not getting ground position properly. Any other function that I can use?

according to this

cae2cd8df9.png

and this

fb216ea934.png

z is always 0 it mean 0+0.3 = 0.3

Do not yield your back to your enemy, might feel something strange in your ass.

Two things are infinite the universe and human stupidity and i'm not sure about the universe.

UF: IsTextInGridList | GetGridListRowIndexFromText | Table.removeValue | removeHex | dxDrawTriangle

Skype: SaSuki102 | About Me | Youtube channel | Lua Tips & Tricks | Lua Strings | Lua Tables | Lua Operators

Posted (edited)
local Timers = {} 
items = { 
    "Water Bottle", 
    "Tea", 
    "Fence", 
    'Morphine', 
    "Pizza", 
    "Chicken Burger", 
    "Pepsi", 
    "Fanta", 
    "Engine", 
    "Wheel" 
} 
function getXY( ) 
    return math.random(-1500, 1500), math.random(-1500, 1500) 
end 
  
function spawnObjects( ) 
    local x, y = getXY( ) 
    local object = createObject( 1271, x, y, 0 ) 
    setElementData( object, "item", items[ math.random( #items ) ] ) 
    setElementOnGround(object) 
    createBlip( 1, x, y, 100 ) 
    setTimer( spawnObjects, 3000, 1 ) 
end 
spawnObjects( ) 
  
function setElementOnGround(element) 
    local eX,eY,eZ = getElementPosition(element) 
    Timers[element] = setTimer( 
    function(x,y,z) 
    local succeed, _, _, groundZ = processLineOfSight(x, y, 3000, x, y, -3000) 
        if succeed then 
            local wZ = getWaterLevel(x, y, 100) 
            z = (wZ and math.max(groundZ, wZ) or groundZ) + getElementDistanceFromCentreOfMassToBaseOfModel(element) 
            setElementPosition(x,y,z) 
            killTimer(Timers[element]) 
            Timers[element] = nil 
        end 
    end,100,0,eX,eY,eZ) 
end 

setElementOnGround function codes&idea rights back to who created the freeroam, i have just make it as a function

Edit:

I haven't test, so im not sure if it's gonna works

Edited by Guest
Posted

ALw7sh @ Thanks for helping me.

There were bugs in setElementPosition argument 1 element, and element wasn't defined in timer. I fixed it and it worked like charm!

FIXED VERSION IF SOMEONE NEEDS:

  
local Timers = {} 
items = { 
    "Water Bottle", 
    "Tea", 
    "Fence", 
    'Morphine', 
    "Pizza", 
    "Chicken Burger", 
    "Pepsi", 
    "Fanta", 
    "Engine", 
    "Wheel" 
} 
function getXY( ) 
    return math.random(-1500, 1500), math.random(-1500, 1500) 
end 
  
function setElementOnGround(element) 
    local eX,eY,eZ = getElementPosition(element) 
    Timers[element] = setTimer( 
    function(x,y,z, element) 
    local succeed, _, _, groundZ = processLineOfSight(x, y, 3000, x, y, -3000) 
        if succeed then 
            local wZ = getWaterLevel(x, y, 100) 
            z = (wZ and math.max(groundZ, wZ) or groundZ) + getElementDistanceFromCentreOfMassToBaseOfModel(element) 
            setElementPosition(element, x,y,z) 
            killTimer(Timers[element]) 
            Timers[element] = nil 
        end 
    end,100,0,eX,eY,eZ, element) 
end 
  
function spawnObjects( ) 
    local x, y = getXY( ) 
    local object = createObject( 1271, x, y, 0 ) 
    setTimer( function() 
    setElementData( object, "item", items[ math.random( #items ) ] ) 
    setElementOnGround(object) 
    createBlip( x, y, 1, 0 ) 
    end, 200, 1 ) 
    setTimer( spawnObjects, 3000, 1 ) 
end 
spawnObjects( ) 
  
  

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