cocos2d-x : setCatgoryBitmask not working - cocos2d-x

For some reason the function setBatgoryBitmask() of a PhysicsBody in cocos2d-x has no effect :
OutputDebugStringA("Wall cat bitmask : ");
OutputDebugStringA(std::to_string(body->getCategoryBitmask()).c_str());
OutputDebugStringA("\n");
body->setCategoryBitmask(2);
OutputDebugStringA("Wall cat bitmask : ");
OutputDebugStringA(std::to_string(body->getCategoryBitmask()).c_str());
OutputDebugStringA("\n");
body->addShape(PhysicsShapeEdgeBox::create(Size(dest - or ), mat, borderW));
OutputDebugStringA("Wall cat bitmask : ");
OutputDebugStringA(std::to_string(body->getCategoryBitmask()).c_str());
OutputDebugStringA("\n");
Result :
Wall cat bitmask : -1
Wall cat bitmask : -1
Wall cat bitmask : -1
As a result I cannot define what collide with what. Both setCategoryBitmask and getCategoryBitmask seems to be simplet setter and getter however so I have no idea what is happening here.

Ok that is stupid. Seems that you cannot set category or collision bitmask if your body has no shape yet. Setting those after adding the first shape seems to resolve the problem.

Related

Use jq to remove top level module while retaining values

I'm trying to use jq in order to achieve the following -
With an input of:
{
"SomeValue": {
"x" : "y",
"a" : "b"
}
}
I'd like to be able to remove 'SomeValue' and return just the key/values below so that my output would look like:
{
"x" : "y",
"a" : "b"
}
I've tried various permutations of commands I've seen on the forum but either end up deleting the whole structure or nothing at all - thanks in advance for any help/pointers in the right direction.
If all you want is the content of .SomeValue, that's already exactly your filter:
jq '.SomeValue'
If this part is nested deeper, and you want to update that part while keeping the rest, use the update operator |= with .SomeValue on that context (with your small sample this is still just .):
jq '. |= SomeValue'

How can I count objects in jq (not in an array)

I have some json input that comes as a list of objects (not as a json array) like, for which I want to count the number of active, and the number of inactive:
{
"key" : "state",
"value" : "active"
}
{
"key" : "state",
"value" : "active"
}
{
"key" : "state",
"value" : "active"
}
{
"key" : "state",
"value" : "inactive"
}
I want to transform using only JQ (in fact it will be handled by jackson-jq in java code, so I can not use some shell tricks).
I tried many things, like select(.value == "active") | length to get the number of active, but it is always handle object per object.
Even when using reduce, it is always handled object per object.
Example of output is :
2
2
2
(I have 3 objects with 2 fields that match value="active)
The output that I expect is :
3
Here is a playground with my example.
To avoid "slurping" the input, you would use either reduce or foreach. Here's a suitable abstraction built from reduce:
def count(s; f; g):
reduce s as $s ([0,0];
if $s|f then .[0]+=1 else . end
| if $s|g then .[1]+=1 else . end);
With this, you would invoke jq with the -n command-line option and run a query such as:
count(inputs|.value; .=="active"; .=="inactive")
Notice that one cannot use count/1 (defined analogously) twice since inputs consumes the input stream.

Python/JSON : I want to go to a specific thing when it can't find it in a json file

I'm actually making my discord bot and I wanted to make a command to know plane informations. I made it but know i want to put plane photos depending on the company.
I tried to do that:
if plane_data["data"][f"{flight_companie}"] == "FedEx" or "Ryanair" or "SWISS" or "Air France" or "SWISS" or "British Airways":
plane_photo = plane_data["data"][flight_companie]
else:
plane_photo = plane_data["data"]["unknown"]
Unknown is equal to a url photo that says there is no plane photos.
When i try with UPS it gives me that out:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'UPS Airlines'
Please help me out!!
if plane_data["data"][f"{flight_companie}"] == "FedEx" or "Ryanair"...
will always evaluate to True. This is because a string like "Ryanair" is truthy. Meaning it becomes true when converted to a boolean. So your line is equivialent to
if plane_data["data"][f"{flight_companie}"] == "FedEx" or True or True or True ...
or can not be used in this situation. I would use
if plane_data["data"][f"{flight_companie}"] in ["FedEx","Ryanair","SWISS","Air France","SWISS","British Airways"]:
instead.

Evaluate a string term in prolog

I'm trying to create a prolog program which receives the queries to run as strings (via json) and then print the result (succeed or fails).
:- use_module(library(http/json)).
happy(alice).
happy(albert).
with_albert(alice).
does_alice_dance :- happy(alice),with_albert(alice),
format('When alice is happy and with albert, she dances ~n').
with_alice(albert).
does_albert_dance :- happy(albert),with_alice(albert),
format('When albert is happy and with alice, he dances ~n').
fever(martin).
low_appetite(martin).
sick(X):-fever(X),low_appetite(X).
main(json(Request)) :-
nl,
write(Request),
nl,
member(facts=Facts, Request),
format('Facts : ~w ~n',[Facts]),
atomic_list_concat(Facts, ', ', Atom),
format('Atom : ~w ~n',[Atom]),
atom_to_term(Atom,Term,Bindings),
format('Term : ~w ~n',Term),
write(Bindings).
After executing this query :
main(json([facts=['sick(martin)', 'does_alice_dance',
'does_albert_dance']])).
i had :
[facts=[sick(martin), does_alice_dance, does_albert_dance]]
Facts : [sick(martin),does_alice_dance,does_albert_dance]
Atom : sick(martin), does_alice_dance, does_albert_dance
Term : sick(martin),does_alice_dance,does_albert_dance
[]
true
What i would like to do is to evaluate Term. I tried to make it work using the is/2 and the call predicates but it doesn't seem to work.
using
call(Term)
(i added in the tail of the main), i had this error :
Sandbox restriction!
Could not derive which predicate may be called from
call(C)
main(json([facts=['sick(martin)',does_alice_dance,does_albert_dance]]))
using
Result is Term
(Result is a variable i added to store the result), i had this error :
Arithmetic: `does_albert_dance/0' is not a function
Is there ay solution to evaluate strings expressions in prolog please ?
As #David Tonhofer said in the first comment, The issue was that i'm testing my code on an online editor (which restrict some prolog features like the invocation of the call predicate). So after adding the call predicate to the tail of my program :
main(json(Request)) :-
nl,
write(Request),
nl,
member(facts=Facts, Request),
format('Facts : ~w ~n',[Facts]),
atomic_list_concat(Facts, ', ', Atom),
format('Atom : ~w ~n',[Atom]),
atom_to_term(Atom,Term,Bindings),
format('Term : ~w ~n',Term),
write(Bindings),
call(Term).
and testing it on my local machine. It works fine.

JSONPath regular expression - NOT starting with

My JSON (simplified) looks like this:
[
{"name" : "foobar",
"id" : 123
},
{"name" : "bar",
"id" : 123
},
{"name" : "foobar",
"id" : 456
}, ...
]
I'm using https://jsonpath.herokuapp.com/ to try and find the right JSONPATH syntax to filter out anything not starting with foo, and having id == 123.
Getting it to filter the ones that do start with foo is easy:
$..[?(#.name =~ /foo.*/i)]
This yields the following results:
[
{
"name" : "foobar",
"id" : 123
},
{
"name" : "foobar",
"id" : 456
}
]
I can get rid of the id 456 by adding an additional filter like so:
$..[?(#.name =~ /foo.*/i && #.id==123)]
But how do I do the opposite of getting the name starting with foo? I want all entities that do not start with foo.
I tried something like this:
$..[?(!#.name =~ /foo.*/i && #.id==123)]
Which at least parses as valid JSONPATH, and should negate the filter, but for some reason it still happily only reports the foobar entry:
[
{
"name" : "foobar",
"id" : 123
}
]
How can I achieve a NOT LIKE in JSONPATH?
Thanks!
Regex to identify data not starting with a given string foo:
^([^f]|f[^o]|fo[^o])
If your regex engine supports negative lookahead, that reduces to
^(?!foo)
Note the starting anchor (^) that limits the permissible matching location to the start of the test string.
Your attempt $..[?(!#.name =~ /foo.*/i && #.id==123)] is almost correct. Surround the regex condition with parenthesis before negating with ! like so $..[?(!(#.name =~ /foo.*/i) && #.id==123)]. Tested at https://jsonpath.herokuapp.com/
Edit: This was assuming that you were using Jayway's jsonpath (Java, https://github.com/json-path/JsonPath), but from the documentation link you provided for SmartBear, it looks like it uses the Goessner jsonpath (Javascript, https://goessner.net/articles/JsonPath/). Both, for whatever reason use slightly differing syntaxes.
Thanks to #collapsar for nudging me in the correct direction, in that the key to solving it was in the regular expression (but specifically using the JavaScript Regular Expression syntax, and merging that with the JSONPath syntax).
What actually ended up doing the trick was reading the documentation for JASONPath a bit more careful. It states:
=~
Match a JavaScript regular expression. For example, [?(#.description =~ /cat.*/i)] matches items whose description starts with cat (case-insensitive).
Note: Not supported at locations that use Ready! API 1.1.
The link to Javascript Regular Expression in turn contains the following:
[^xyz]
A negated or complemented character set. That is, it matches anything that is not enclosed in the brackets. You can specify a range of characters by using a hyphen. Everything that works in the normal character set also works here.
For example, [^abc] is the same as [^a-c]. They initially match 'r' in "brisket" and 'h' in "chop."
The resulting expression that works is:
$..[?(#.name =~ /[^foo].*/ && #.id == 123)]
Result:
[
{
"name" : "bar",
"id" : 123
}
]
(I added an additional bar with id 456 to the JSON payload I had, to double-check the filter also worked).