|
| 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 |
|
|
| |
|
|
| 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 |
|
|
|
| 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 |
|
|