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 » Scripting Helpers
Home Search
 

Re: Fake math methods?

Previous Thread :: Next Thread 
Augmentative is not online. Augmentative
Joined: 08 Jul 2014
Total Posts: 593
18 Aug 2014 01:59 PM
Is there a way to make fake math methods?

EX; local num = math.round(3.47)

Could I put a function in a global script that is called when this happens? Im pretty sure it's possible but I'm not sure how.
Report Abuse
ROBLOXTHEMOVIEs is not online. ROBLOXTHEMOVIEs
Joined: 11 Jun 2012
Total Posts: 5895
18 Aug 2014 02:00 PM
just use math.floor
Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
18 Aug 2014 02:03 PM
_G.add = function(x, y)
return x+y
end

in another script:

print(_G.add(1,2))

output:
3
Report Abuse
Augmentative is not online. Augmentative
Joined: 08 Jul 2014
Total Posts: 593
18 Aug 2014 02:04 PM
Nah, that only rounds down. Besides I'm making a library for myself and I want to know if its possible.
Report Abuse
Augmentative is not online. Augmentative
Joined: 08 Jul 2014
Total Posts: 593
18 Aug 2014 02:04 PM
@Kodran

I want it to be using the math method like so:

math.round(3.47)
Report Abuse
robocu3 is not online. robocu3
Joined: 13 Mar 2009
Total Posts: 6485
18 Aug 2014 02:05 PM
_G would permit you to globally use a function, assuming you use it right. As for the round function;

function Round(N,T)return T and Round(N/T)*T or math.floor(N+.5)end

print(Round(.6,.5))
print(Round(.2))
print(Round(7,10))
> .5
> 0
> 10

-=Robo=-
Report Abuse
Augmentative is not online. Augmentative
Joined: 08 Jul 2014
Total Posts: 593
18 Aug 2014 02:06 PM
I don't think you guys understand. I want to be able to use the actual math method to create a global function like this: math.round(3.47)
Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
18 Aug 2014 02:07 PM
No, the most similar you could make it is like this:

_G.math = {
["round"] = function() print("whatever") end
["add"] = function(x, y) return x+y end
}

then you can do:

_G.math["add"](1, 2)
Report Abuse
robocu3 is not online. robocu3
Joined: 13 Mar 2009
Total Posts: 6485
18 Aug 2014 02:11 PM
Math is a Lua keyword, @Kodran.
It'd work if that wasn't the case, but you forgot a comma in the table. :P

_G.math = {
["round"] = function() print("whatever") end,
["add"] = function(x, y) return x+y end
}

then you can do:

_G.math["add"](1, 2)

-=Robo=-
Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
18 Aug 2014 02:11 PM
Actually after some tests I came up with this:

_G.math = {
round = function() print("hi") end
}

_G.math.round()
Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
18 Aug 2014 02:12 PM
Actually it doesn't matter that it's a keyword, it still works.
Report Abuse
Augmentative is not online. Augmentative
Joined: 08 Jul 2014
Total Posts: 593
18 Aug 2014 02:13 PM
Could you use getfenv on a metatable?
Report Abuse
robocu3 is not online. robocu3
Joined: 13 Mar 2009
Total Posts: 6485
18 Aug 2014 02:13 PM
It does? I figured it'd throw an error... I've never used keywords out of context lol

-=Robo=-
Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
18 Aug 2014 02:19 PM
If you want to set it up in every script you have and want to rewrite every math function there is (floor, random, etc.) then this is possible:

math = {
round = function() print("hi") end
}

math.round()
Report Abuse
DaMrNelson is not online. DaMrNelson
Joined: 27 Jul 2009
Total Posts: 4405
18 Aug 2014 02:21 PM
Here's a script I hacked together. It replaces the math variable for the current script with a metatable that allows you to set custom math properties/functions.

local oldMath = math
local customMath = {}
local mt = {
__index = function(tbl, index)
if (oldMath[index]) then
return oldMath[index]
else
return customMath[index]
end
end,

__newindex = function(tbl, index, value)
customMath[index] = value
end
}
math = setmetatable({}, mt)

print(math.random(1, 2)) --> 1/2
math.round = function() print("Round!") end
math.round() -- Round!

Wiki Profile: http://wiki.roblox.com/index.php/User:Nelson
Report Abuse
Augmentative is not online. Augmentative
Joined: 08 Jul 2014
Total Posts: 593
18 Aug 2014 02:22 PM
What about something like this??

getfenv()['math'] = _G

_G.round = function(Number)
print(Number)
end

math.round(3)
Report Abuse
robocu3 is not online. robocu3
Joined: 13 Mar 2009
Total Posts: 6485
18 Aug 2014 02:23 PM
daaaaamn
gg nelson

-=Robo=-
Report Abuse
Augmentative is not online. Augmentative
Joined: 08 Jul 2014
Total Posts: 593
18 Aug 2014 02:23 PM
Wow thats amazing @Nelson

Do you mind explain whats going on I can hardly read it :o
Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
18 Aug 2014 02:24 PM
^^
that works augmentative
Report Abuse
robocu3 is not online. robocu3
Joined: 13 Mar 2009
Total Posts: 6485
18 Aug 2014 02:24 PM
I don't think you can set _G to anything. it's a global variable table lol.

-=Robo=-
Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
18 Aug 2014 02:25 PM
I just got ninja'd by a million people xD
Report Abuse
Augmentative is not online. Augmentative
Joined: 08 Jul 2014
Total Posts: 593
18 Aug 2014 02:28 PM
Lol my method worked but it negates all the other custom roblox ones which means I'd have to re-write all of them...

Ill just stick with Nelsons and figure out how it works :P

Thanks for the help guys
Report Abuse
DaMrNelson is not online. DaMrNelson
Joined: 27 Jul 2009
Total Posts: 4405
18 Aug 2014 02:29 PM
@Augmentative
Sure thing! I'll just comment every line c:

local oldMath = math -- We save this so we can keep the old math functions
local customMath = {} -- A table to store our custom math functions
local mt = {
__index = function(tbl, index) -- Metatable stuff. Basically, when you go math.round()
-- tbl is the blank table we put in. Index is the thing we're looking for, "round"
if (oldMath[index]) then -- If it existed in the old math table
return oldMath[index]
else
return customMath[index] -- If not we return the version from our customMath table. No need to check if nil
end
end,

__newindex = function(tbl, index, value) -- More metatable stuff. Basically, when you go math.round = function() bleh end
-- tbl and index are the same as above. Value is the thing after the equal sign (or in this case, our round function)
customMath[index] = value
end
}
math = setmetatable({}, mt) -- Set the math varaible as a metatable with the commanding table being mt

print(math.random(1, 2))
math.round = function() print("Round!") end -- Sets customMath.round to function() print("Round!") end
math.round()

Wiki Profile: http://wiki.roblox.com/index.php/User:Nelson
Report Abuse
Augmentative is not online. Augmentative
Joined: 08 Jul 2014
Total Posts: 593
18 Aug 2014 02:33 PM
Alright I get it now. Thanks :)

One last question, is it possible to make the metatables global so I can use math.round in any script?
Report Abuse
DaMrNelson is not online. DaMrNelson
Joined: 27 Jul 2009
Total Posts: 4405
18 Aug 2014 02:34 PM
You could store that function in ModuleScript, then use this:

math = require(game.ReplicatedStorage.MathModule)

But other than that, sorry but there is no way.

Wiki Profile: http://wiki.roblox.com/index.php/User:Nelson
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • 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