#Opening the Player Inventory Outside the Render Thread.

30 messages · Page 1 of 1 (latest)

arctic gazelle
#

Hey there, right now I'm trying to get my code to open the player's inventory using:

MinecraftClient.getInstance().setScreen(new InventoryScreen(player));

This tells me I can't open the inventory screen outside of the render thread. As explained in this error:

[pool-4-thread-1/ERROR] (fabric-screen-api-v1) Attempted to set screen to "net.minecraft.client.gui.screen.ingame.InventoryScreen@2790f2c1" outside the render thread ("pool-4-thread-1"). This will likely follow a crash! Make sure to call setScreen on the render thread.

How would I go about accessing this thread to tell it to open my inventory and how could I keep the code telling it to do that in its own thread. Right now I have a separate thread asking the inventory to open and it needs to open the inventory at a specific time.

My thoughts are maybe I need to find a method within the code running on the render thread that could schedule the inventory to open on the render frame or I will need to create this method myself through a mixin? What do you think? This is Minecraft 1.19.4 by the way

remote dagger
#

where are you calling your MinecraftClient.getInstance()... piece of code. within what method/event/etc

unkempt basalt
#

Share your code also

arctic gazelle
#

The thread trying to run the setScreen() is created by the onTickStart event hook

remote dagger
#

which onTickStart

arctic gazelle
#

oh client. Is that what you mean?

remote dagger
#

there is a server and client version and also a distinction between world tick and game tick

arctic gazelle
#

client game tick

remote dagger
#

hrm

unkempt basalt
#

Share please code part

remote dagger
#

share your code in any case, but you can try i believe MinecraftClient.getInstance().execute(client -> { client.setScreen(...); });

arctic gazelle
#

There's more going on that this but as a basic example

public class clientTickHandler implements ClientTickEvents.StartTick{

public void onStartTick(MinecraftClient client) {
do some stuff, wait for a condition...
MinecraftClient.getInstance().setScreen(new InventoryScreen(player));
}}

unkempt basalt
#

Why you call client instance when you already have client variable?concern

arctic gazelle
arctic gazelle
#

Oh I think you may be onto something with the execute method in the instance there. Though it isn't working for me just yet. My client isn't finding that setScreen method like you mentioned. I think just need to tweak it a bit.

#

my god...

#

it works

#

MinecraftClient.getInstance().execute(() -> client.setScreen(new InventoryScreen(player)));

#

thats what it took

#

Hats off to you both, I believe I will need to know that execute method for some other bits of my project as well.

#

looks like I can also shorten it to
client.execute(() -> client.setScreen(new InventoryScreen(player)));
just to make you happy Syorito.

unkempt basalt
arctic gazelle
#

woah woah woah, lets not get carried away now.

#

I'll try it

#

no that fails, as without the () -> it isn't a runnable type and only a void

unkempt basalt
#

Interesting and odd

arctic gazelle
#

:peepoShrug: