I have an issue with money printers that I am looking for an easy solution that doesn’t require hacky code. At the moment, I have money printers that go up to 2.1b in capacity, which as I am sure you have already guessed is the maximum I can store in an integer. The amount of money a printer has is stored in a NetworkVar, which does not seem to have anything that goes over integer. Does anybody have any ideas?
This might be a very stupid idea, but how about storing it in binary as a string?
What kind of performance impact would this have? This would be a lot more type conversions. Most people store about 30-50 printers in their base, times 32 players on a full server.
I might be wrong, but I dont believe there would be any significant sort of impact, as the entities were already networking stuff. Instead of networking Integers you would be networking Strings, and using
math.BinToInt to convert them when needed to use a normal number. Maybe this is a totaly dumb idea, but I cant think of any other means of storing such information
I will look into doing this, thank you.
[lua]lua_run Entity(1):SetNWInt(‘t’,200000000000000000000000000000000000000); lua_run_cl print( Entity(1):GetNWInt(‘t’) )[/lua]
You seem to lose accuracy with higher values, but at least it accepts them.
If possible, you could just store the number of times money has been generated, then multiply by amount of money it generates each cycle. AFAIK, either the database or Lua’s still going to mess up when you use extreme values.
Did you even read the title?
You could use 2 integers to cover a low and high part of the number, e.g.
-- Defining NetworkVar self:NetworkVar("Int", 0, "ThisVarLowPart") self:Networkvar("Int", 1, "ThisVarHighPart") -- Function to get actual value function ENT:GetThisVar() return self:GetThisVarHighPart() * 0x100000000 + self:GetThisVarLowPart() + 0x80000000 end -- Function to set actual value function ENT:SetThisVar(num) self:SetThisVarLowPart((num % 0x100000000) - 0x80000000) self:SetThisVarHighPart(math.floor(num / 0x100000000)) end
This’ll have the range of a signed 64bit integer, so you’ll succeed the accuracy of a Lua number (double) way before you reach the limit of this.
I’d say you could store the number as a string and not a binary string.
It would work the same way and would be better because you would not have to make the conversion, only convert the number to a string thanks to
I don’t think 20-50 type conversions would be much for your server to handle, but if you’re worried about performance you could use unsigned types in net messages.
Also you could consider having a single value represent like $100 or something if you somehow getting 2.1 billion.
Other than that, bigdogmat’s solution is the best performance wise
Lose accuracy is an understatement, this gains no more range than the original had. All that does is completely mangle the number you’ve given.
You could handle it as unsigned and at least double your value.
net.WriteUInt and net.ReadUInt will network it for you (but really you could keep using WriteInt and ReadInt). The important part is when you display the string use string.format(’%u’, variable).
Other than that store it as two integers and handle string formatting manually.
Edit: Just realized you were using NW vars; brain fart.
You should just split the money into multiple vars and handle the math yourself.
Updated the code in my post, made a small mistake.
A string is just an array of characters, which are all represented by the same amount of bits, so storing a 1 or a 0 uses as much space as storing any other number or letter.
If using a string was the only option, it would be much more efficient to store the actual number in the string rather than converting it to binary.
Thanks for the help guys. It was fixed by changing the data type to string, and doing type conversions.