[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. Sillyness - Pastebin.com