generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Practical uses of metatables?

Previous Thread :: Next Thread 
MINEBLOX106 is not online. MINEBLOX106
Joined: 28 Feb 2012
Total Posts: 308
02 Dec 2016 11:46 AM
I guess I'm just not seeing what could be done with them.

Could anyone give me some examples?


Report Abuse
Furadious is not online. Furadious
Joined: 23 Dec 2013
Total Posts: 601
02 Dec 2016 11:52 AM
If I understand this correctly in LUA, they are arrays which are very handy.
For example if you're saving usernames, you can list them into an array.
There's tons of examples.
Report Abuse
Soybeen is not online. Soybeen
Joined: 17 Feb 2010
Total Posts: 21462
02 Dec 2016 12:00 PM
Well, I typically use them for requiring huge tables that I don't want cluttering my main script.


Report Abuse
Soybeen is not online. Soybeen
Joined: 17 Feb 2010
Total Posts: 21462
02 Dec 2016 12:02 PM
Fura, ModuleScripts are not tables/arrays.
They can contain tables/arrays, but they are not.


Report Abuse
Soybeen is not online. Soybeen
Joined: 17 Feb 2010
Total Posts: 21462
02 Dec 2016 12:02 PM
Also it's not LUA it's Lua, and ModuleScripts are not a Lua thing, they are a ROBLOX thing that happen to employ the Lua language :)


Report Abuse
x_o is not online. x_o
Joined: 04 Jun 2015
Total Posts: 4378
02 Dec 2016 12:08 PM
custom classes/objects

you can make a wrapper to add extra functionality

etc

≧◡≦
Report Abuse
clc02 is not online. clc02
Joined: 30 Dec 2007
Total Posts: 7393
02 Dec 2016 12:36 PM
Two players have their own tables, the table has a table of their items, a table of their skills, and a table of their money.
Say you want it to be such that, if player A kills player B, they gain three of their items at random, give to player A all player B's money, and to decrease player B's skills. You can do this by using metamethods and metatables such that: PlayerB - PlayerA handles that operation
Report Abuse
Soybeen is not online. Soybeen
Joined: 17 Feb 2010
Total Posts: 21462
02 Dec 2016 12:38 PM
I'm SO SORRY I thought you said Module Scripts!
My brightness is -0 and I scrolled too quickly!

My apologies! :'(


Report Abuse
direMitten is not online. direMitten
Joined: 18 Jun 2015
Total Posts: 1294
02 Dec 2016 12:48 PM
Example of simple OOP in Lua using metatables:

--Private fields/methods that are the same across instances of this class
local ClassName = "Triangle"
local TriangleDict = {}
local TriangleList = {}

function GetUniqueID()
local UID
for i = 1, #TriangleList do
if not TriangleList[i] then
UID = i
break
end
end

if not UID then
UID = #TriangleList + 1
end

return UID
end

function Initialize(self, ...)
local arguments = { ... }

--Public fields/methods that are specific to each instance of this class
self.P1 = arguments[1]
self.P2 = arguments[2]
self.P3 = arguments[3]
self.UID = GetUniqueID()
end

--Public fields/methods that are the same across instances of this class, unless they're
--overwritten at a later stage. In that case these act more like default values.
local Triangle = {
Name = "Triangle"
}

local TriangleMetatable = {
__index = Triangle --make the Triangle table act as "default" values
__tostring = function(self)
return tostring(self.P1) .. ", " .. tostring(self.P2) .. ", " tostring(self.P3)
end
}

function Triangle.new( ... )
local self = setmetatable({}, TriangmeMetatable)

Initialize(self, ...)

TriangleList[self.UID] = self
TriangleList[self] = self.UID --*might* be more useful than just holding a reference to itself

return self
end

function Triangle:Build(thickness, color)
--Code that makes a triangle out of Wedge parts, with the given thickness and color
end

function Triangle:Destroy()
TriangleList[self.UID] = nil
TriangleList[self] = nil

for _, part in pairs(self.WedgeParts) do
part:Destroy()
end

self = nil
end

return Triangle --Assuming the above is in a module script, which is hella useful
Report Abuse
direMitten is not online. direMitten
Joined: 18 Jun 2015
Total Posts: 1294
02 Dec 2016 12:50 PM
Oh and you could then do something like this in another script:

local Triangle = require(script.Triangle)

local triangle = Triangle.new(Vector3.new(0,0,0), Vector3.new(5, 1, 12), Vector3.new(-1, 2, 3)

triangle:Build()

Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image