How to refer to a dynamic property in a conditional

I have a Global Key Command that requests cards to be drawn from a number of different decks.

Each card as two markers, a DeckNumber (value 1 through 10) and a CardType (value “Artifact” or “City”.)

The piece with the GKC has dynamic properties to match these, namely numerical StackNumber and text-valued CardChoice.

If, as a Matching Property in the GKC, I use {DeckNumber==$StackNumber$}, this works fine, and only cards from the desired deck are chosen.

I cannot seem to get this to happen for the CardType. I would have thought {CardType==$CardChoice$} works, but this always evaluates as false, even if CardChoice has value “Artifact”. The condition {CardType==“Artifact”} works; why can’t I refer to it via a property?

I hope I am missing something obvious! :confused:

Strings must be enclosed in quotes in Beanshell: you need {CardType=="$CardChoice$"}. ({DeckNumber==$StackNumber$} works because StackNumber is an integer.)

Beware that GKCs are odd in the sense that they behave differently if you use $variables$ in new style Beanshell expressions vs old style expressions.

With old style, $CardChoice$ is substituted right away, parsing the value in the source piece, before actually running the GKC.

With new style, $CardChoice$ or “$CardChoice” or just CardChoice is checked at target level, instead.

Your {CardType==$CardChoice$} (with or without quotes, or without the $$) is basically looking for pieces where there are two variables, CardType and CardChoice, with identical values. Which is probably 95% of the time not what one was really intending to achieve.

Try simply: CardType = $CardChoice$ (without the {} and with just one = symbol).

Thank you both.

The simple version you give doesn’t work in my module, though I certainly don’t know enough to say why. The only version that seems to scan is the Beanshell expression {CardType=="$CardChoice$"}, with the quotes.

I believe barbanera was mistaken in his description of how variable substitution works in GKCs: within braces (BeanShell expressions), variable names enclosed in $ are substituted based on the calling piece, while unquoted variable names will be substituted based on the called piece. So, {CardType=="$CardChoice$"} is checking CardType on every other piece against CardChoice on the piece with the GKC (which is what you want, presumably).

Wait, admitedly I was thinking about {CardType==$CardChoice$} which, as an alternative to the old style expression CardType=$CardChoice$, does NOT work and I thought this might be the cause of the OP’s issue.

I stand corrected and good to know that {CardType=="$CardChoice$"} will instead work.

On the other hand, I don’t understand why CardType=$CardChoice$ (with NO curly brakets) does not work for the OP. Does it throw errors or just does not work as expected? I have been using old style expressions like that in my GKCs for years and they always worked.

No, this is incorrect.

$$ properties like $CardChoice$ are ALWAYS, IN ALL CASES evaluated and replaced directly with the value of the property from the piece that holds the expression. This happens as a pre-processor step prior to the execution of the expression.

The difference with GKC’s happens at the second step, where the property expression is actually evaluated. For a GKC, the evaluation of the expression resulting from the pre-processor stage is performed on the target piece. For other than GKC’s, the evaluation of the expression is done on the source piece.

The correct, modern form of the expression to use in a GKC will normally be {CardType=="$CardChoice$"}

Say the CardCoice on the source piece is Artifact, then after the pre-processor stage you will have {CardType=="Artifact"} which is the correct expression.

If the expression is not a GKC expression (say a Trigger Action), then the the correct modern form to use will usually just be {CardType==CardChoice} There is no pre-processor step and the evaluation step is a direct comparison between the CardType and CardChoice variables.

What happened when you tried to use {CardType==$CardChoice$} is that after the preprocessor step, you got this:

{CardType==Artifact} which is doing a comparison between the CardType property and property named ‘Artifact’, which I doubt exists, so returns false.


Thanks for the clarification, that makes much more sense of what I said.

However, you forgot to comment on the old-style expression CardType=$CardChoice$. Can you confirm it should also work, as I said it should? Similar examples work for me. I am not sure why it wouldn’t work for the OP. Perhaps he wrapped it in curly brackets, which of course would fail to compile.