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

How can I attach a custom OOP object/function to an in-game object?

Asked by 1 year ago

Hello.

In my game I'm trying to implement a system for grabbing objects. I've created a custom class to define these items like so;

(Module script)

local exampleItem = {}
exampleItem.__index = exampleItem

function exampleItem.new()

    local example = setmetatable({},exampleItem)

    local part = Instance.new("Part")
    part.Name = "PartA"
    part.Parent = workspace

    example.Part = part

    return example

end

function exampleItem:Grab()

    print("Item was grabbed")

end

Then I want to fire the "Grab" function from another script, but I haven't found a way to do so.

What I mean is something like this;

(In another script)


workspace.PartA:Grab()

In short I want to be able to associate or "attach" the table to the part so that I can reference it from knowing only the part.

I've looked around on the forums and the only solution I could find was to store all created items in a table, and then give the part an ID which would refer to the item in the table. However, this solution seems a bit convoluted and looks quite prone to memory leaks. So I'm wondering, is there a simpler solution which would make firing the function more direct?

Thanks.

1 answer

Log in to vote
0
Answered by 1 year ago

I dont know any way to reference the part from workspace, but what you can do is inside of your constructor (exampleItem.new()) you can make one of the arguments the part (exampleItem.new(Part)). after that make the thing passed through there the instance to the part when you construct it. I dont know where your module is, but ill assume it is in serverscriptservice. server script:

local ExampleModule = require(game.ServerScriptService.ExampleModule)
local Part = ExampleModule.new(InstanceOfPart)

then once you have that you can fire the grab function from the server script:

Part:Grab()

it sees that Part:grab() doesnt exist, so since example is the metatable of exampleItem it searches the exampleItem table for grab and finds it. so Part:grab() will automatically pass through that part as a variable, to access it just type in "self" without the quotation marks of course. so say you want to print out the name of the part you grabbed, change out the ~~~~~~~~~~~~~~~~~ print("Item was grabbed")

to be 

print(self.Name) -- you can concatenate "was grabbed" if you want ~~~~~~~~~~~~~~~~~

also, if you want to change the . for a : on the constructor that would be fine too, use self.Name and self.Parent, then you dont really need to store data like that in a table (if i recall correctly).

0
oops... i messed up some of the code lines, im sure youll be able to tell whats code either way (on the part that the code it to be instead of the print thing) Verse_NOVA 52 — 1y
0
Thanks for the help! Upon further investigation it seems that the method I mentioned (giving instances IDs) is the simplest, so I'm going to try implementing that. Anyway, thank you. Vinceberget 1420 — 1y
Ad

Answer this question