Only serialize variable when a bool equals true
Options
Nobrega
✭
I have a quaternion that is constantly changing and needs to be serialized. However, I only need to serialize it while a bool equals true (otherwise it will just waste bandwidth). This is easy to write to the steam, but how can you check if a variable exists within the stream while reading it? There are multiple quaternions within the stream, sometimes this one will be included and other times it wont.
0
Best Answer
-
Hi @Nobrega,
I guess this is not possible without a certain workaround which would be, to send one additional information which tells the receiving client, which data he actually received. The first option therefore might be an array of bool values, where each bool describes, if the related Quaternion is sent. Another option would be to use a single byte - or a short value if there are more than eight Quaternions. For example let's say you have eight Quaternions and you want to send the first, second, fourth and seventh of them, then the additional information would be 2^7 (first Quaternion) + 2^6 (second) + 2^4 (fourth) + 2^1 (seventh) = 210, which you have to send first. On the receiving side you now have this additional information and you can use it. Therefore compare it to a certain power of 2 value (the same you used for creating that information. For the first Quaternion in this example you have to check if the additional information is larger than or equal to 2^7, means 210 >= 2^7 in code. If this condition is true, you know that you actually received this Quaternion and can read from the stream. If this condition is true you additionally have to calculate the difference before continuing with the next value, means that you have to continue with the result of 210 - 2^7 in this case. If this condition is false you know that you didn't receive the certain Quaternion and can continue.
Hopefully this is somehow clear and will help you. If you have question about this approach, please feel free to ask, maybe I can add some code snippets then.5
Answers
-
Hi @Nobrega,
I guess this is not possible without a certain workaround which would be, to send one additional information which tells the receiving client, which data he actually received. The first option therefore might be an array of bool values, where each bool describes, if the related Quaternion is sent. Another option would be to use a single byte - or a short value if there are more than eight Quaternions. For example let's say you have eight Quaternions and you want to send the first, second, fourth and seventh of them, then the additional information would be 2^7 (first Quaternion) + 2^6 (second) + 2^4 (fourth) + 2^1 (seventh) = 210, which you have to send first. On the receiving side you now have this additional information and you can use it. Therefore compare it to a certain power of 2 value (the same you used for creating that information. For the first Quaternion in this example you have to check if the additional information is larger than or equal to 2^7, means 210 >= 2^7 in code. If this condition is true, you know that you actually received this Quaternion and can read from the stream. If this condition is true you additionally have to calculate the difference before continuing with the next value, means that you have to continue with the result of 210 - 2^7 in this case. If this condition is false you know that you didn't receive the certain Quaternion and can continue.
Hopefully this is somehow clear and will help you. If you have question about this approach, please feel free to ask, maybe I can add some code snippets then.5