Jump to content

Darkness Awaits Template - Player.lua & 3rdPersonFollow.lua issues


WazMeister
 Share

Recommended Posts

Hi All,

I'm trying to understand Leadwerks and scripting by playing around with Darkness Awaits template.

I tried using some of the code from the above scripts on my own character model for the camera follow, but kept getting errors of field entity 'nil'.

 

So to test it, I used the original scripts on my model and change a few things to suit bare minimum, and still get an error - attempt to index field 'script' (a nil value) on line 58 which is-

self.camera.script.target = self.entity

 

 

What am I doing wrong?

Dream since child of making games! From Game Programming Starter Kit 3.0, Blitz Basic, Map Creation since Duke 3D, Game Maker, Blitz3D (of recent..2023) and many other engines and years..... never really sticking to it with inner struggles that I've had to fight along with pushing to learn and acheive.

40 years old.. came across Leadwerks on Steam... Learning slowly but surely and loving it!

Learn with me or just watch me fail! at my random Youtube Channel, as I stream adhoc while learning and using LeadWerks and other game creating tools.

https://www.youtube.com/@wazmeister3151/featured

Link to comment
Share on other sites

Yea, it's basically the Darkness Awaits scripts, code below-

Because it did not work, I just went back to basic of self.camera:move and updateworld.. but code below-

Player script-

local runmultiplier = 0.8

--Set Script properties / define variables
Script.health = 100--"Health
Script.maxHealth = 100
Script.WalkSpeed = .02
Script.RunSpeed = 0.1
Script.maxAcceleration = 1.0--float "Max Acceleration"

--Player states (Setup in a lua table with fields)
Script.state = {} --Table called state
--Add fields to table
Script.state.idle = 0
Script.state.walk = 1
Script.state.run = 2
Script.state.carry = 3
Script.state.dead = 4


function Script:Start()
	--Set the type to player,  keyvalue is string value associated with a key name
	self.entity:SetKeyValue("type", "player")
	
	if self.entity:GetMass() == 0 then
		self.entity:SetMass(10)
	end

	self.angle = self.entity:GetRotation().y
	self.entity:SetPhysicsMode(Entity.CharacterPhysics) --Setup character controller physics mode
	self.entity:SetCollisionType(Collision.Character, true)

	--Get the players current position
	playerPosition = self.entity:GetPosition()


	--Create a camera to view game/screen.
	self.camera = Camera:Create()
	-- Leave for example changing Field of View
	--self.camera:SetFov(70)
	--Set the antialias of camera (MSAA)
	self.camera:SetMultisampleMode((System:GetProperty("multisample", "1")))
	--Set camreas position to the player + above him
	self.camera:SetPosition(playerPosition.x, playerPosition.y - 3, playerPosition.z)
	--Rotate the camrea for looking down on player (Top Down View)
	self.camera:SetRotation(Vec3(90, 0, 0))
	--Set script to refer to the camrea follow script etc. set to false so start function won't start within.
	self.camera:SetScript("Scripts/Objects/Camera/3rdPersonFollow.lua", false)
	self.camera.script:target = self.entity
	--set target varible in the camrea script to this entity (player)
	self.camera.script:Start()

	--setup lua table to hold healthbar images
	self.image = {}
	self.image.healthbar = Texture:Load("Materials/HUD/healthbar.tex")
	self.image.healthmeter = Texture:Load("Materials/HUD/healthmeter.tex")
	
	self.image={}
	self.image.healthbar = Texture:Load("Materials/HUD/healthbar.tex")
	self.image.healthmeter = Texture:Load("Materials/HUD/healthmeter.tex")

	--Define default state variables
	self.currentState = -1

	
end

--Adjust the camera orientation relative to the player
function Script:UpdateCamera()
	--Rotate the camrea for looking down on player (Top Down View)
	self.camera:SetRotation(Vec3(90, 0, 0))
	--Set camreas position to the player + above him
	self.camera:SetPosition(self.entity:GetPosition())
	self.camera:Move(0, 0, -4)
end

function Script:UpdatePhysics()
	--blank vec3 variable for later adding key presses movement.x, movement.y and movement.z
	local movement = 0
	--Get the Game Window
	local window = Window:GetCurrent()
	local changed

	--Check state for player dead
	if self.currentState == self.state.dead or self.health <= 0 then return end
	
	--Check if carrying, if not then player will be idle state
	if self.currentState ~= self.state.carry then
		self.currentState = self.state.idle
	end

	--Detect if grabbing a bin
	if self.currentState ~= self.state.carry then
		local docarry = false
		if window:KeyDown(Key.Space) then 
			self.currentState = self.state.carry
			--self.attackstarttime = Time:GetCurrent()
		end
	end

	--Movement
	--Code for detecting key presses for movement and attacks
	if window:KeyDown(Key.W) then movement = movement - 1 changed = true end
	if window:KeyDown(Key.S) then movement = movement + 1 changed = true end
	if window:KeyDown(Key.D) then self.entity:Turn(0, 3, 0) change = true end
	if window:KeyDown(Key.A) then self.entity:Turn(0, -3, 0) change = true end
	if changed then
		if self.currentState ~= self.state.carry then
			if window:KeyDown(Key.Shift) then
				movement = movement * self.RunSpeed
				self.currentState = self.state.run
			else
			movement = movement * self.WalkSpeed
			self.currentState = self.state.walk
			end
		end
	end

	self.entity:Move(0, 0, movement)

--Update animation
	if prevState ~= self.currentState then
		if self.currentState == self.state.idle then
			self.entity:PlayAnimation("Idle", .1, 200)
		elseif self.currentState == self.state.walk then
			self.entity:PlayAnimation("Walk", .1, 200)			
		elseif self.currentState == self.state.run then
			self.entity:PlayAnimation("Run", .1, 200)
		elseif self.currentState == self.state.carry then
		end	
	end
end

function Script:UpdateWorld()
	--Update the camrea position each frame (i.e follow player)
	self:UpdateCamera()
end
camrea script

----------------------------------------------------------------------
--This script will make a camera follow any entity

--target: The entity to follow
--distance: The distance away from the entity to place the camera
--radius: collision radius of the camera (so it doesn't go through walls)
----------------------------------------------------------------------

Script.target = nil--Entity "Target"
--Script.distance = 6--float
--Script.debugmode = false--bool
--Script.pitch = 15--float
--Script.height = 6--float
Script.radius = 0.5--float

function Script:Start()
	self.smoothness=60
	if self.target==nil then
		return
		Debug:Error("Script must have a target.")
	end
	
	self.firstframe=true

	self.targetoffset=Vec3(0)
	self.targetpositionoffset=Vec3(0)
	local camposition = self.entity:GetPosition(true)
	self.targetposition = self.target:GetPosition(true)
	self.relativeposition = camposition - self.targetposition
	
	--Construct a vertical plane facing the camera
	local v = Vec3(self.relativeposition.x,0,self.relativeposition.z)
	local vn = v:Normalize()
	local plane = Plane(self.targetposition,vn)
	local result = Vec3(0)
	local dir = Transform:Point(0,0,1000,self.entity,nil)
	if plane:IntersectsLine(camposition,camposition + dir,result) then
		self.targetpositionoffset = result - self.targetposition
	end
	
	self.height = self.entity.mat[3][1]

	--self.entity:SetDebugPhysicsMode(self.debugmode)
	--self.entity:SetDebugNavigationMode(true)
	--self.entity:SetDebugPhysicsMode(true)
	--self.entity:SetRotation(self.pitch,0,0)
	self.entity:SetPickMode(0)
end

function Script:UpdateWorld()
	
	--Exit the function if the target entity doesn't exist
	if self.target==nil then return 0 end
	
	--Get the target entity's position, in global coordinates
	local p0 = self.target:GetPosition(true) + self.targetpositionoffset
	local p1 = p0 + self.relativeposition--Vec3(p0.x,p0.y+self.height,p0.z-self.distance)
	local velocity = self.target:GetVelocity()
	velocity.y=0
	 
	self.targetoffset.x = Math:Curve(velocity.x*0.25,self.targetoffset.x,self.smoothness / Time:GetSpeed())
	self.targetoffset.z = Math:Curve(velocity.z*0.25,self.targetoffset.z,self.smoothness / Time:GetSpeed())
	
	--if(velocity.x ~= 0) then
	--	io.write (velocity.x,", ",velocity.y,", ",velocity.z, "\n")
	--end
	
	--Add our original offset vector to the target entity position
	--p0 = p0 + velocity * 2
	--p1 = offset + velocity * 2
	
	p0 = p0 + self.targetoffset
	p1 = p1 + self.targetoffset
	
	--Calculate a second point by backing up the camera away from the first point
	--local p1 = p0 + Transform::Normal(0,0,-1,self.entity,nil) * distance
	
	--Perform a raycast to see if the ray hits anything
	local pickinfo = PickInfo();
	
	local pickmode = self.target:GetPickMode()
	self.target:SetPickMode(0)
	--if self.entity.world:Pick(p0,p1,pickinfo,self.radius,true,Collision.Debris) then
		--If anything was hit, modify the camera position
	--	p1 = pickinfo.position
	--end
	self.target:SetPickMode(pickmode)
	
	--Smooth the camera motion
	--[[
	if self.smoothness>0 then
		local currentpos = self.entity:GetPosition(true)
		p1.x = Math:Curve(p1.x,currentpos.x,self.smoothness)--/Time:GetSpeed())
		p1.y = Math:Curve(p1.y,currentpos.y,self.smoothness)--/Time:GetSpeed())
		p1.z = Math:Curve(p1.z,currentpos.z,self.smoothness)--/Time:GetSpeed())
	end
	]]--
	
	--[[local currentPosition = self.entity:GetPosition(true)

		currentPosition.x = Math:Curve(p1.x,currentPosition.x,self.smoothness)--/Time:GetSpeed())
		--p1.y = Math:Curve(p1.y,p0.y,self.smoothness)--/Time:GetSpeed())
		currentPosition.z = Math:Curve(p1.z,currentPosition.z,self.smoothness)--/Time:GetSpeed())]]--
	--Finally, set the camera position
	
	--Smooth the height when going up stairs, etc.
	if self.firstframe then
		self.firstframe=false
		self.height = p1.y
	else
		self.height = Math:Curve(p1.y,self.height,5)	
	end
	p1.y = self.height

	self.entity:SetPosition(p1,true)
	
end

 

Dream since child of making games! From Game Programming Starter Kit 3.0, Blitz Basic, Map Creation since Duke 3D, Game Maker, Blitz3D (of recent..2023) and many other engines and years..... never really sticking to it with inner struggles that I've had to fight along with pushing to learn and acheive.

40 years old.. came across Leadwerks on Steam... Learning slowly but surely and loving it!

Learn with me or just watch me fail! at my random Youtube Channel, as I stream adhoc while learning and using LeadWerks and other game creating tools.

https://www.youtube.com/@wazmeister3151/featured

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