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: I decided to overwrite math rules.

Previous Thread :: Next Thread 
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Jun 2014 08:32 PM
For some weird reason I swapped addition and subtraction by making a function to create a 'number' (A userdata in disguise.) that when used in math will return subtraction if added, and addition if subtracted. This coding can explain it better;

function CreateNumberValue(var, value)
getfenv()[var] = newproxy(true)
getmetatable(getfenv()[var]).__add = function(a, b)
if a == getfenv()[var] then
return value - b
elseif b == getfenv()[var] then
return a - value
end
end
getmetatable(getfenv()[var]).__sub = function(a, b)
if a == getfenv()[var] then
return value + b
elseif b == getfenv()[var] then
return a + value
end
end
end

CreateNumberValue("X", 10)
print(X + 5)
print(5 + X)
print(X - 5)
print(5 - X)


Output;
--> 5
--> 5
--> 15
--> 15


The only problem I am currently having is that if you attempted to say

CreateNumberValue("X", 10)
CreateNumberValue("XX", 10)
print(XX + X)

It will not print 0 however, it will error;

-->attempt to perform arithmetic on local 'b' (a userdata value)

How would I detect this and get it to return the 'number' value of that userdata?

Do not spam me saying it is a userdata and cannot hold a number, I said 'number' as if to use it fairly losely.

Basically I would like it if I attempted to do this with 2 userdata's (Like above) for it to output as if I used a userdata and a number. Is this possible?
Report Abuse
Duelingwarlord is not online. Duelingwarlord
Joined: 15 Feb 2010
Total Posts: 22333
27 Jun 2014 08:35 PM
get lost n0b
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Jun 2014 08:37 PM
Better fix that typo before I get spam for posting false information...


FIXED OUTPUT;

Output;
--> 5
--> -5
--> 15
--> 15


I forgot the negative sign (-) infront of the second 5.
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Jun 2014 09:15 PM
Nevermind guys. I got some help from Notunknown99

It now works on two userdatas!

Here is the full overwritten math rules script. In order for the calculations you do, to run with these new rules, one number in the calculation (or both) has to have been created with the function!


function CreateNumberValue(var, value)
getfenv()[var] = newproxy(true)
getmetatable(getfenv()[var]).__index = function()
return value
end
getmetatable(getfenv()[var]).__add = function(a, b)
if a == getfenv()[var] then
if type(b) == "userdata" then
return value - b.v
else
return value - b
end
elseif b == getfenv()[var] then
if type(a) == "userdata" then
return a.v - value
else
return a - value
end
end
end
getmetatable(getfenv()[var]).__sub = function(a, b)
if a == getfenv()[var] then
if type(b) == "userdata" then
return value + b.v
else
return value + b
end
elseif b == getfenv()[var] then
if type(a) == "userdata" then
return a.v + value
else
return a + value
end
end
end
getmetatable(getfenv()[var]).__mul = function(a, b)
if a == getfenv()[var] then
if type(b) == "userdata" then
return value / b.v
else
return value / b
end
elseif b == getfenv()[var] then
if type(a) == "userdata" then
return a.v / value
else
return a / value
end
end
end
getmetatable(getfenv()[var]).__div = function(a, b)
if a == getfenv()[var] then
if type(b) == "userdata" then
return value * b.v
else
return value * b
end
elseif b == getfenv()[var] then
if type(a) == "userdata" then
return a.v * value
else
return a * value
end
end
end
getmetatable(getfenv()[var]).__mod = function(a, b)
if a == getfenv()[var] then
if type(b) == "userdata" then
return value % b.v
else
return value % b
end
elseif b == getfenv()[var] then
if type(a) == "userdata" then
return a.v % value
else
return a % value
end
end
end
getmetatable(getfenv()[var]).__pow = function(a, b)
if a == getfenv()[var] then
if type(b) == "userdata" then
return value ^ b.v
else
return value ^ b
end
elseif b == getfenv()[var] then
if type(a) == "userdata" then
return a.v ^ value
else
return a ^ value
end
end
end
getmetatable(getfenv()[var]).__unm = function()
if value < 0 then
return value
else
return value * -1
end
end
end


CreateNumberValue("X", 5)
CreateNumberValue("XX", 5)
print(X + XX)
print(XX + X)
print(X - XX)
print(XX - X)
print(XX / XX)
CreateNumberValue("XX", 25)
print(XX * X)
CreateNumberValue("XXX", XX * X)
print(XXX + 0)

-->0
-->0
-->10
-->10
-->25
-->5
-->5




The operators %, ^, and -(negative sign) still do the same thing.
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