It is currently 11 Sep 2025, 17:41
   
Text Size

making code wait on Input

Post MTG Forge Related Programming Questions Here

Moderators: timmermac, Blacksmith, KrazyTheFox, Agetian, friarsol, CCGHQ Admins

making code wait on Input

Postby slapshot5 » 04 Jul 2011, 14:25

Hi all,

I know I've run across this before, but don't remember if I solved it.

Basically, I have:

Code: Select all
public void resolve() {
player.discard();
player.drawCard();
}
What needs to happen is the discard happens, then the draw.

What actually happens is showMessage() for discard, card is drawn, now I can Input my discard (and discard the card that was drawn, which is incorrect).

Do we have a way to do this correctly?

Thanks,
slapshot5
slapshot5
Programmer
 
Posts: 1391
Joined: 03 Jan 2010, 17:47
Location: Mac OS X
Has thanked: 25 times
Been thanked: 68 times

Re: making code wait on Input

Postby friarsol » 04 Jul 2011, 14:37

I don't believe this exists currently. It would be great if it did, because there are definitely some other timing issues related to anything that resolves with a Custom Input.
friarsol
Global Moderator
 
Posts: 7593
Joined: 15 May 2010, 04:20
Has thanked: 243 times
Been thanked: 965 times

Re: making code wait on Input

Postby Hellfish » 04 Jul 2011, 15:28

As far as I understand the Input system, there is no way to do this save for modifying the Input base class to contain a Command object that can be run when the Input's stop() method is called.

FAKE EDIT: I don't think you could mod InputControl to do such a thing, but it's possible.
So now you're
Screaming for the blood of the cookie monster
Evil puppet demon of obesity
Time to change the tune of his fearful ballad
C is for "Lettuce," that's good enough for me
User avatar
Hellfish
Programmer
 
Posts: 1297
Joined: 07 Jun 2009, 10:41
Location: South of the Pumphouse
Has thanked: 110 times
Been thanked: 169 times

Re: making code wait on Input

Postby Braids » 07 Jul 2011, 14:35

slapshot5 wrote:. . . What needs to happen is the discard happens, then the draw . . . Do we have a way to do this correctly?
my understanding of the code is shaky, but maybe you could consider one of these?
  • extend the Input for discarding a card so that, during its custom resolution method {selectCard i think}, call super's version of selectCard, then draw the card.
  • use the MagicStack. push a draw action/trigger/whatever, then immediately push the discard action. this is fairly elegant, but it violates atomicity/inseverability/indivisibility of the card's resolution. either player could respond after the discard, before the draw. i don't think that's supposed to happen.
  • i seem to recall some references to an Input stack. this is an Input stack, not a MagicStack.
    does it work? if it is supported, perhaps you could push a dummy Input for drawing a card that calls its own selectX method from showMessage, then push the one for discarding. then they resolve in reverse order.

edit 1: fixed list tag.
"That is the dumbest thing I've ever seen." --Rob Cashwalker, regarding Innistrad double-sided cards. One of the first times he and I have ever agreed on something. ;)
User avatar
Braids
Programmer
 
Posts: 556
Joined: 22 Jun 2011, 00:39
Location: Unknown. Hobby: Driving myself and others to constructive madness.
Has thanked: 1 time
Been thanked: 1 time

Pipe dreaming Re: making code wait on Input

Postby Braids » 07 Jul 2011, 14:41

slapshot5 wrote:
Code: Select all
public void resolve() {
player.discard();
player.drawCard();
}
Code: Select all
public void resolve() {
    final Player finalPlayer = player;
    Thunk<Null> drawThunk = new Thunk<Null> {
        public Null apply() { finalPlayer.drawCard(); return null; }
    };
    player.discardThen(drawThunk);
}
a thunk is a function that takes no arguments. it's like a simplified lambda. you can pass the thunk around without applying/invoking it to defer execution. too ugly? too strange?

there are thunks and lambdas on the minimax branch. :-)

edit 1: fixed bugs in code #-o
edit 2: fixed typo
edit 3: improved indentation
Last edited by Braids on 07 Jul 2011, 14:47, edited 1 time in total.
"That is the dumbest thing I've ever seen." --Rob Cashwalker, regarding Innistrad double-sided cards. One of the first times he and I have ever agreed on something. ;)
User avatar
Braids
Programmer
 
Posts: 556
Joined: 22 Jun 2011, 00:39
Location: Unknown. Hobby: Driving myself and others to constructive madness.
Has thanked: 1 time
Been thanked: 1 time

Re: making code wait on Input

Postby Hellfish » 07 Jul 2011, 14:46

So it's like a Command object with a return type? :P
So now you're
Screaming for the blood of the cookie monster
Evil puppet demon of obesity
Time to change the tune of his fearful ballad
C is for "Lettuce," that's good enough for me
User avatar
Hellfish
Programmer
 
Posts: 1297
Joined: 07 Jun 2009, 10:41
Location: South of the Pumphouse
Has thanked: 110 times
Been thanked: 169 times

Re: making code wait on Input

Postby Braids » 07 Jul 2011, 14:48

yes. yes, it is. Null is a special class that cannot be instantiated by anyone ever, so about the only value it can take on is an actual {null}.
"That is the dumbest thing I've ever seen." --Rob Cashwalker, regarding Innistrad double-sided cards. One of the first times he and I have ever agreed on something. ;)
User avatar
Braids
Programmer
 
Posts: 556
Joined: 22 Jun 2011, 00:39
Location: Unknown. Hobby: Driving myself and others to constructive madness.
Has thanked: 1 time
Been thanked: 1 time

Re: making code wait on Input

Postby Rob Cashwalker » 08 Jul 2011, 11:52

a thunk is a function that takes no arguments.
What, did Dr. Suess have a hand in designing Java?
The Force will be with you, Always.
User avatar
Rob Cashwalker
Programmer
 
Posts: 2167
Joined: 09 Sep 2008, 15:09
Location: New York
Has thanked: 5 times
Been thanked: 40 times

Re: making code wait on Input

Postby Braids » 08 Jul 2011, 13:45

Rob Cashwalker wrote:
a thunk is a function that takes no arguments.
What, did Dr. Suess have a hand in designing Java?
.
.
if there's a funk in your thunk that's passed by reference,
and your lesser professor is demanding more deference,
then junk your thunk for a grander scheme;
and pass lambda on as an internet meme.


from http://dictionary.reference.com/browse/thunk under "Computing Dictionary":
In fact, according to the inventors, it was coined after they realised (in the wee hours after hours of discussion) that the type of an argument in ALGOL 60 could be figured out in advance with a little compile-time thought, simplifying the evaluation machinery. In other words, it had "already been thought of"; thus it was christened a "thunk", which is "the past tense of "think" at two in the morning".
"That is the dumbest thing I've ever seen." --Rob Cashwalker, regarding Innistrad double-sided cards. One of the first times he and I have ever agreed on something. ;)
User avatar
Braids
Programmer
 
Posts: 556
Joined: 22 Jun 2011, 00:39
Location: Unknown. Hobby: Driving myself and others to constructive madness.
Has thanked: 1 time
Been thanked: 1 time

Re: making code wait on Input

Postby friarsol » 09 Jul 2011, 15:00

Did you get anywhere with this Slap? Looks like here's another issue we have with it: http://code.google.com/p/cardforge/issu ... 1310223583
friarsol
Global Moderator
 
Posts: 7593
Joined: 15 May 2010, 04:20
Has thanked: 243 times
Been thanked: 965 times

Re: making code wait on Input

Postby slapshot5 » 09 Jul 2011, 15:53

friarsol wrote:Did you get anywhere with this Slap? Looks like here's another issue we have with it: http://code.google.com/p/cardforge/issu ... 1310223583
I tried a post-input command implementation, but the Input object I wanted didn't exist when I expected.

I ended up just coding around it for the card I wanted to add: Chains of Mephistopheles

-slapshot5
slapshot5
Programmer
 
Posts: 1391
Joined: 03 Jan 2010, 17:47
Location: Mac OS X
Has thanked: 25 times
Been thanked: 68 times


Return to Developer's Corner

Who is online

Users browsing this forum: No registered users and 33 guests

Main Menu

User Menu

Our Partners


Who is online

In total there are 33 users online :: 0 registered, 0 hidden and 33 guests (based on users active over the past 10 minutes)
Most users ever online was 7967 on 09 Sep 2025, 23:08

Users browsing this forum: No registered users and 33 guests

Login Form