Hey! I’m looking for the best way to compress a cframe but not to byte so it can be jsonencoded
Couldn’t you serialize it as a string directly?
local function CFrameToString(CF)
local components = {CF:GetComponents()}
return table.concat(components, " ")
end
How would I convert this back?
I was trying to figure out how I could implement a solution using string patterns, however I’m lazy so I chose not to. Basically, you’d just search the string from start to end, stopping at the first white space (which are used as value separators) and storing each value in a table, then unpacking the table and constructing a new CFrame with the tuple values returned from unpack
.
I’ll write some simple demo code in Studio real quick since it’s too hard here.
So I’m not very good with string or table manipulation, but I think I’m close?
return CFrame.new(table.unpack(stng:gsub(" ",""):split(",")))
local separator = ", "
function EncodeCFrame(CF: CFrame): string
return table.concat({CF:GetComponents()}, separator)
end
function DecodeCFrame(encodedCF: string): CFrame
return CFrame.new(unpack(string.split(encodedCF, separator)))
end
Example
local CF = CFrame.new(1, 2, 3)
local encodedCFrame = EncodeCFrame(CF)
local decodedCFrame = DecodeCFrame(encodedCFrame)
print(CF == decodedCFrame) --> true
I’m getting this error for the Decode function
I printed the string that I’m putting into the function and I believe it’s fine
46.79191589355469 4.047104835510254 1.6304510831832886 1 0 0 0 1 0 0 0 1
What code have you got currently?
function decode(str)
local components = {}
local substrings = str:split(" ")
for _, substring in ipairs(substrings) do
table.insert(components, tonumber(substring))
end
return CFrame.new(unpack(components))
end
print(Cfr) -- "46.79191589355469 4.047104835510254 1.6304510831832886 1 0 0 0 1 0 0 0 1"
print(Compressor_Module.LightCFrameDescompress(Cfr)) -- forgot to rename function
You’re not using my functions, otherwise the format would’ve been 1, 2, 3, 4, ...
This is within the module that im using
Exactly what you said, just within my module
You got rid of the separator variable for some reason, and the separators don’t match now. You probably use " "
in the encoding function and ", "
in the decoding function. The issue is on your end
I just didn’t include the variable but they have the same seperator
I have no issues when using the two functions you sent
I’m looking through my code to see if the cframe is ever passed as nil when compressing
It turns out I made a stupid mistake with the cframes passed to encode. Thank you and @C_Sharper for helping!
The second parameter of string.split()
defaults to a single comma, “,”, you may want to use this as your separator/delimiter instead of a comma followed by a whitespace.
It was more for readability, as JSON is a format that is generally supposed to be readable by humans, so this helps if he were to print the whole JSON-encoded data at some point.
JSON encoded strings shouldn’t be parsed via string.split()
they should be correctly decoded using the JSONDecode
instance method of the HttpService.