Jump to content

bytedata resource uses


Recommended Posts

There are no "string types", the script simply converts the numeric and string data into a string which looks like the same values stored in RAM or HDD. Format argument (the first values passed to dataToBytes and bytesToData functions) describes what data you store/retrieve.

For example, if you want to store two 2-byte integers v1, v2 and a 4-byte integer v3 (all signed and little-endian), this is how it would look like:

local packed = dataToBytes("ssi", v1, v2, v3) 

"s" means a 2-byte integer and "i" means a 4-byte integer. So it's "s" for v1, another "s" for v2 and "i" for v3. And now the string packed contains a string whose length is 8 characters. First two bytes contain the value of v1, the following two bytes contain v2 and the last four bytes contain v3. If type identifiers in the format string are repeating, you can put a number before the letter instead of repeating it. In this case, you can substitute "ssi" with "2si".

Now, when you need to get the values from the string packed, you need to use bytesToData with the same format string and it will return the values which were passed to bytesToData:

v1, v2, v3 = bytesToData("ssi", packed) 

If my explanation wasn't good enough, you could try reading this: http://docs.python.org/3.3/library/struct.html . I made the functions to work like struct.pack and struct.unpack in Python do.

By the way, it looks like the example in my website has some mistakes. Also, I noticed that while the floating point data works on standalone Lua interpreter, it gives incorrect results on MTA client (not sure about the server). This is probably because of reduced mathematical precision of calculations on the client.

So I hope that's enough info for you :)

Link to comment
  • 3 weeks later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...