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

A Method Parameter?

Asked by
woodengop 1134 Moderation Voter
9 years ago

In the Method page (in the Roblox Wiki), and the Object Oriented page, they had the parameter of self and arg, That part really troubled me. What does the Method's parameter mean, Example.

Body={
    arms=2;
    legs=2;
    torso=1;
    head=1;
    bodyCount=function(arg) -- Methods act like Functions within an Object
        print(arg.arms+arg.head)
        return Body.arms+Body.legs -- Ignore this, I was just playing around with the `return`.
    end
}

Body:bodyCount()

But without the parameter, it prints nil.

1 answer

Log in to vote
1
Answered by 9 years ago

The parameter is actually self, meaning the object you pass with the colons ':' is actually passing itself as the first parameter.

So basically what you can do is have your object set it's body parts. Using a function that needs the object you can accomplish this quiet easily. Look below to see what I mean:

setArmCount = function(self, armCount)
    print("Arm count is now: "..arms)
    self.arms = Count
end

Essentially what this does is allow the object to sets ITS arms rather than the whole main object. Now passing the parameters can be done like so, don't forget what the ':' does.

Body:setArmCount(3)

As you hopefully can tell, there is only one argument that you can see passes. Except two are passed, one being the Body object and the other being the armCount of 3.

So, in essence, the reason the output would say nil, is due to no object being given to show how many arms and heads it has.

I hope this helps a bit, but if not I'll try to explain it again.

Ad

Answer this question