Binary-to-integer conversion?
Ignis
Member Posts: 72
The recent topic from peachpellen and jmp909's suggestion to use a binary number routine now seems applicable to an aspect of my own game. Refer to that topic: http://gamesalad.com/forums/topic.php?id=6715
My question is, how would I approach writing an expression to convert a binary number string into an integer in GS? For example, if I have a string "101", how could I convert this to "5" with minimal fuss and minimal rules?
Aside from this, I'm pondering how to manipulate the binary string in advance of converting it. I want to store the binary number until a certain rule triggers its conversion and checks its integer value. So, it might change from 10 to 10010, then to 1101 or something else. My first thought is: if I wanted to modify place #4 in the string, I could simply add 1000 to the string: thus 10 would become 1010. But then the problem becomes, place #4 can only be "on" or "off" according to binary principle; I would need to prevent the rule from adding another 1000 to the string, producing 2010 or 3010.
My overall goal here is to detect a certain specific combination of events, which are tracked by one game attribute (the binary string). For example, 5 actors would each affect one of the placeholders in the binary string, similar to peachpellen's "vials" example. If I had 5 actors (A, B, C, D, E), I might want to detect when A/B/D were "on" and C/E "off", thus the binary-to-integer equivalent of "26".
Perhaps there's an easier way? Any suggestions or ideas?
My question is, how would I approach writing an expression to convert a binary number string into an integer in GS? For example, if I have a string "101", how could I convert this to "5" with minimal fuss and minimal rules?
Aside from this, I'm pondering how to manipulate the binary string in advance of converting it. I want to store the binary number until a certain rule triggers its conversion and checks its integer value. So, it might change from 10 to 10010, then to 1101 or something else. My first thought is: if I wanted to modify place #4 in the string, I could simply add 1000 to the string: thus 10 would become 1010. But then the problem becomes, place #4 can only be "on" or "off" according to binary principle; I would need to prevent the rule from adding another 1000 to the string, producing 2010 or 3010.
My overall goal here is to detect a certain specific combination of events, which are tracked by one game attribute (the binary string). For example, 5 actors would each affect one of the placeholders in the binary string, similar to peachpellen's "vials" example. If I had 5 actors (A, B, C, D, E), I might want to detect when A/B/D were "on" and C/E "off", thus the binary-to-integer equivalent of "26".
Perhaps there's an easier way? Any suggestions or ideas?
Comments
item1 = 1 point
item 2 = 2 points
item 3 = 4 points
item 4 = 8 points
item5 = 16 points
item6 = 32 points
item7 = 64 points
item8 = 128 points
that way any result can only ever mean one certain combination (because each one has it's own binary column)
so item 1 + item 2 + item 4 = 11 (eleven) points and no other combination can have this value
in binary that would be 00001011 (allowing for 8 items there)
however if you need ABC to be different to CBA (eg order collected) then that's a whole different matter
j
I would recommend including a 9th position at the front of the string...say "5" so that your string reads 500001011. The five always remains and could be followed by all zeros if necessary.
I would just use modulus check rules to verify a place value. You may have trouble with literal binary conversions. Combining modulus formulas with unique column multipliers assigned to an actor should keep it fairly simple.
This is a clever way BTW to do boolean arrays.
@synthesis: also a great suggestion, thanks! I will keep this in mind for the future, using modulus formulas and such, as you describe. I might very well need such functionality sometime, especially the "boolean array" concept since GS hasn't yet implemented full array capabilities.