GML Storing multiple values into one (great for RPG`s)

Z

zendraw

Guest
So recently i wanted to know how long a number can game maker have and people gave me various answers

@Bingdom https://forum.yoyogames.com/index.php?threads/biggest-number.43446/#post-267419

@TsukaYuriko -3.4E+38 to +3.4E+38 or -1.7E+308 to +1.7E+308, depending on whether a 32-bit or 64-bit float is used.

@hippyman Tsuka just told you the answer. Learn scientific notation.

@Joe Ellis Unsigned 64-bit integer: 18,446,744,073,709,551,615
signed 64-bit integer: 9,223,372,036,854,775,807
64-bit float: 4,503,599,627,370,495

Though cus all variable numbers are stored as floats, I guess 4,503,599,627,370,495 is the max value a variable can hold,
but buffers let you use u_64, so I think 18,446,744,073,709,551,615 is the max for that

@GMWolf Yeah but @Joe Ellis isn't 100% correct;
Numbers are not always stored as floats, the runtime will decide hat format is best and convert between them.
If you stick to integers, you should be able to get 32 or 64 bit numbers (depending on target platform, I believe).

Signed 32 bit numbers get you 2,147,483,647

Signed 64 bit gets you 9,223,372,036,854,775,807

-as @Joe Ellis stated, you can use buffers to store unsigned integers, however, when reading them, they will be converted again and you will lose that extra gain, so manipulation won't work.

-Finally, although your own variables will be cast to the most appropriate format (either a double precision float, or integers), remember that built in variables like X, y or image_angle are always stored as single precision floats. They go up to 3.402823 × 10^38.
However, you have to deal with precision.
For example, if you want 1mm precision, don't make you map larger than 16km, or you will have precision issues.

Joe Ellis @GMWolf Ahh, someone on here told me they're all stored as floats, is the sorting just a gms2 thing or has it always done that?

GMWolf As far as I know it's since GMS1.x, but could be before?

The thing is, what type is going to be used is undefined, so it's best to always assume its going to be a floating point number.
If you really, really need to do things with types, and sizes, then GM may not be the tool for the job. (Or write a dll or something)

zendraw what im trying to do is store values in a single value. like 1234567 where 12=val1 345=val2 67=val3. so i wanted to know how meny values i can store like this.

zendraw main reason is to save it as single value. when i use that value i fracture it in the manner i wrote in my previous comment.

Joe Ellis @zendraw if your doing it like that you may aswell use strings, then the length is unlimited or far more than 16-20 digits
like:

str = "1234567"

val1 = real(string_copy(str, 1, 2))
val2 = real(string_copy(str, 3, 3))
val3 = real(string_copy(str, 6, 2))

--------------------------------------------------------------------------------------------------------------------------------

and so i came to the conclusion i dont need to store them in a real value and limit myself, but a string and have an infinite number of values in one string as Joe suggested.

then i came with the problem how to store each value so that theres aways a pair of 4 numbers
like so 0321 0012 0004 0032
if i simply turn a real number into a string it will resault in 32112432 and if i try to extract those values again it will fail so what im doing to store the exact string back is this

Code:
t=string_insert(t, '0000', 5);
a=string_insert(a, '0000', 5);
ap=string_insert(ap, '0000', 5);

t=string_copy(t, string_length(t)-3, 4);
a=string_copy(a, string_length(a)-3, 4);
ap=string_copy(ap, string_length(ap)-3, 4);

return string(t)+string(a)+string(ap);
basically you add 4 0`s to your values as a string and then remove what 0`s you dont need from that string and finally you combine them all.

now this works fine for me, if anyone has a better way to do it please do so, the reason for this topic is so people can learn somthing, and hopefully expand this idea, i think this is a great way to store great amount of info for items in a RPG without worrying about arrays and stuf. also making it in a thread was @TsukaYuriko `s idea so ban hur if its against the rules.

anyway any additions are welcomed. much love to @TsukaYuriko
 

GMWolf

aka fel666
I still think arrays are the better way to go, that is what they are supposed to do.

Code:
a[0] = 5;
a[1] = 10;
a[2] = 6;

t = a[0] //t is now 5
t = a[1] //t is now 10
t = a[2] //t is now 6


b = a; //now b holds all the same values as a.

t = b[1] // t is now 10;
You can use arrays like any other variables otherwise, passing them between functions, etc.

Also, now that this isn't a status update anymore, I would like to add that tsuka answered you original question really well.
You see, the maximum number is 1.7×10^308.
That means 17 followed by 307 0s.
Or
170000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
I hope i didn't miscount...

I hope you see why scientific notation is so useful.
 
Last edited:
Z

zendraw

Guest
arrays dont really make much sense. i mean the whole point is to write everything as a single value. im not sayng i use that value in the instances themselvs. what im doing is im saving these values on a temporary file. like i open a chest now, it generates items, these items need to be aways in the world unless the chest is such that refreshes. so i save all these items on that temporary file, and to avoid a bazzilion read calls i do as meny calls as there are items. maximum being the inventory`s slots.

basically i convert all the diffrent values that an item has to a single value, then i save that value, then when i summon that item i load that value and convert it to the various values that the item needs. its basically for optimisation. so if i load 100 items, that will be 100 reads, if i use a single value. but if i use an array, and lets say each item has a minimum of 5 values. thats 500 reads. and thats a minimum values. with an array everything will be N times more. and chests wont be the only thing to store information on a file. so lets say 100 instances store info on that file 50 are chests 50 are others. the others store 5 values each =250 values, and reads at the begining of the level, the 50 chestseach have lets say 5 items X 5 values= 25 values X 50 = 1250 +250 = 1500 values minimum on a single file. where if you save a single value you store 50 others + 50 x 5 =300 values. like i sayd 5 times more, minimum.

but this is more useful also if you lets say store values on instances themselvs. so having 100 instances with 1 value is way better then 100 with 5.

like i sayd is for optimisation purposes. the philosophy is you convert the values only when you need them. otherwise they dont need to be eating memory.
 

GMWolf

aka fel666
Well, if you really want to get your read/write operations down, use buffers!
They allow you to store binary data, that can be loaded and saved really, really fast.
They also allow you to choose exactly what format each piece of data should be, so you save on space too .
 
Z

zendraw

Guest
give some example, ive never used buffers. is it somthing i set up in a persistent instance and then just use it or? im aiming for a room with almost empty instances. aside from instances that need code to 'animate'.
 
Z

zendraw

Guest
for half the video you write on paint...

edit: finnaly you open game maker. so basically buffers are a 1d grid/array
 
Last edited:
Z

zendraw

Guest
but they store info in bits, parts like arrays. theyr just managed more simply and efficiently then a grid or array.
 
Z

zendraw

Guest
so i thought about buffers and in this case i dont really see a reason to use them. reason being, with buffers i will load the information first in the buffer then distribute it in variables, thus there will be 2 transfers. with the direct approach the info will simply go to the variables.

altho im not entirely sure. reason being i will have that loaded information well stored in the buffer.

one thing i dont get is what happens with the buffer after i destroy the instance holding it?

like i create an inventory instance. then create the buffer. load the data. manage it w/e. then i destroy the instance. do i destroy the buffer manually like data structures? i havent looked up all the buffer functions yet.
 

GMWolf

aka fel666
If you had looked at the manual entry, you will see that you need to manually destroy the buffer.

thus there will be 2 transfers.
Reading from a text file is far more costly.
Internally, GM .pat probably uses a buffered stream, meaning that the data first is loaded in memory, then it is decoded (since it's stored as text, not numbers, very costly), then needs to be transferred to your variables.

But if all you wanted qas to reduce the number of read / write operations...
Then don't. It's a bad idea.
You will just end up with really hard to manage code, with practically no gain.
Reading one large value and then splitting it up into many values has practically the same cost as just reading 3 values to start with.
That is, unless I underestimated GML overhead...
 
Z

zendraw

Guest
what do you think is better.
-loading a string from file to an array, then managing that string in the array and fracturing it into real values for an instance.
or
-loading a string from file to a buffer, managing it in the buffer, like changing it deleting it entirely, and fracturing it into real values for an instance.

currently im using an array to store the strings i load from a file. i cant say its entirely nececery to seek more optimal way, for there will be at the most 5 inventory instances active at the same time. and they barely cost anything, performance wise. even if full with items.
 
Top