#Molang Encoding/Decoding Variable question/feedback?
1 messages · Page 1 of 1 (latest)
I rewrote some of it so now, it should in theory check if any of of the given variables were changed and only be running the query check once per loop so more efficient?!
Add variable.has_initialized = 0 and variable.new_hash = 0 in initialize then the rest of the below inside of pre_animation.
//Checks hash after running atleast once.
variable.hash_changed = 0
//Defining variables.
variable.state1 = query.property("prefix:state1")
variable.state2 = query.property("prefix:state2")
variable.state3 = query.property("prefix:state3")
variable.state4 = query.property("prefix:state4")
variable.state5 = query.property("prefix:state5")
variable.state6 = query.property("prefix:state6")
variable.state7 = query.property("prefix:state7")
variable.state8 = query.property("prefix:state8")
variable.state9 = query.property("prefix:state9")
variable.state10 = query.property("prefix:state10")
variable.state11 = query.property("prefix:state11")
variable.state12 = query.property("prefix:state12")
//Encoding as an old hash.
variable.old_hash = (variable.state1 * 1 + variable.state2 * 2 + variable.state3 * 4 + variable.state4 * 8 + variable.state5 * 16 + variable.state6 * 32 + variable.state7 * 64 + variable.state8 * 128 + variable.state9 * 256 + variable.state10 * 512 + variable.state11 * 1024 + variable.state12 * 2048)
//Check if any states changed.
variable.hash_changed = ((variable.old_hash != variable.new_hash) && variable.has_initialized ? 1 : 0)
//Encoding as a new hash.
variable.new_hash = (variable.state1 * 1 + variable.state2 * 2 + variable.state3 * 4 + variable.state4 * 8 + variable.state5 * 16 + variable.state6 * 32 + variable.state7 * 64 + variable.state8 * 128 + variable.state9 * 256 + variable.state10 * 512 + variable.state11 * 1024 + variable.state12 * 2048)
//Checks hash after running atleast once.
variable.has_initialized = 1
I'll mess around with it later, it needs some work. I think.
Thanks in advance for any pointers.
Molang Encoding/Decoding Variable question/feedback?
What's the question?
Basically is there any way to do this more efficiently or am over complicating this?
I know the above works after doing a bunch of testing, I'm just wanting if it can be optimized or improved.
I'd probably just encode properties into the bit field right away and then check if it's equal to an existing value or not (assuming that there's a way to retrieve saved bit field value, like if it's saved in a property)
Actually yeah, just initializing should work good enough.
What do you mean by initializing?
Set variable values to properties in initialize field
Wouldn't encoding the bits in that not update since it only loads once on load?
So if my values change at all I wouldn't know if they did.
They can't change if the world/entity is unloaded. They might've changed before it was unloaded, but then you were able to detect that
Also are you just looking to check if the values changed, or do you specifically want to encode them in a variable?
I'm looking to do a multi check, of 12 variables in one go and that was the most effcient way I could think of.
if the any of the vars changed. I need a variable to return true or false.
Why not just do v.changed = false; v.state1 != q.property('state1') || v.state2 != q.property('state2') || .... ? { v.state1 = q.property('state1'); .... v.changed = true; };
If you count the number of operations, it'd have less. However you are querying 2 times, but you can easily just save query in a temp for doing the checks and assignment, and it still should be less operations
I guess that could work, I was trying to stay away from calling the query twice.
Mainly, because I'd figured it'd be better for performance atleast on query checks.
But your method does have the benefit of being more readable and using less operations.
Yours can be modified to be almost the same btw, but with only 1 variable that you're saving, instead of 12+
Just calculate the hash right away and compare it to old value, that's it
v.changed = 0; t.hash = <calc hash>; t.hash != v.hash ? {v.hash = t.hash; v.changed = 1;};
I think that would be a better option for perfomance
Calculating hash has the same number of operations as comparing each property to a variable
Though you might argue that it's more expensive, but I doubt there's any meaningful difference. But the memory is definitely used less
Yeah that's a good method!
And thank you so much for the help Veka always super helpful and knowledgeable!
So, I attempted using that method, it fails for some odd reason, it can't detect any changes.
v.changed = 0; t.hash = (query.property('state1') * 1 + query.property('state2') * 2 + query.property('state3') * 4 + query.property('state4') * 8 + query.property('state5') * 16 + query.property('state6') * 32 + query.property('state7') * 64 + query.property('state8') * 128 + query.property('state9') * 256 + query.property('state10') * 512 + query.property('state11') * 1024 + query.property('state12') * 2048); t.hash != v.hash ? {v.hash = t.hash; v.changed = 1;};
gonna reload my game because that should work.
It just makes sense logistically.
Okay, so seems my changes to the file didn't save that was the issue. The above method works!
Thanks again!
Cool, your welcome!
It actually works faster too, which is a plus!