Instance:interpolate("Transparency",from,to,time)

Many times we want to fade an objects transparency, or the volume of a sound, or the size of a fire. We then use a for or a while loop combined with a wait() inside.

I’d like to suggest the implementation of an interpolate function in the basic Instances. It can interpolate any number value that the instance has, given the from, to and time values.

Example:
workspace.Part:interpolate(“Transparency”,0,1,2) --Fade from 0 to 1 in transparency over a timespan of 2 seconds.
workspace.Sound:interpolate(“Volume”,1,0,4) --Fade down volume to 0, over a timespan of 4 seconds.

It’s absolutely not something that’s urgent, but it just crossed my mind so I thought I’d share it.
How does this sound?

1 Like

There’s lots of free code out there that does this already. If anything, I’d like to see a built in library for it at some point, but maybe not anytime soon.

I often find myself doing this. do local time, duration = tick(), 2 local halfPi = math.pi/2 local targetValue, startValue = 1, object.Transparency local difValue = targetValue - startValue repeat local i = math.sin(((tick() - time) / duration) * (halfPi)) object.Transparency = startValue + ((difValue) * i) until tick() - time >= duration object.Transparency = targetValue end

That wouldn’t work so well locally though. I think PlaceRebuilder is suggesting a method to simulate this locally on clients, which would actually be good for those who are not that exprienced with math.

This sort of thing is meant to be done locally, not on the server. I’ve created a library for this anyway now, almost complete. I’ll see if the admins would be interested in adding it to the content folder so we can set a module scripts linked source as it.

You can use this to animate objects and their properties. It also lets you select easing types.

That’s awesome!
But what I was suggesting was it to be added as a default function of instances. Since the usage is very wide and newbies learn how to use for loops before they learn to require modules…

I agree with PlaceRebuilder that a built in function would be great because it works server side and client side and is easier for narbs.

[quote] Many times we want to fade an objects transparency, or the volume of a sound, or the size of a fire. We then use a for or a while loop combined with a wait() inside.

I’d like to suggest the implementation of an interpolate function in the basic Instances. It can interpolate any number value that the instance has, given the from, to and time values.

Example:
workspace.Part:interpolate(“Transparency”,0,1,2) --Fade from 0 to 1 in transparency over a timespan of 2 seconds.
workspace.Sound:interpolate(“Volume”,1,0,4) --Fade down volume to 0, over a timespan of 4 seconds.

It’s absolutely not something that’s urgent, but it just crossed my mind so I thought I’d share it.
How does this sound? [/quote]

ytime soon.

I often find myself doing this.
do
local time, duration = tick(), 2
local halfPi = math.pi/2
local targetValue, startValue = 1, object.Transparency
local difValue = targetValue - startValue
repeat
local i = math.sin(((tick() - time) / duration) * (halfPi))
object.Transparency = startValue + ((difValue) * i)
until tick() - time >= duration
object.Transparency = targetValue
end

[quote] Many times we want to fade an objects transparency, or the volume of a sound, or the size of a fire. We then use a for or a while loop combined with a wait() inside.

I’d like to suggest the implementation of an interpolate function in the basic Instances. It can interpolate any number value that the instance has, given the from, to and time values.

Example:
workspace.Part:interpolate(“Transparency”,0,1,2) --Fade from 0 to 1 in transparency over a timespan of 2 seconds.
workspace.Sound:interpolate(“Volume”,1,0,4) --Fade down volume to 0, over a timespan of 4 seconds.

It’s absolutely not something that’s urgent, but it just crossed my mind so I thought I’d share it.
How does this sound? [/quote]

Wrote something to do this, it’s pretty silly though : 3

-- Instance:Interpolate(numberproperty,numberstart,numberend,time)
-- Example at bottom

local oldinstance = Instance.new;
local Instance = {};
local metas = {
 __index = function(s,i)
  return s._u[i];
 end,
 __newindex = function(s,i,v)
  s._u[i]=v;
 end
}
function interpolate(s,prop,startValue,targetValue,d) 
   if type(targetValue) == "number" then 
    local object = s._u;
    local time, duration = tick(), d;
    local halfPi = math.pi/2;
    local difValue = targetValue - startValue;
    repeat  
    local i = math.sin(((tick() - time) / duration) * (halfPi));
    object[prop] = startValue + ((difValue) * i)  
    wait();
    until
    tick() - time >= duration  
    object[prop] = targetValue;
   end
end
function find(obj,str)
   local a,b = pcall(function() print(obj[str]) end);
   return a;
end
function destroy(s)
   s = null;
   s._U:Destroy();
end
function get(obj)
 if type(obj) == 'userdata' then
  if find(obj,'Parent') and not find(obj,'_u') then
   local array = {_u=obj,Interpolate=interpolate,Destroy=destroy};
   array.u = obj;
   array.Interpolate = interpolate;
   setmetatable(array,metas);
   return array;
  else
   error(".",0);
  end
 else
     error("..");
 end
end
Instance.new = function(str,par)
 local array = {
 Destroy=destroy,_u=oldinstance(str,par),Interpolate = coroutine.wrap(interpolate)
 };
 setmetatable(array,metas);
 return array;
end



p = Instance.new("Part",Workspace);
p.Name = "Hi";
wait(4);
p:Interpolate("Transparency",0,1,2);

BasePlate = get(Workspace.BasePlate)
BasePlate:Interpolate("Reflectance",0,2,5);

Pastebin if it didn’t paste over well to code blocks. http://pastebin.com/XhihhpRL