Create account / Log in

How to refer to a dynamic property in a conditional

Talk about module design ideas and techniques.

Moderators: uckelman, Tim M

How to refer to a dynamic property in a conditional

Postby mimem » December 26th, 2020, 3:01 am

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! :?
mimem
 
Posts: 9
Joined: July 1st, 2010, 1:21 am

Re: How to refer to a dynamic property in a conditional

Postby jrwatts » December 26th, 2020, 6:17 am

Strings must be enclosed in quotes in Beanshell: you need {CardType=="$CardChoice$"}. ({DeckNumber==$StackNumber$} works because StackNumber is an integer.)
jrwatts
 
Posts: 222
Joined: April 29th, 2020, 10:30 pm

Re: How to refer to a dynamic property in a conditional

Postby barbanera » December 26th, 2020, 9:18 am

mimem wrote: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! :?


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).
barbanera
 
Posts: 467
Joined: January 12th, 2012, 2:27 pm

Re: How to refer to a dynamic property in a conditional

Postby mimem » December 26th, 2020, 4:34 pm

Thank you both.
mimem
 
Posts: 9
Joined: July 1st, 2010, 1:21 am

Re: How to refer to a dynamic property in a conditional

Postby mimem » December 29th, 2020, 4:51 am

barbanera wrote: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).


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.
mimem
 
Posts: 9
Joined: July 1st, 2010, 1:21 am

Re: How to refer to a dynamic property in a conditional

Postby jrwatts » December 29th, 2020, 5:06 am

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).
jrwatts
 
Posts: 222
Joined: April 29th, 2020, 10:30 pm

Re: How to refer to a dynamic property in a conditional

Postby barbanera » December 29th, 2020, 10:40 pm

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.
barbanera
 
Posts: 467
Joined: January 12th, 2012, 2:27 pm

Re: How to refer to a dynamic property in a conditional

Postby Brent Easton » January 1st, 2021, 2:11 am

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.


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
Code: Select all
{CardType=="$CardChoice$"}


Say the CardCoice on the source piece is Artifact, then after the pre-processor stage you will have
Code: Select all
{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
Code: Select all
{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
Code: Select all
{CardType==$CardChoice$}
is that after the preprocessor step, you got this:
Code: Select all
{CardType==Artifact}
which is doing a comparison between the CardType property and property named 'Artifact', which I doubt exists, so returns false.

Regards.
User avatar
Brent Easton
 
Posts: 3307
Joined: December 21st, 2007, 3:06 am
Location: Berry, NSW, Australia

Re: How to refer to a dynamic property in a conditional

Postby barbanera » January 1st, 2021, 10:43 am

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.
barbanera
 
Posts: 467
Joined: January 12th, 2012, 2:27 pm


Return to Module Design

Who is online

Users browsing this forum: No registered users and 4 guests