steadyfi Posted April 5, 2015 Posted April 5, 2015 Hello How can I split a number into more numbers and store them in a table? Example: Number = 130 Split = 30,30,30,30,10 And then into a table: {30,30,30,30,10}OR Number = 54 Split = 30,24 And then into a table: {30,24} The biggest number will always be 30, if reached then go form another. Does anyone know ? Thanks
MTA Team botder Posted April 5, 2015 MTA Team Posted April 5, 2015 (edited) Does not work with negative numbers function split_number(number) local values = { } while (number > 0) do values[#values + 1] = (number >= 30) and 30 or number number = number - values[#values] end return values end Works with negative numbers: function split_number(number) local values = { } while number ~= 0 do if number > 0 then values[#values + 1] = (number >= 30) and 30 or number else values[#values + 1] = (number <= -30) and -30 or number end number = number - values[#values] end return values end Edited April 5, 2015 by Guest
steadyfi Posted April 5, 2015 Author Posted April 5, 2015 Does not work with negative numbers function split_number(number) local values = { } while (number > 0) do values[#values + 1] = (number >= 30) and 30 or number number = number - values[#values] end return values end Works with negative numbers: function split_number(number) local values = { } while number ~= 0 do if number > 0 then values[#values + 1] = (number >= 30) and 30 or number else values[#values + 1] = (number <= -30) and -30 or number end number = number - values[#values] end return values end Ok, but how can I make it work with another table and number ? I use it for a Weapon Magazine system. I get the length of the initial table, then make as many values as that one has. Example: Mags: 30, 30, 30, 15, 0 # of values in Mags: 5 Number = getPedTotalAmmo (EX: 100) Output: 30, 30, 30, 10, 0 How can I do this ? I want the player to keep the empty mags.
WhoAmI Posted April 5, 2015 Posted April 5, 2015 Done function for you. (i'm not good in math but it works) local max = 30; function split ( n ) if ( type ( n ) ~= "number" ) then return false; end local x = ( n / max ) + 1; local x = math.floor ( x + 0.5 ); local numbers = { }; local last = n; for i=1, x do local y = last / max; if ( y > 1 ) then table.insert ( numbers, max ); last = last - max; else local z = n % max; if ( z > 0 ) then table.insert ( numbers, z ); end end end return numbers; end for _, v in pairs ( split ( 100 ) ) do print ( v ); end It outputs 30 30 30 10
steadyfi Posted April 5, 2015 Author Posted April 5, 2015 Done function for you. (i'm not good in math but it works) local max = 30; function split ( n ) if ( type ( n ) ~= "number" ) then return false; end local x = ( n / max ) + 1; local x = math.floor ( x + 0.5 ); local numbers = { }; local last = n; for i=1, x do local y = last / max; if ( y > 1 ) then table.insert ( numbers, max ); last = last - max; else local z = n % max; if ( z > 0 ) then table.insert ( numbers, z ); end end end return numbers; end for _, v in pairs ( split ( 100 ) ) do print ( v ); end It outputs 30 30 30 10 Doesn't work as intended. I want to override the original table with the new values, but keeping the 0s
WhoAmI Posted April 5, 2015 Posted April 5, 2015 local max = 30; local mags = 5; function split ( n ) if ( type ( n ) ~= "number" ) then return false; end local numbers = { }; local last = n; for i=1, mags do if ( last > 0 ) then local y = last / max; if ( y > 1 ) then table.insert ( numbers, max ); last = last - max; else local z = n % max; table.insert ( numbers, z ); last = last - z; end if ( last == 0 ) then table.insert ( numbers, 0 ); end end end return numbers; end for _, v in pairs ( split ( 100 ) ) do print ( v ); end Output: 30 30 30 10 0
WhoAmI Posted April 5, 2015 Posted April 5, 2015 Oops. Fixed. function split ( n, max, mags ) if ( type ( n ) ~= "number" ) then return false; end local numbers = { }; local last = n; for i=1, mags do if ( last > 0 ) then local y = last / max; if ( y > 1 ) then table.insert ( numbers, max ); last = last - max; else local z = n % max; table.insert ( numbers, z ); last = last - z; end else table.insert ( numbers, 0 ); end end return numbers; end for _, v in pairs ( split ( 100, 30, 5 ) ) do print ( v ); end
steadyfi Posted April 5, 2015 Author Posted April 5, 2015 There is a problem: When I reload the Total Ammo goes down -30 but the clip is empty so Save and Load it and then I takes -30 again. I was not having this problem. Problem Representation: Example Bullets: 180 Reload: -30 bullets Reload isn't instant, there aren't bullets in weapon The split get's triggered and then I load => -30 bullets This is a scheme of how my system works: Shoot Event Trigger > Get Ammo > Split > Save ElementData > Load ElementData > GiveBullets depending on ElementData
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now