Jump to content

Time function Time:Millisecs() not working as expected


Defranco
 Share

Recommended Posts

Script.thirst = 100 --float "Thirst"
Script.maxThirst = 200 --float "Max Thirst"
Script.thirst_time = 0 --time to keep track of thirst
Script.location_rate_thirst = -0.00000008
Script.healthLocation_ratethirst = .12


--(this part is inside the script: start area)--
--Activate Thirst
self.thirst_time = Time:Millisecs()

--(this controls using items that give thirst back to player)-- Not really relevant to the timer function.
function Script:ReceiveThirst(thirstPoints)
--Increase thirst
self.thirst = self.thirst + thirstPoints;
if self.thirst > self.maxThirst then
self.thirst = self.maxThirst
end
self.component:CallOutputs("ThirstReceived")
end

--(this part is inside the script: Update World area)--

-- Update Thirst
if self.thirst > 0 then
self.thirst=self.thirst-self.location_rate_thirst*(self.thirst_time-Time:Millisecs())
end
if self.health > 0 then
if self.thirst < 5 then
self.health=self.health-self.healthLocation_ratethirst;
end
end
if self.thirst > self.maxThirst then
self.thirst = self.maxThirst
end

 

What I am trying to do: Have thirst decrease at a steady rate that keeps the same forever. Then if it hits lower than 5, it starts damaging the player over time until the player drinks to get thirst back.

 

So what this script seems to be doing is increasing over-time. The first 20 seconds seems normal. But as time increases, so does the rate of decrease.

 

Just an Example: First 1-20 seconds it decreases at 0.5 per second, then 21-40 it decreases at 0.8 per second, then at 41-60 seconds it decreases at 1.2 per second.

 

It seems to be adding on extra time as time goes on and increases the rate. Not sure what I'm missing.

Link to comment
Share on other sites

Its because the value that you keep subtracting from 'self.thirst' is constantly getting larger.

self.thirst=self.thirst-self.location_rate_thirst*(self.thirst_time-Time:Millisecs())

Time:Millisecs() returns the system time in milliseconds.

if you want a constant rate of decrease then do something like:

function Script:UpdateWorld()

if self.thirst > 0 then

self.currenttime = Time:Millisecs()

if self.currenttime>self.thirst_time+2000 then -- 2000 = 2 seconds

self.thirst = self.thirst - 0.5 --- or however much you want to descrease but should be a constant if thats what you want

self.thirst_time = self.currenttime

end

end

...

end

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