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: first time using metatables

Previous Thread :: Next Thread 
lostend is not online. lostend
Joined: 21 Aug 2011
Total Posts: 8265
22 Aug 2015 01:38 PM
and i figured out metamethods have a double underscore. >.<
so ive been doing this:
_add, but it is actually this:
__add


well anyways, this is what I made. pretty simple, metatables are a LOT easier than most would think.
this allows you to perform arithmetic on tables

local tab1={5,7,'Hello','MOM OLLEH'}
local tab2={1,2,8,'SELBAT','NAMTAB'}

function getTotals(a,b)
local totals={0,0}
for i=1,#(#a>=#b and a or #b>#a and b) do
totals[1]=totals[1]+(tonumber(a[i]) or 0)
totals[2]=totals[2]+(tonumber(b[i]) or 0)
end
return totals
end

local arithmetic={
__add=function(a,b)
local totals=getTotals(a,b)
return totals[1]+totals[2]
end,
__mul=function(a,b)
local totals=getTotals(a,b)
return totals[1]*totals[2]
end,
__div=function(a,b)
local totals=getTotals(a,b)
return totals[1]/totals[2]
end,
__sub=function(a,b)
local totals=getTotals(a,b)
return totals[1]-totals[2]
end,
__mod=function(a,b)
local totals=getTotals(a,b)
return totals[1]%totals[2]
end,
__pow=function(a,b)
local totals=getTotals(a,b)
return totals[1]^totals[2]
end,
__unm=function(a)
local new_Table={}
for i=1,#a do
if type(a[i])=='number' then
new_Table[#new_Table+1]=-(a[i])
elseif type(a[i])=='string' then
new_Table[#new_Table+1]=(a[i]:reverse())
end
end
return new_Table
end
}

setmetatable(tab1,arithmetic)
setmetatable(tab2,arithmetic)


print(tab1+tab2)
print(tab1*tab2)
print(tab1/tab2)
print(tab1-tab2)
print(tab1%tab2)
print(tab1^tab2)
print(table.concat(-tab1,' '))
print(table.concat(-tab2,' '))
Report Abuse
lostend is not online. lostend
Joined: 21 Aug 2011
Total Posts: 8265
22 Aug 2015 07:21 PM
buamp
Report Abuse
lokkut is not online. lokkut
Joined: 18 Mar 2009
Total Posts: 1794
22 Aug 2015 07:31 PM
what's the point in this thread?
Report Abuse
ShungTzu is not online. ShungTzu
Joined: 14 Jun 2014
Total Posts: 959
22 Aug 2015 07:43 PM
Holly mother of tables, Batman!
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