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: Ah, I love metatables.

Previous Thread :: Next Thread 
Real_Spooky is not online. Real_Spooky
Joined: 23 Oct 2013
Total Posts: 3149
13 Jan 2017 08:10 PM
They certainly are great. Something more satisfying about using tables rather than using a simple 'class' keyword.
Report Abuse
CeaselessQuokka is not online. CeaselessQuokka
Joined: 03 Jul 2012
Total Posts: 7841
13 Jan 2017 08:11 PM
What.


Report Abuse
Real_Spooky is not online. Real_Spooky
Joined: 23 Oct 2013
Total Posts: 3149
13 Jan 2017 08:13 PM
I know metatables aren't specifically used for object-oriented programming, but I find them to be well-equipped for it. Just clearing that up in case anybody wants to attack me on my opinion. Which I'm wholly prepared for.
Report Abuse
Wrathsong is online. Wrathsong
Joined: 05 Jul 2012
Total Posts: 22393
13 Jan 2017 08:47 PM
what practical uses do they hold


Ever wanted to learn how to script? Check out my YouTube channel: youtube.com/austintheslayer Want to see me code in action? Follow my twitch: twitch.tv/austinrblx
Report Abuse
Bobby_Darin is not online. Bobby_Darin
Joined: 05 Jan 2013
Total Posts: 5587
13 Jan 2017 08:52 PM
Ah, yes. I too love metatables. Ahh.
Report Abuse
Real_Spooky is not online. Real_Spooky
Joined: 23 Oct 2013
Total Posts: 3149
13 Jan 2017 08:52 PM
object orientedness and cleaner-looking data manipulation through assigning helper functions to operators pretty much
Report Abuse
Wrathsong is online. Wrathsong
Joined: 05 Jul 2012
Total Posts: 22393
13 Jan 2017 08:54 PM
example?


Ever wanted to learn how to script? Check out my YouTube channel: youtube.com/austintheslayer Want to see me code in action? Follow my twitch: twitch.tv/austinrblx
Report Abuse
Real_Spooky is not online. Real_Spooky
Joined: 23 Oct 2013
Total Posts: 3149
13 Jan 2017 08:59 PM
pair = {}
pair.__index = pair

function pair.new(a, b)
return setmetatable({a or 0, b or 0}, pair)
end

function pair.__tostring(self)
return '(' .. self[1] .. ', ' .. self[2] .. ')'
end

function pair.__add(a, b)
return pair.new(a[1] + b[1], a[2] + b[2])
end

function pair.__unm(self)
return pair.new(-self[1], -self[2])
end

function pair.__sub(a, b)
return a + -b
end

print(pair.new(1, 0) + pair.new(0, 1))
Report Abuse
cody123454321 is not online. cody123454321
Joined: 21 Nov 2009
Total Posts: 5408
13 Jan 2017 09:55 PM
You remind me of me when cntkillme really introed me into heavy metatabling and fenv.
I highly suggest crying at this... https://www.roblox.com/library/259272831/Function-Environment-GO-BOOM

As well as looking at the description.

One more thing to note about OOP styled coding with metatables.

local a = #}#
##a.new = function()
local this = {}

this.example = function() print("Annoyance")

return this
end

return a

This is a conventional 'class' that you would build. Note the function. If you call a.new() 1k times, you just created 1000 functions. Say you have 16 functions and call a.new() 1k times. You made 16k functions.

Use a static-themed convention. Store all your functions in a. Pass your object you wish to do unto into the first variable usually called 'self'.

Here is a little more example:
github com/Hydroque/Lua-Inventory-API

Also, when using metatables, you run into the same exact problem. You want to create functions not stored in the table (local globally) when the table is created. You can just pass a reference to the same exact function and use it as a method.
Report Abuse
Froast is not online. Froast
Joined: 12 Mar 2009
Total Posts: 3134
13 Jan 2017 09:57 PM
metatables are not for OOP what is even happening on this thread what are you people doing
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
13 Jan 2017 10:01 PM
smh froast
'I know metatables aren't specifically used for object-oriented programming, but I find them to be well-equipped for it.'
Report Abuse
cody123454321 is not online. cody123454321
Joined: 21 Nov 2009
Total Posts: 5408
13 Jan 2017 10:09 PM
Vector3.new() + Vector3.new()

print(type(Vector3.new()))

Hmm...
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
13 Jan 2017 10:10 PM
Roblox added typeof in case you weren't here when they did.
Now it returns the actual type (i.e. getmetatable(obj).__type such that obj is a valid Roblox type (registered in the registry))
Report Abuse
cody123454321 is not online. cody123454321
Joined: 21 Nov 2009
Total Posts: 5408
13 Jan 2017 10:13 PM
But they still deny namespace-ception?

local a = {x = 0, y = 0}

setmetatable(a, {__type = "Vector3"})

Vector3.new().lerp(Vector3.new(), a, 0.5)
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
13 Jan 2017 10:16 PM
Yes because that metatable you're creating isn't going to be registered in the Lua registry.
Report Abuse
Real_Spooky is not online. Real_Spooky
Joined: 23 Oct 2013
Total Posts: 3149
13 Jan 2017 10:31 PM
haha, I knew that little opinion disclaimer would come in handy
Report Abuse
Froast is not online. Froast
Joined: 12 Mar 2009
Total Posts: 3134
13 Jan 2017 10:32 PM
would have been more useful if you put it in your original post
Report Abuse
cody123454321 is not online. cody123454321
Joined: 21 Nov 2009
Total Posts: 5408
13 Jan 2017 11:13 PM
Well... it was about time they added that. That was more anticipated than anything else they have made.
Report Abuse
clc02 is not online. clc02
Joined: 30 Dec 2007
Total Posts: 7393
13 Jan 2017 11:33 PM
I highly recommend using functional programming rather than object orient programming. In my experience OOP leads to more confusion than is necessary, especially if the maintainer for a piece of code changes, I just wrote a sliding scale program for my wife (Type one diabetic) and I can use it as an example, because it was my first time actually working with the windows API, and it was object oriented. It might get a little tech heavy, but if you're talking about metatables you probably don't mind that, otherwise feel free to stop here.

The desired function: Let the user input text into two text boxes, calculate a number, based on the numbers entered into textbox1 and textbox2 (TB1 and TB2 from now on), and put it as the text for a TB3, and set the font to be larger under a set condition of TB2 (Harder to read with low blood sugar)

Functionally it would be simple,
int n1 = tonumber(TB1.Text);
int n2 = tonumber(TB2.Text);
int n3 = n1/5 + n2/20;
TB3.Text = tostring(n3);
if (n2 < 90) TB3.FontSize = 16
else TB3.FontSize = 12

However, the windows API is object oriented. First abstraction, the form is the parent object, and you must specify TB1.Text as System.Form1.Page1.TextBox1.Text. That's not too bad, but it gets worse. TB1.Text, is not in fact a string. String isn't an object, and since text is an object under TB1, it must be an object, so they made a new class called SystemString, so first I need to convert SystemString to string, and since this conversion takes an object, the implemented way to convert it (To a table of characters, not actually a string) using an object, called marshal, which is of course in a heirachy of objects, so the function is called as: System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(System.Form1.Page1.TextBox1.Text).ToPointer();

So after this, you can convert it to a string, and convert that to a number, do computations, and turn it back using standard libraries (s#################t#############That's the end of functional programming, back to OOP
Next step, changing the font.

System::Form1::Page1::TextBox3.Font = gcnew System::Drawing::Font(L"Arial", 16));
What could've been a simple TextBox3.Font = "Arial" TextBox3.FontSize = 16; now becomes bogged down, you need to know the heirarchy of Font to the topmost level, know that it's a drawing, know that Drawing is under the System, and remember to initialize a new font you need to give it a string for font name and a number for the font size.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
13 Jan 2017 11:35 PM
"However, the windows API is object oriented" HAHAhahaHAhahA
Report Abuse
Z_Doctor is not online. Z_Doctor
Joined: 17 Jan 2010
Total Posts: 750
13 Jan 2017 11:48 PM
When I first was learning about metatables, I didn't understand them. I found them to bulky and the examples people would show me were better off as functions. But then I figured I could somehow use them to make do oop. It's funny, I haven't been on for a while and haven't tried to refine my first attempts, but today I was working on using metatables for oop. And I think I made some really nice progress.

https://www.roblox.com/games/611620934/ObjLua

I created this place as a proof of concept. It's not copy locked, so take a look at the code and see if you like it. I trying to make my code look more like java, and have some examples that I think came out pretty nice. Tell me what you guys think.
Report Abuse
cody123454321 is not online. cody123454321
Joined: 21 Nov 2009
Total Posts: 5408
14 Jan 2017 12:16 AM
I made a script that allowed this to work...


class "Example" {
constructor = function() end,
destructor = function() end,
public = {
example = function(self) print(1) end

},
private = {},
protected = {}

}

register("Example") -- or something like that

Example.example() --> 1

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