LWJGL Multiplayer Game Threads

Started by DarkerMinecraft, April 01, 2019, 23:35:27

Previous topic - Next topic

DarkerMinecraft

I am trying to create a Multiplayer game but when I use different threads for the server and client information I am getting this error
FATAL ERROR in native method: Thread[Thread-1,5,main]: No context is current or a function that is not available in the current context was called. The JVM will abort execution.
	at org.lwjgl.opengl.GL30C.nglGenVertexArrays(Native Method)
	at org.lwjgl.opengl.GL30C.glGenVertexArrays(GL30C.java:2420)
	at org.lwjgl.opengl.GL30.glGenVertexArrays(GL30.java:2369)
	at com.widowmc.opengl.Vao.createVAO(Vao.java:19)
	at com.widowmc.opengl.Vao.<init>(Vao.java:15)
	at com.widowmc.graphics.Loader.loadToVAO(Loader.java:37)
	at com.widowmc.toolbox.Utils.modelDataToRawModel(Utils.java:14)
	at com.widowmc.obj.OBJFileLoader.loadOBJ(OBJFileLoader.java:74)
	at com.widowmc.entities.Player.<clinit>(Player.java:20)
	at sun.misc.Unsafe.ensureClassInitialized(Native Method)
	at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(Unknown Source)
	at sun.reflect.ReflectionFactory.newFieldAccessor(Unknown Source)
	at java.lang.reflect.Field.acquireFieldAccessor(Unknown Source)
	at java.lang.reflect.Field.getFieldAccessor(Unknown Source)
	at java.lang.reflect.Field.getLong(Unknown Source)
	at java.io.ObjectStreamClass.getDeclaredSUID(Unknown Source)
	at java.io.ObjectStreamClass.access$700(Unknown Source)
	at java.io.ObjectStreamClass$3.run(Unknown Source)
	at java.io.ObjectStreamClass$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.io.ObjectStreamClass.<init>(Unknown Source)
	at java.io.ObjectStreamClass.lookup(Unknown Source)
	at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
	at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
	at java.io.ObjectInputStream.readClassDesc(Unknown Source)
	at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
	at java.io.ObjectInputStream.readObject0(Unknown Source)
	at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
	at java.io.ObjectInputStream.readSerialData(Unknown Source)
	at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
	at java.io.ObjectInputStream.readObject0(Unknown Source)
	at java.io.ObjectInputStream.readObject(Unknown Source)
	at com.widowmc.multiplayer.Client.run(Client.java:61)
	at java.lang.Thread.run(Unknown Source)


I got this error when I added this part of code
if(obj instanceof SendPlayerInformationPacket) {
			SendPlayerInformationPacket packet = (SendPlayerInformationPacket) obj;
			Player player = new Player(new Vector3f(packet.x, packet.y, packet.z), packet.info);
			player.addComponet(new PlayerMoveComponent());
			player.addComponet(new LevelingComponent());
			CLIENT_PLAYER = player;
		}


The player class use OpenGL stuff in the constructor so I can't really change it so how can I fix this problem?

KaiHH

Only one thread can have the same OpenGL context current at any given time.
According to the stacktrace, the Player class contains a static initializer block (Player.<clinit>) which calls OpenGL methods (very bad idea, btw.).

QuoteThe player class use OpenGL stuff in the constructor so I can't really change it
What do you mean by "I can't really change it". If you do not own/control the source code of the Player class, then you must take care of where you instantiate the Player the first time or call any static methods the first time (i.e. this is when the static initializer block is executed for the first time).