Nifty callback, how to use in the game?

When I’m scanning a planet I notify the user with a flashing “quad” that last for about 3 seconds.



The goal is to use a callback notifying me when the effect has completed so I can add a button.



Problem is, I have no idea how to achieve that.



Internally it works fine. Meaning, I do this and everything works as it should. What exactly happens is this:

The noteBox appears, flashes for 3 secs the first callback notifies when it’s done and the panel is turned invisible.

But, the returned EndNotify, I have no idea how to use it elsewhere.



[java]

@Override

public EndNotify sendNotification(String notification, final boolean hideAfter) {

nifty.getCurrentScreen().findNiftyControl(“noteBox”, Label.class).setText(notification);

nifty.getCurrentScreen().findElementByName(“notePanel”).show();

nifty.getCurrentScreen().findElementByName(“notePanel”).resetEffects();

if (hideAfter) {

final EndNotify flashEnd = new EndNotify() {



@Override

public void perform() {

nifty.getCurrentScreen().findElementByName(“notePanel”).hide();

}

};

nifty.getCurrentScreen().findElementByName(“notePanel”).startEffect(EffectEventId.onCustom, new EndNotify() {



@Override

public void perform() {

nifty.getCurrentScreen().findElementByName(“noteBox”).startEffect(EffectEventId.onCustom, flashEnd, “fadeOut”);

}

}, “note”);

return flashEnd;

} else {

nifty.getCurrentScreen().findElementByName(“notePanel”).startEffect(EffectEventId.onCustom, null, “note”);

return null;

}

}

[/java]



As the code above shows, I return flashEnd to the caller method (as shown below).

[java]

public void scanSelection() {

noteCallback = asm.getState(ScreensManager.class).sendNotification(“Scanning…”, true);

// for now dump the info to console

asm.getState(GameState.class).scanSelection();

}

[/java]



How do I know when it’s been called? How do I use that callback? Nifty manages those callbacks inside its own code, but how do I use that in my code?



Thanks peeps.

Gah. I think I just realized how to use this properly… Unsure though. I’ll test that idea and hopefully we can forget about this post. (Unlikely)

Never mind, that didn’t work.



What I thought was the solution was to do the following in the calling class:



[java]

// class global field

noteCallback = new EndNotify() {



@Override

public void perform() {

HUD.effectDone();

}

};



public static void effectDone() {

System.out.println(“Effect has completed.”);

}

[/java]



Then use noteCallback when scanning, like so:



[java]

public void scanSelection() {

// for now dump the info to console

asm.getState(ScreensManager.class).sendNotification(“Scanning…”, true, noteCallback);

asm.getState(GameState.class).scanSelection();

}

[/java]



But that static method isn’t called. There’s no output.



Is there a way to do this?

Found how to do it.



Element has a method to use callbacks, so I pass my notifier to hide.



[java]

nifty.getCurrentScreen().findElementByName(“notePanel”).hide(notifier);

[/java]



And it works fine, although the notification happens maybe a second after the effect has really stopped. I guess it’s better than nothing. shrug

1 Like

talking to yourself is the first sign of madness, ah now your name makes sense :slight_smile:

1 Like
@wezrule said:
talking to yourself is the first sign of madness, ah now your name makes sense ^_^

Yeah... Goes with the territory, although I could argue I'm dialoging with myself for the posterity, but I won't. Crazy is crazy, Mad is mad. ;)