Jump to content

SetParent() to remove parent


Rick
 Share

Recommended Posts

So after the main game loop in lua I try to unparent the camera with

fw.main.camera:SetParent(0, 1)

 

But I get an error: Value at index (2) is not an object. I thought passing 0 as the first parameter was how you removed a parent? I also tried 0 as the second parameter but the same error. Any ideas what I'm missing?

Link to comment
Share on other sites

This is the Thingoid. Basically what it does is allow rotating around target 0. I place an object in the middle of my map and place this Thingoid in the map also. Make the link on index 0, position this Thingoid where I want the camera to be and run it. It then allows me to rotate the camera around the object. When I escape game mode, I then can't move the camera at all in editor mode.

 

 

require("scripts/class")
require("scripts/hooks")

--- create the class
local class=CreateClass(...)

function class:InitDialog(grid)
self.super:InitDialog(grid)
end


function class:CreateObject(model)
local object=self.super:CreateObject(model)

object.model = model
object.oneTime = false
object.leftMouseDown = false
object.mouseX = 0
object.mouseY = 0
object.camRotationX = 0
object.camRotationY = 0
Notify("Unparent camera")
fw.main.camera:SetParent(nil, 1)		-- unparent the camera. when we leave game mode this gets called and should free the camera from being parented

function object:Free(model)
	self.super:Free()
end

function object:SetKey(key,value)
	if key == "" then
	else
		return self.super:SetKey(key,value)
	end
	return 1
end

function object:GetKey(key,value)
	if key=="" then
	else
		return self.super:GetKey(key,value)
	end
	return value
end

function object:Initialize()
end

function object:ReceiveMessage(message,extra)
	if message == "" then
	else
		self.super:ReceiveMessage(message,extra)
	end
end

function object:Update()
	if GetGlobalString("mode") == "GAME_MODE" then
		if object.oneTime == false then
			-- position the camera to the editor object
			fw.main.camera:SetPosition(object.model:GetPosition(1), 1)

			-- make the target the parent to the camera
			fw.main.camera:SetParent(object.model:GetTarget(0), 1)

			object.oneTime = true
		end

		if MouseHit(1) == 1 then
			object.mouseX = MouseX()
			object.mouseY = MouseY()
			MoveMouse(200, 200)		-- 200 doesn't mean anything. It's just picked at random. It doesn't matter what it is since we are always finding the difference from where the mouse started
			HideMouse()
		end

		if MouseDown(1) == 1 then
			local mx
			local my
			mx = MouseX() - 200
			my = MouseY() - 200
			MoveMouse(200, 200)

			object.camRotationY = object.camRotationY + mx / 4.0	-- this is left and right
			--object.camRotationX = object.camRotationX - my / 4.0	-- this is up and down

			object.leftMouseDown = true
			object.model:GetTarget(0):SetRotation(Vec3(0, -object.camRotationY, 0), 1)
		elseif MouseDown(1) ~= 1 and object.leftMouseDown == true then
			MoveMouse(object.mouseX, object.mouseY)
			ShowMouse()
			object.leftMouseDown = false
		end

		--fw.main.camera:SetPosition(object.model:GetPosition(1), 1)
		object.model:SetPosition(fw.main.camera:GetPosition())

		fw.main.camera:Point(object.model:GetTarget(0), 3, 1, 0)
	end
end
end

Link to comment
Share on other sites

This results in the notify telling me the camera parent is nil

local p = fw.main.camera.parent
if p == nil then
Notify("camera parent is nil")
end

 

It's very strange. After I exit game mode when I right click the mouse acts like normal. It is hidden and when I unpress the right mouse it moves to the center of the screen just like normal camera in edit mode. I can't move with the keys and the camera doesn't rotate though.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...