Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How to use : in functions?

Asked by 8 years ago

Hi! I was making a function for fading out parts.

function fadeOut(part)
    for q=0,1,.1 do
        part.Transparency = q
        wait()
    end
end

And I was wondering how I could use it like this.

game.Workspace.Baseplate:fadeOut()
--Instead of
fadeOut(game.Workspace.Baseplate)

Also, I just found out you can moderate your own question...

So, thank you ScriptGuider, I tried this.

part = {"hi"}
thing={"OH YES IT WORKED"}

function part:fadeOut()
    print(self[1])
end

part:fadeOut()

thing:fadeOut()

I get this error

Script:10: attempt to call method 'fadeOut' (a nil value)

2 answers

Log in to vote
2
Answered by 8 years ago

Wrappers

Because ScriptGuider is just one of those people who likes to miss the point by half a mile, let me explain. When you want to add custom methods to something like a Roblox Instance, you have to get into the hacky world of wrappers. The reason for this is simple - You can't change how the Roblox classes behave, but you can change how everything else treats them.


An example wrapper is simple enough - It's a small table which pretends that it's a Part. Note that it is not a part, and that this kind of implementation is incredibly naïve and has several pitfalls, but should be sufficient for you to learn the fundamental concepts from.

local FakePartClass = {};
local function newFakePart(Part)
    return setmetatable({_self = Part},FakePartClass);
end;
function FakePartClass:Explode()
    Instance.new("Explosion", self).Position = self.Position -- self refers to the Part this was called on
    -- Notice how I didn't use self._self? We'll get to that.
end;
FakePartClass.__index = function(t,k)
    local v = FakePartClass[k] or t._self[k]; -- Get the new value
    if type(v) == 'function' then -- If it's a function, we need to cheat
        return function(_,...) -- Proxy the function, ignore self
            return v(t._self, ...) -- Make the function think it's safe
        end;
    end;
    return v;
end;

Did you notice the problems? There are a few - Other things won't treat this like a real Part, so you can't use it as a function argument. All member functions are expected to be called as methods, which is fine in this case. All children of the Part will not have the same methods. There are also several other issues but I won't detail them here.


Want a full wrapper to toy with? Valkyrie has a pretty heavy one which doesn't have all the same flaws as other primitive solutions.

0
I didn't think it would be so hard, thanks anyway! ScriptsAhoy 202 — 8y
Ad
Log in to vote
1
Answered by 8 years ago

Methods

What you're referring to, is a method. A method is no more than a function that can hold a reference to itself, and is quite easy to manipulate once you get the hang of it.


I'd go more in detail about it, but I've answered it pretty substantially already here: https://scriptinghelpers.org/questions/28060/gamedestroy-a-thing#31525

Let me know if you have any questions.

0
Nice long answer there. I read through it, and maybe I missed the point, but did it have anything about custom methods? ScriptsAhoy 202 — 8y
0
Yeah, it has several examples of creating custom methods lol ScriptGuider 5640 — 8y

Answer this question