Jump to content

How can I synchronize a script with the game's tickrate?


Coron

Recommended Posts

I'm working on a forge mod in which it is possible to compile and run (Java) scripts at runtime. It is possible to manipulate a robot, for example by making it walk to a set of coordinates or mine in a direction.

The script runs on a separate thread from the game, but this causes some occasional crashes since the script may indirectly modify collections while they are being iterated.

The solution to this, I think, would be that every command executed by the script would have to wait for the next game tick to execute. I know whenever the game ticks, because the robot's onEntityUpdate() is called. I'm new to Java and I've looked into synchronization, but I'm not sure what an actual implementation would look like, so I would greatly appreciate some help!

Link to comment
Share on other sites

11 hours ago, diesieben07 said:

It comes down to this: You cannot have the script run in a separate thread. You need to have the script listen for hooks like "every tick" or "every zombie tick", etc.

 

Also, using Java for this is pretty cumbersome, since compiling stuff at runtime is tricky and not possible unless the user has the JDK installed (pretty unlikely if they aren't a developer). I suggest you use something like JavaScript, Java ships a runtime for it called Nashorn (or Rhino pre Java 8).

I've managed to get it working as a temporary solution for the script to wait for the tick and then make the tick wait for the current command to execute. Obviously not a good long term solution, but fine at the moment.

 

That's a good point, but I want to use Java (or bytecode) as I will be implementing a compiler that either compiles to Java or bytecode.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.