Anubhav Posted May 17, 2015 Share Posted May 17, 2015 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. Link to comment
xXMADEXx Posted May 17, 2015 Share Posted May 17, 2015 You can use math.random to calculate the X and Y, then use getGroundPosition. Link to comment
Mr.unpredictable. Posted May 17, 2015 Share Posted May 17, 2015 I remember i created something similar to this but it had weird bugs. Like sometimes it was creating object on objects. Link to comment
Anubhav Posted May 17, 2015 Author Share Posted May 17, 2015 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( ) Link to comment
WhoAmI Posted May 17, 2015 Share Posted May 17, 2015 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( ) Link to comment
Anubhav Posted May 17, 2015 Author Share Posted May 17, 2015 Now it spawns too low ( under the objects ) or too high in the sky. Z is always going wrong.. Link to comment
ALw7sH Posted May 17, 2015 Share Posted May 17, 2015 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 Link to comment
Anubhav Posted May 17, 2015 Author Share Posted May 17, 2015 It doesn't matter for now, the problem is Z. It's not getting ground position properly. Any other function that I can use? Link to comment
Walid Posted May 17, 2015 Share Posted May 17, 2015 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 and this z is always 0 it mean 0+0.3 = 0.3 Link to comment
WhoAmI Posted May 17, 2015 Share Posted May 17, 2015 It's not always 0 z = getGroundPosition( x, y, z ) Link to comment
ALw7sH Posted May 17, 2015 Share Posted May 17, 2015 (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 May 17, 2015 by Guest Link to comment
Anubhav Posted May 17, 2015 Author Share Posted May 17, 2015 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( ) Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now