Page 1 of 1

Spawn an object in a random rotation.

Posted: Fri Feb 05, 2016 2:46 am
by vanblam
How would you go about spawning an object in a random rotation via script? For example I want to spawn random details on my floor facing in a random direction ( I have random details spawning on my tile set already, just need to figure out how to randomly face them). I know I could just turn on random tile facing but the base floor doesn't look good that way. I just want to rotate the details that spawn on top.

Re: Spawn an object in a random rotation.

Posted: Fri Feb 05, 2016 2:52 am
by minmay
spawn(name,level,x,y,math.random(0,3),elevation,id)

Re: Spawn an object in a random rotation.

Posted: Fri Feb 05, 2016 3:29 am
by vanblam
minmay wrote:spawn(name,level,x,y,math.random(0,3),elevation,id)
I have this set up atm.. I thought I did it correctly but it doesn't work in this state. Not getting any errors, its just not working. I missed something just can't find it :P

Code: Select all

local autoDecoFloorDetail = function(self)
	local g = self.go
	local details = {
		rock_tunnel_ground_detail01=true,
		rock_tunnel_ground_detail02=true,
		rock_tunnel_ground_detail03=true,
		rock_tunnel_ground_detail04=true,
		rock_tunnel_ground_detail05=true,
	}
	if g.map:getElevation(g.x,g.y) == g.elevation then
		local choice = math.random()
		if choice <= 0.5 then
			g:spawn("rock_tunnel_ground_detail05",g.level,g.x,g.y,math.random(0,3),g.elevation)
		elseif choice <= 0.625 then
			g:spawn("rock_tunnel_ground_detail02",g.level,g.x,g.y,math.random(0,3),g.elevation)
		elseif choice <= 0.75 then
			g:spawn("rock_tunnel_ground_detail03",g.level,g.x,g.y,math.random(0,3),g.elevation)
		elseif choice <= 0.875 then
			g:spawn("rock_tunnel_ground_detail04",g.level,g.x,g.y,math.random(0,3),g.elevation)
		else
			g:spawn("rock_tunnel_ground_detail01",g.level,g.x,g.y,math.random(0,3),g.elevation)
		end
	end
end
EDIT: Yes I do have the "autoDecoFloorDetail" on the floor object ;).

Re: Spawn an object in a random rotation.

Posted: Fri Feb 05, 2016 3:47 am
by minmay
http://www.grimrock.net/modding/scripting-reference/
GameObject:spawn() only takes a single parameter, the name of the object to be spawned. It spawns the object with the same level, x, y, facing, and elevation as the GameObject you called it from - that's the point of GameObject:spawn().
If you want to spawn an object with a different facing, use the global spawn() function instead, like I said.

Also, you obviously copied and pasted that function from somewhere else without really looking at what it does...the following will give you the functionality you wanted:

Code: Select all

local autoDecoFloorDetail = function(self)
   local g = self.go
   local choice = math.random()
   if choice <= 0.5 then
      spawn("rock_tunnel_ground_detail05",g.level,g.x,g.y,math.random(0,3),g.elevation)
   elseif choice <= 0.625 then
      spawn("rock_tunnel_ground_detail02",g.level,g.x,g.y,math.random(0,3),g.elevation)
   elseif choice <= 0.75 then
      spawn("rock_tunnel_ground_detail03",g.level,g.x,g.y,math.random(0,3),g.elevation)
   elseif choice <= 0.875 then
      spawn("rock_tunnel_ground_detail04",g.level,g.x,g.y,math.random(0,3),g.elevation)
   else
      spawn("rock_tunnel_ground_detail01",g.level,g.x,g.y,math.random(0,3),g.elevation)
   end
end
There is no point in declaring a table if you never use it, nor in checking the elevation, since you're attaching this to a floor object already.

Re: Spawn an object in a random rotation.

Posted: Fri Feb 05, 2016 4:06 am
by vanblam
Thank you,
Yes I did copy that code its yours lol..I know what it does I just didn't catch the game object spawn vs the global spawn.
I have that page open at all times and I still didn't see the "Global" .. ugg