Enums in messages

Is it possible to use enums in messages?



I tried to serialize it but I get

java.lang.RuntimeException: Registration error: no-argument constructor not found on:class mygame.network.messages.helpers.Command


even though I have added a no arg constructor.

enums already implement the serializable interface so you can use them right off the bat. Have a look at TestSerialization.java in the jMETests

@rasmuseneman said:
Is it possible to use enums in messages?

I tried to serialize it but I get
even though I have added a no arg constructor.


Your error has nothing to do with enums. Post your mygame.network.messages.helpers.Command

[java]package mygame.network.messages.helpers;



import com.jme3.network.serializing.Serializable;



/**

*

  • @author Rasmus Eneman <rasmus@eneman.eu>

    */

    @Serializable

    public enum Command {

    START_GAME(true),

    STOP_GAME(true);



    private boolean reliable;



    Command() {

    reliable = true;

    }



    Command(boolean reliable) {

    this.reliable = reliable;

    }



    public boolean isReliable() {

    return reliable;

    }

    }

    [/java]

Oh… an enum with a constructor. Still shouldn’t be an issue though.



The isReliable() thing scares me a little… how are you using this class?

I use it to send multiple commands with the CommandMessage. The is reliable is to define if the Command should be sent over TCP or UDP.

[java]package mygame.network.messages;



import com.jme3.network.AbstractMessage;

import com.jme3.network.serializing.Serializable;

import mygame.network.messages.helpers.Command;



/**

*

  • @author Rasmus Eneman <rasmus@eneman.eu>

    */

    @Serializable

    public class CommandMessage extends AbstractMessage {

    private Command command;



    public CommandMessage() {

    this.setReliable(true);

    }



    public CommandMessage(Command command) {

    this.command = command;

    this.setReliable(command.isReliable());

    }



    public Command getCommand() {

    return command;

    }

    }

    [/java]



    I thought that it were unnecessary to create a lot of message classes that only differs in name and maybe in reliability.



    Right now i only have two commands but there will probably be a bunch more later.

Makes sense. Can you post the full stack trace for the error?

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.RuntimeException: Registration error: no-argument constructor not found on:class mygame.network.messages.helpers.Command
at com.jme3.network.serializing.serializers.FieldSerializer.checkClass(FieldSerializer.java:58)
at com.jme3.network.serializing.serializers.FieldSerializer.initialize(FieldSerializer.java:64)
at com.jme3.network.serializing.Serializer.registerClassForId(Serializer.java:182)
at com.jme3.network.serializing.Serializer.registerClass(Serializer.java:210)
at com.jme3.network.serializing.Serializer.registerClass(Serializer.java:151)
at mygame.network.messages.helpers.RegisterMessages.register(RegisterMessages.java:18)
at mygame.network.MusicriderClient.initialize(MusicriderClient.java:50)
at com.jme3.app.state.AppStateManager.initializePending(AppStateManager.java:219)
at com.jme3.app.state.AppStateManager.update(AppStateManager.java:249)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:238)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:184)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
at java.lang.Thread.run(Thread.java:679)


The register is straight forward
[java]Serializer.registerClass(Command.class);
Serializer.registerClass(CommandMessage.class);[/java]

This should work… you don’t even need to preregister the Command.class (actually, it’s possible that’s the part that’s failing).



Two things:

  1. what version of JME are you running? The latest stable or an older version?
  2. can you try it without explicitly registering the Command.class?
  1. Latest nightly
  2. It worked. Now I get what wezrule actually meant :slight_smile:
@rasmuseneman said:
1. Latest nightly
2. It worked. Now I get what wezrule actually meant :)


It's still a bug that you couldn't register them directly but at least you have a work-around now.