Jump to content

why controller gets positioned at 0,0,0


Rick
 Share

Recommended Posts

So with the below code I'm trying to figure out why the controller gets positioned at the origin when I run in game mode? I set it's position to the editor model position. So why is it that no matter where I drag the editor model, and I see the controller follow it, that when once I go to game mode the controller goes right to 0,0,0. I'm pretty sure this used to work where it would start the controller at the position of the editor model.

 

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


local class=CreateClass(...)


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

group = grid:AddGroup("Character")
group:AddProperty("model", PROPERTY_FILE, "GMF Files (*.gmf):gmf", "", "Model Files")
group:AddProperty("controller_height", PROPERTY_FLOAT, "Controller Height")
group:AddProperty("controller_radius", PROPERTY_FLOAT, "Controller Radius")
group:AddProperty("controller_step_height", PROPERTY_FLOAT, "Controller Step Height")
group:AddProperty("controller_max_slope", PROPERTY_FLOAT,  "Controller Max Slope")
group:AddProperty("model_scale", PROPERTY_VEC3, "Model Scale")
group:Expand(1)
end

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

object.model = model
object.height = tonumber(object:GetKey("controller_height", "1.8"))
local radius = tonumber(object:GetKey("controller_radius", "0.4"))
local step = tonumber(object:GetKey("controller_step_height", "0.5"))
local slope = tonumber(object:GetKey("controller_max_slope", "45.0"))

object.oneTime = false
object.controller = CreateController(object.height, radius, step, slope)

-- set the position of the controller to the editor object
local pos = object.model:GetPosition()
object.controller:SetPosition(Vec3(pos.x, pos.y + (object.height/2), pos.z))
object.characterModel = nil

object.move = 0
object.strafe = 0
object.rotate = 0
object.jump = 0
object.moveSpeed = 2
object.strafeSpeed = 2

function object:SetKey(key,value)
	if key=="model" then
		local pos = self.model:GetPosition()
		object.characterModel = LoadMesh("abstract::"..value)
		object.characterModel:SetPosition(Vec3(pos.x, pos.y, pos.z))
	elseif key == "controller_height" then
		object.controller:Free()
		object.controller = nil
		object.height = value
		object.controller = CreateController(value, radius, step, slope)
	elseif key == "controller_radius" then
		object.controller:Free()
		object.controller = nil
		object.controller = CreateController(object.height, value, step, slope)
	elseif key == "model_scale" then
		if object.characterModel ~= nil then
			object.characterModel:SetScale(StringToVec3(value))
		end
	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()
	--object.controller:SetMass(1.0)
	--EntityType(object.controller, 1)
end

function object:Update()
	if GetGlobalString("mode") == "GAME_MODE" then
		if self.characterModel ~= nil then
			if object.oneTime == false then
				object.oneTime = true

				object.controller:SetMass(1.0)
				EntityType(object.controller, 1)
				object.controller:SetPosition(object.model:GetPosition(), 1)
			end

			if KeyHit(KEY_T) == 1 then
				Notify(object.model:GetPosition().x..","..object.model:GetPosition().y..","..object.model:GetPosition().z)
			end

			self.controller:Update(self.rotation, self.move, self.strafe, self.jump, 5000, 10, 0)
			self.jump = 0		-- reset each frame

			-- the character model must follow the controller
			-- for the 3rd person camera to work we must also move the object model, which is stupid
			self.model:SetPosition(Vec3(object.controller.position.x, object.controller.position.y, object.controller.position.z))
			self.characterModel:SetPosition(Vec3(object.controller.position.x, object.controller.position.y, object.controller.position.z))
		end
	else
		-- constantly update the controller to the object in the editor
		object.controller:SetPosition(object.model:GetPosition())

		-- make the model follow
		if self.characterModel ~= nil then
			self.characterModel:SetPosition(object.model:GetPosition())
		end
	end
end


function object:ReceiveMessage(message,extra)
	if message == "move" then
		object.move = tonumber(extra) * self.moveSpeed
	elseif message == "jump" and object.controller:IsAirborne() == 0 then
		object.jump = tonumber(extra)
	elseif message == "strafe" then
		object.strafe = tonumber(extra) * self.strafeSpeed
	else
		self.super:ReceiveMessage(message,extra)
	end

end


function object:Free(model)
	object.controller:Free()

	if object.characterModel ~= nil then
		object.characterModel:Free()
	end

	self.super:Free()
end

end

Link to comment
Share on other sites

OK I fixed it but not following why it mattered. I took the code in the oneTime check in Update() and put it inside the Initialize() method. This is a method I created and call before the main loop in the main lua game file. I guess I don't know why that would make a difference.

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