Question

I'm currently writing methods for Roblox's PointLight objects. Currently I'm writing methods to increase and decrease the Brightness property of the PointLight's, but I've run flat up against a wall trying to get the formula I need.

The program needs to loop from Brightness (1) to FadedBrightness (.3) over the span of DarkenSpeed (2 seconds). I've tried Googling around for a possible solution but I fear what I'm looking for is too specific.

Here's a code snippet:

local LightSource = {
  -- The value the PointLight's Brightness property will be when night and day,
  -- respectively.
  Brightness = 1,
  FadedBrightness = .3,

  -- How long (in seconds) it takes for the light source's brightness to be
  -- faded in/out.
  BrightenSpeed = 2,
  DarkenSpeed = 2
}

-- There is an IncreaseBrightness method, but that should be easy enough to
-- modify once the below is working.

function LightSource:DecreaseBrightness()
  -- self.Light refers to the PointLight instance.
  -- light.Brightness would be the current `Brightness` property.
  local light = self.Light

  -- Need to combine Brightness, FadedBrightness and DarkenSpeed into a formula
  -- for decrementing.

  local decrement = self.FadedBrightness / (self.Brightness * self.DarkenSpeed) -- 0.15, that won't work at all.

  while light.Brightness >= self.FadedBrightness do
    light.Brightness = light.Brightness - decrement
    wait()
  end
end

If there are any better ways to accomplish this – or even a different method – I'm all ears. I tend to get tunnel vision with my code and I give no thought to anything other than the present issue.

Was it helpful?

Solution 2

From Odoth's sample code I switched out wait() with tick(), changed variable names to match my coding style, and broke up the formula onto two lines for readability.

It's working well, the only issue is it doesn't stop perfectly at FadedBrightness (Goes past to about .298), so I've included a line at the bottom of the method to counteract the inaccuracy.

Timing works perfectly and I'm very happy with the result.

Completed method:

function LightSource:DecreaseBrightness()
  local light = self.Light
  local startBrightness = light.Brightness
  local startTime = tick()
  local endTime = startTime + self.DarkenSpeed

  while light.Brightness >= self.FadedBrightness do
    local currentTime = tick()
    local timer = (currentTime - startTime) / (endTime - startTime)
    local brightness = startBrightness - (startBrightness - self.FadedBrightness) * timer

    light.Brightness = brightness
    wait()
  end

  -- Set the brightness in case the loop spills over.
  light.Brightness = self.FadedBrightness
end

OTHER TIPS

According to Roblox doc, wait() yields for maybe 1/30th of a second with no argument: http://wiki.roblox.com/index.php?title=Function_dump/Functions_specific_to_ROBLOX#Wait

You could try using os.clock() to calculate the difference each loop and scale proportionately. Untested code, but should give an idea:

function LightSource:DecreaseBrightness()
  -- self.Light refers to the PointLight instance.
  -- light.Brightness would be the current `Brightness` property.
  local light = self.Light

  -- Need to combine Brightness, FadedBrightness and DarkenSpeed into a formula
  -- for decrementing.
  local strtBright=light.Brightness
  local _,strtTime=wait()
  local finTime=strt+self.DarkenSpeed
  while light.Brightness >= self.FadedBrightness do
    local _,currTime=wait()
    light.Brightness = math.max( self.FadedBrightness, strtBright - (strtBright-self.FadedBrightness) * ((curr-strtTime)/(finTime-strtTime)) )
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top