(Godot Engine) How do I know which exported enum flags are enabled in script - dictionary

By using the Godot engine and writing in the GDScript language,
let's say I have an enum declared as:
enum eTextMode {CHAR, NUMBER, SYMBOLS_TEXT, SYMBOLS_ALL}
And an export variable as:
export(eTextMode, FLAGS) var _id: int = 0
In the inspector panel I can see which flag is selected or not, but how can I know in code which specifically flag is selected?
By selecting in the inspector, for example: the NUMBER and SYMBOLS_TEXT flags, the _id variable will be set as 5
My approach is the following hard-coded dictionary:
var _selected_flags: Dictionary = {
CHAR = _id in [1, 3, 5, 7, 9, 11, 13, 15],
NUMBER = _id in [2, 3, 6, 7, 10, 11, 14, 15],
SYMBOLS_TEXT = _id in [4, 5, 6, 7, 12, 13, 14, 15],
SYMBOLS_ALL = _id in [8, 9, 10, 11, 12, 13, 14, 15]
}
Resulting in:
{CHAR:True, NUMBER:False, SYMBOLS_ALL:False, SYMBOLS_TEXT:True}
The above result is exactly what I'm expecting (a dictionary with string keys as they are defined in the enum with a boolean value representing the selection state).
How could I manage to do this dynamically for any enum regardless of size?
Thank you very much,

One tacky solution that I could manage is by not using an enum at all, but instead a dictionary like the following example:
const dTextMode: Dictionary = {CHAR = false, NUMBER = false, SYMBOLS_TEXT = false, SYMBOLS_ALL = false}
export(Dictionary) var m_dTextMode: Dictionary = dTextMode setget Set_TextMode, Get_TextMode
func Get_TextMode() -> Dictionary: return m_dTextMode
func Set_TextMode(_data: Dictionary = m_dTextMode) -> void: m_dTextMode = _data
An exported dictionary is not as good-looking as an exported enum with FLAGS, and by following this approach it kind of invalidates my initial problem.
By selecting CHAR and SYMBOLS_TEXT in the exported dictionary from the inspector, and then calling print(self.Get_TextMode()) the result is indeed what I expected:
{CHAR:True, NUMBER:False, SYMBOLS_ALL:False, SYMBOLS_TEXT:True}
I still can't figure out though how to achieve this result by using the export(*enum, FLAGS) aproach.
Edit: also, the setter function is not feasible to be used in script since the user must know to duplicate the dTextMode constant first, edit it and set is as an argument.

Thanks to the comments from #Thearot from my first answer, I have managed to figure out the following solution which meets all expectations, with one caveat: it seems like an overkill solution...
enum eTestFlags {FLAG_1, FLAG_2, FLAG_3, FLAG_5, FLAG_6}
export(eTestFlags, FLAGS) var m_iTestFlags: int = 0 setget Set_TestFlags
func Get_TestFlags() -> Dictionary: return self._get_enum_flags(m_iTestFlags, eTestFlags)
func Set_TestFlags(_id: int = m_iTestFlags) -> void: m_iTestFlags = _id
func _get_enum_flags(_val_selected: int, _enum: Dictionary, _bit_check_limit: int = 32) -> Dictionary:
var _enum_keys: Array = _enum.keys() ; _enum_keys.invert()
var _bin_string: String = ""
var _val_temp: int = 0
var _val_count: int = _bit_check_limit - int(_is_pow2(_bit_check_limit))
while(_val_count >= 0):
_val_temp = _val_selected >> _val_count
_bin_string += "1" if _val_temp & 1 else "0"
_val_count -= 1
var _bin_string_padded: String = "%0*d" % [_enum_keys.size(), int(_bin_string)]
var _result_dict: Dictionary = {}
for _str_id in range(_bin_string_padded.length(), 0, -1):
_result_dict[_enum_keys[_str_id - 1]] = bool(_bin_string_padded[_str_id - 1] == "1")
return _result_dict
func _is_pow2(_value: int) -> bool:
return _value && (not (_value & (_value - 1)))
Now, if I print(self.Get_TestFlags()) after selecting FLAG_2 and FLAG_6 the result is:
{FLAG_1:False, FLAG_2:True, FLAG_3:False, FLAG_5:False, FLAG_6:True}

You're on the right track but overcomplicating things. Without going too much into the math (see Wikipedia), here's what you'd do in Godot:
enum eTextMode {CHAR, NUMBER, SYMBOLS_TEXT, SYMBOLS_ALL}
export(eTextMode, FLAGS) var _id: int = 0
func _ready() -> void:
for modeName in eTextMode:
var bit_flag_value: int = int(pow(2, eTextMode[modeName]))
if _id & bit_flag_value:
printt("Flagged", modeName)
You can access the named fields of your enum like elements in an Array/Dictionary by default (iterate through the keys, get their 0-based index as values). The above math trick turns the 0-based index into the correct bit flag number, and if you (single) '&' it with the combined bit-flags value you can check whether or not that flag is set.

Related

Idiomatic `obj.value = f(obj)` in Ramda?

R.evolve lets us replace object properties with the result of a function applied to that property's current value:
R.evolve({ count: R.inc }, { count: 1 })
== { count: 2 }
But I frequently find I want to add a property calculated from multiple properties of input object:
assocFruitTotal({ appleCount: 5, orangeCount: 3 })
== { appleCount: 5, orangeCount: 3, fruitCount: 8 }
I came up with my own simple utility function:
const assocDerived = R.curry(
(name, f, obj) => ({
...obj,
[name]: f(obj)
});
... and I use it a lot:
const sumFruit = R.pipe(
R.props(['appleCount', 'orangeCount']),
R.sum);
const assocFruitTotal = assocDerived('fruitCount', sumFruit);
But the sheer frequency with which I use this makes me wonder why it's not
native to Ramda, as so many other convenient functions are. And that makes
me wonder whether I'm missing a better idiom that achieves the outcome -- that is, building up detail in an object by adding properties based upon combinations of other properties.
Is there an idiomatic functional programming construct I should be using instead?
Personally I would do it this way:
const fruitCount = applySpec({fruitCount: compose(sum, values)})
fruitCount({apple: 5, orange: 3})
//=> {"fruitCount": 8}
const withFruitCount = converge(mergeRight, [identity, fruitCount]);
withFruitCount({apple: 5, orange: 3});
//=> {"apple": 5, "fruitCount": 8, "orange": 3}
If there are non-count properties to exclude from the sum, you can use pickBy:
const pickCount = pickBy(flip(includes('Count')));
pickCount({appleCount: 5, orangeCount: 3, foo: 'bar'});
//=> {"appleCount": 5, "orangeCount": 3}
Let's start by recognizing that obj.value = f(obj) is a mutable assignment and therefore not a functional idiom to begin with. This is imperative-style thinking at work.
Storing a computed value as a property on your object is a misstep, in most cases. If either appleCount or orangeCount changes, there's nothing there to enforce the integrity of fruitCount.
fruitCount should be a function, not a property.
const fruitCount =
pipe
( props ([ 'appleCount', 'orangeCount' ])
, sum
)
fruitCount ({ appleCount: 1, orangeCount: 3 }) // 4
fruitCount ({ appleCount: 5, orangeCount: 3 }) // 8
If I had to guess, this is fake data and an example problem. In some scenarios, a computed value does make sense (memoisation is the first technique that comes to mind) but those cases make up the exception, not the rule. You say "the sheer frequency with which I use this ...", so I'd wager you do it in more areas than you should.
And as you pointed out, Ramda doesn't have a built-in for this, so this should further indicate that there are more conventional ways of solving this kind of problem.
An object-oriented programmer would assign this as a computed property -
const FruitData = function (apples = 0, oranges = 0)
{ this.apples = apples
this.oranges = oranges
}
Object.defineProperty
( FruitData.prototype
, 'fruitCount'
, { get () { return this.apples + this.oranges } }
)
const f =
new FruitData (3, 4)
console .log (f.fruitCount) // 7
When writing functional style, we leave OOP concepts at the door. Start thinking in terms of functions and your problems go away -
const FruitData = (apples = 0, oranges = 0) =>
({ apples, oranges })
const appleCount = fd =>
fd.apples
const orangeCount = fd =>
fd.oranges
const fruitCount = fd =>
appleCount (fd) + orangeCount (fd)
console .log (fruitCount (FruitData (10, 3))) // 13

How do you store a certain part of a string into a variable?

How do you store a certain part of a string into a variable?
For example:
x = myString // - But store the 9th character into a variable
Try this,
x = string_char_at(myString , 9);
This gets a single character from a string:
var x = string_char_at("This is my string", 4); //X == "s"
And you can use the string_copy function to copy parts of a string;
var x = string_copy("This is my string", 8, 2); //X == "my"

Creating Expression Tree Kotlin

Hi my task is to recursively create an expression tree from a List input.
ex. ("+", 2, 3)
"+"
/ \
2 3
I know my recursion is off, but I can't figure out how to make it work. I've been at it for longer than I'm proud to admit. Any insight is appreciated! This is what I have so
fun TreeBuild(input:List<Any>): Pair<Tree, List<Any>>{
var tree = Tree(input.get(0), Unit, Unit)
var list = input
System.out.println(list)
if(input.size == 1 ){
list = list.drop(1)
return Pair(Tree(input.get(0), Unit, Unit), list)
}
var flag: Boolean = (input.get(0) is Int)
if (!flag){
list = list.drop(1)
tree.left = TreeBuild(list).first
list = list.drop(1)
tree.right = TreeBuild(list).first
}
return Pair(tree, list)
}

How to convert List to Map in Kotlin?

For example I have a list of strings like:
val list = listOf("a", "b", "c", "d")
and I want to convert it to a map, where the strings are the keys.
I know I should use the .toMap() function, but I don't know how, and I haven't seen any examples of it.
You have two choices:
The first and most performant is to use associateBy function that takes two lambdas for generating the key and value, and inlines the creation of the map:
val map = friends.associateBy({it.facebookId}, {it.points})
The second, less performant, is to use the standard map function to create a list of Pair which can be used by toMap to generate the final map:
val map = friends.map { it.facebookId to it.points }.toMap()
From List to Map with associate function
With Kotlin 1.3, List has a function called associate. associate has the following declaration:
fun <T, K, V> Iterable<T>.associate(transform: (T) -> Pair<K, V>): Map<K, V>
Returns a Map containing key-value pairs provided by transform function applied to elements of the given collection.
Usage:
class Person(val name: String, val id: Int)
fun main() {
val friends = listOf(Person("Sue Helen", 1), Person("JR", 2), Person("Pamela", 3))
val map = friends.associate({ Pair(it.id, it.name) })
//val map = friends.associate({ it.id to it.name }) // also works
println(map) // prints: {1=Sue Helen, 2=JR, 3=Pamela}
}
From List to Map with associateBy function
With Kotlin, List has a function called associateBy. associateBy has the following declaration:
fun <T, K, V> Iterable<T>.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map<K, V>
Returns a Map containing the values provided by valueTransform and indexed by keySelector functions applied to elements of the given collection.
Usage:
class Person(val name: String, val id: Int)
fun main() {
val friends = listOf(Person("Sue Helen", 1), Person("JR", 2), Person("Pamela", 3))
val map = friends.associateBy(keySelector = { person -> person.id }, valueTransform = { person -> person.name })
//val map = friends.associateBy({ it.id }, { it.name }) // also works
println(map) // prints: {1=Sue Helen, 2=JR, 3=Pamela}
}
If you have duplicates in your list that you don't want to lose, you can do this using groupBy.
Otherwise, like everyone else said, use associate/By/With (which in the case of duplicates, I believe, will only return the last value with that key).
An example grouping a list of people by age:
class Person(val name: String, val age: Int)
fun main() {
val people = listOf(Person("Sue Helen", 31), Person("JR", 25), Person("Pamela", 31))
val duplicatesKept = people.groupBy { it.age }
val duplicatesLost = people.associateBy({ it.age }, { it })
println(duplicatesKept)
println(duplicatesLost)
}
Results:
{31=[Person#41629346, Person#4eec7777], 25=[Person#3b07d329]}
{31=Person#4eec7777, 25=Person#3b07d329}
Convert a Iteratable Sequence Elements to a Map in kotlin ,
associate vs associateBy vs associateWith:
*Reference:Kotlin Documentation
1- associate (to set both Keys & Values): Build a map that can set key & value elements :
IterableSequenceElements.associate { newKey to newValue } //Output => Map {newKey : newValue ,...}
If any of two pairs would have the same key the last one gets added to the map.
The returned map preserves the entry iteration order of the original array.
2- associateBy (just set Keys by calculation): Build a map that we can set new Keys, analogous elements will be set for values
IterableSequenceElements.associateBy { newKey } //Result: => Map {newKey : 'Values will be set from analogous IterableSequenceElements' ,...}
3- associateWith (just set Values by calculation): Build a map that we can set new Values, analogous elements will be set for Keys
IterableSequenceElements.associateWith { newValue } //Result => Map { 'Keys will be set from analogous IterableSequenceElements' : newValue , ...}
Example from Kotlin tips :
You can use associate for this task:
val list = listOf("a", "b", "c", "d")
val m: Map<String, Int> = list.associate { it to it.length }
In this example, the strings from list become the keys and their corresponding lengths (as an example) become the values inside the map.
That have changed on the RC version.
I am using val map = list.groupByTo(destinationMap, {it.facebookId}, { it -> it.point })

converting string to vector in Unity

I am trying to convert an array of strings in unityscript with values holding values like:
"Vector3(5, 3, 8)"
into an array of vectors, but Unity will not take these strings as is. Anyone have any ideas?
you cant convert all vector elements together in c# you can do it like bellow:
its psuedoCode:
position.x=convert.ToFloat("3");
position.y=....
i think there is no api to make this for you:
"Vector3(5, 3, 8)"
You didn't give us any sample code, so I'll assume the array is nicely organized, all elements right next to each other.
While I don't think there's any way that you can do this fully using UnityScript, there's ways you can process the literal string with other languages, that is, copying from your Unity script and into another program that will do the change for you.
Here's a small sample of what I would do.
Usage: Create a file array.txt with all the Vector3(x,x,x) strings, separated by line breaks and create another file array.php
Array.txt (sample)
Vector3(5, 1, 3)
Vector3(3, 3, 1)
Vector3(2, 2, 7)
Vector3(6, 6, 4)
Vector3(8, 8, 8)
Vector3(9, 3, 2)
Vector3(1, 2, 1)
Vector3(4, 3, 6)
Vector3(5, 3, 8)
Array.php
<?
$file = file('array.txt');
$text = array();
$text[] = "var vectors = new Array;";
foreach ( $file as $i )
{
$text[] = "vectors.Push(".trim($i).");";
}
echo implode("<br>", $text);
Then just run it under a PHP sandbox or a web server and copy and paste the new array into your script.
use this method to convert a single String value into a vector3 value
public static Vector3 StringToVector3(string sVector)
{
// Remove the parentheses
if (sVector.StartsWith ("(") && sVector.EndsWith (")")) {
sVector = sVector.Substring(1, sVector.Length-2);
}
// split the items
string[] sArray = sVector.Split(',');
// store as a Vector3
Vector3 result = new Vector3(
float.Parse(sArray[0]),
float.Parse(sArray[1]),
float.Parse(sArray[2]));
return result;
}
I figure someone else might find this useful later so:
if(PlayerPrefs.GetFloat("nodeNum") > 0) {
//Assemble axis value arrays
//X
var xString = PlayerPrefs.GetString("xVals");
var xValues = xString.Split(","[0]);
//Y
var yString = PlayerPrefs.GetString("yVals");
var yValues = yString.Split(","[0]);
//Z
var zString = PlayerPrefs.GetString("zVals");
var zValues = zString.Split(","[0]);
var countNode = 0;
var goal = PlayerPrefs.GetFloat("nodeNum");
var nodeVecs = new Array(Vector3.zero);
while (countNode != goal) {
var curVec = Vector3(float.Parse(xValues[countNode]), float.Parse(yValues[countNode]), float.Parse(zValues[countNode]));
nodeVecs.Push(curVec);
countNode += 1;
}
var convNodeVecs : Vector3[] = nodeVecs.ToBuiltin(Vector3) as Vector3[];
for(var nodeVec : Vector3 in convNodeVecs) {
Instantiate(nodeObj, nodeVec, Quaternion.identity);
}
}

Resources