Jan5676 Posted October 13, 2025 Report Posted October 13, 2025 Hi, basically my question is, I want to start a random timer when loading the world inside of a Block Entities Initialize function. When it gets loaded however, I noticed that because Client and Server call them independently of one another that their values will be different. Is there a way to ensure they will both be passed the same random value so they can act in sync?
Jan5676 Posted October 13, 2025 Author Report Posted October 13, 2025 Okay I was able to answer this question myself in case anyone needs this for the future. It's pretty simple actually, just take the Block Entities position in the world, convert it to a Hashcode and feed it as a seed to the RNG. This will however always produce the same seed you load up the world so keep that in mind. If someone has an idea on how to make them completely random so they change their values on each world load please feel free to share! 2
Diff Posted October 13, 2025 Report Posted October 13, 2025 1 hour ago, Jan5676 said: Okay I was able to answer this question myself in case anyone needs this for the future. It's pretty simple actually, just take the Block Entities position in the world, convert it to a Hashcode and feed it as a seed to the RNG. This will however always produce the same seed you load up the world so keep that in mind. If someone has an idea on how to make them completely random so they change their values on each world load please feel free to share! How about the current in-game time through ICoreAPI.World.Calendar.ElapsedSeconds? It's monotonic across world loads and seems like it should match between server and client.
Jan5676 Posted October 13, 2025 Author Report Posted October 13, 2025 That sounds pretty good. I just remembered it's not just world loads/Server joins but also required each time a chunk with the Entity is reloaded, but I think that solution should cover it. Will the ElapsedSeconds always be in sync between server in client though? Because I can imagine even a lag of a few millis might generate a different seed.
Diff Posted October 13, 2025 Report Posted October 13, 2025 I'm not sure. If it is a problem, there's a good handful of things you could do to mitigate it. 1. Round the ElapsedSeconds to the nearest hundred or thousand seconds to shrink the possible problem window. 2. Use coherent noise instead of random noise. That way a slight difference in input will only result in a slight difference in output. 3. Use ElapsedHours or ElapsedDays, possibly combining with either #1 or #2 since these are doubles rather than longs. All those just reduce the problem than actually solving it though. It's more complex, but for a full solution maybe you could register a new network channel for your mod? Have the server generate a random seed on startup, and have the clients all ask for it when they start up.
Recommended Posts