Spawn an object in a random rotation.

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
User avatar
vanblam
Posts: 243
Joined: Sun Nov 09, 2014 12:15 am

Spawn an object in a random rotation.

Post 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.
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Spawn an object in a random rotation.

Post by minmay »

spawn(name,level,x,y,math.random(0,3),elevation,id)
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
vanblam
Posts: 243
Joined: Sun Nov 09, 2014 12:15 am

Re: Spawn an object in a random rotation.

Post 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 ;).
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Spawn an object in a random rotation.

Post 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.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
vanblam
Posts: 243
Joined: Sun Nov 09, 2014 12:15 am

Re: Spawn an object in a random rotation.

Post 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
Post Reply