Jump to content

Respawn not quick enough


Braqoon
 Share

Recommended Posts

Hi, so I got this problem that I'm not sure how to resolve.

 

I'm trying to add lives counter in my little test but seems like respawn is not quick enough and instead of removing one life it goes crazy and removes a lot more.

 

I have took a respawn function from marble game tutorial with some modifications:

 

function Script:Respawn()
self.lives = self.lives - 1
 if self.lives > 0 then
self.death = 0
self.health=100
self.altitude = 0
self.entity:SetPosition(self.startposition)
self.entity:SetRotation(0,0,0)
self.entity:SetVelocity(Vec3(0,0,0))
self.entity:SetOmega(Vec3(0,0,0))
self.gamestarttime = Time:GetCurrent()+2000
 end
end

 

A bit of explanation:

 

So I got a 'player' which is a just a box at the moment which is floating and if it will hit an obstacle then it starts to fall (hence self.altitude). Eventually it will hit a TriggerPain object and that will initiate respawn.

 

Now if I remove from above code

self.lives = self.lives - 1

 

and run the game, all is fine. 'player' hits the TriggerPain, Respawn function resets the 'player' and all is OK.

 

If self.lives is there and Respawn function is called, life is being deducted but game is not quick enough to reset players position and TriggerPain is being active again so Respawn goes again and another life is being deducted (or more). I have got lives counter rendered on the screen so I'm quite sure that is the problem.

 

Question here is that how can I make it so Respawn allows to deduct a life and resets position properly? Not sure how to tackle it.

Link to comment
Share on other sites

Where ever your "Hurt" function is, put in:

 

if self.health <= 0 then return end

 

at the top. That will stop Respawn() from being called multiple times

 

Hi,

 

Running out of health is not a problem but lives is. At the moment if lives is <= 0 then it will not respawn. I have not done game over screen yet but that's only showing that live counter is to blame here as counter is being constantly altered as if one respawn cycle has not finished and another is already started.

 

if self.health <= 0 then
self.altitude = -1

-- Which then will contribure overall to:

function Script:UpdatePhysics()
[...]
self.vel = Vec3(self.pos,self.altitude,self.speed)

-- and by altering altitude (falling), object (player) will hit TriggerPain and invoke restart in :

function Script:TakeDamage(damage)
self.health = self.health - damage
if self.health<=0 then
self:Respawn()
end
end

-- Just to remind TriggerPain.lua
--[[
This script will act as a trigger to change the current map.
Place this at the end of your map to make the player progress
to the next level.
]]--
Script.damage=1000--int "Damage"
Script.enabled=true--bool "Enabled"

function Script:Collision(entity, position, normal, speed)
if self.enabled then
if entity.script then
if type(entity.script.TakeDamage)=="function" then
entity.script:TakeDamage(self.damage)
end
end
end
end

function Script:Enable()--in
if self.enabled==false then
self.enabled=true
self:CallOutputs("Enable")
end
end

function Script:Disable()--in
if self.enabled then
self.enabled=false
self:CallOutputs("Disable")
end
end

 

Now I might be wrong but on TakeDamage function must call Respawn or (yet to complete) Game Over screen, but for ever reason it does not do Respawn properly only when lives counter is in place. If number of lives is not being taken to account, Respawn works as it should.

Link to comment
Share on other sites

I suspect when your player hits the TriggerPain it causes multiple calls to the Script:Collision() function and not just one call on contact. You can verify by putting in a 'System:Print("made contact") in the collision function. So you would have to put a check into the Script:Collision() embedded into your other nested if/then statements like:

function Script:Collision(entity, position, normal, speed)

if self.enabled then

self.enabled== false --disable after first call

if entity.script then

if type(entity.script.TakeDamage)=="function" then

entity.script:TakeDamage(self.damage)

end

end

end

end

 

then set 'script.enabled' to true at the end of the Script:Respawn() function

  • Upvote 1

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

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