Custom Button

Hello,

I created a custom button associating it to a Java class.

For doing that, I followed the programming tutorials.

When I associate the Java class to the button in the editor, then Java crashes and gives an error.

The error is a ClassCastException and it is generated by the following piece of code (copied directly from the turorial):

public void addTo(Buildable parent)
{
	GameModule mod = (GameModule)parent;

very strange, because that means that the parent is not a GameModule.

Do you have any suggestions?

Thus spake cianchet:

Hello,

I created a custom button associating it to a Java class.

For doing that, I followed the programming tutorials.

When I associate the Java class to the button in the editor, then Java
crashes and gives an error.

The error is a ClassCastException and it is generated by the following
piece of code (copied directly from the turorial):

public void addTo(Buildable parent)
{
GameModule mod = (GameModule)parent;

very strange, because that means that the parent is not a GameModule.

Do you have any suggestions?

If you need the GameModule, use the static method in that class to
get it. The parent here could be any number of things—the cast
you’re doing is not safe.


J.

Very good.

That worked.

Thank you for the quick reply.

Just for the documentation, the change I did is:

public void addTo(Buildable parent)
{
	GameModule mod = GameModule.getGameModule();

The error is a ClassCastException and it is generated by the following
piece of code (copied directly from the turorial):

   public void addTo(Buildable parent)
   {
           GameModule mod = (GameModule)parent;

very strange, because that means that the parent is not a GameModule.

It hardly ever is! The parent is probably a Map or something else
entirely. All you know about parent is that it’s a Buildable.

To get the GameModule do GameModule.getGameModule().

  • M.