Jump to content

Mod doesn't work with caves? Why..!?


Recommended Posts

So you make your first mod, and it works just fine. However, you find out soon, either by yourself or from other users, that your mod is completely broken and ceases to function when you're playing with caves. Either it crashes, makes players have weird stats, messes up the crafting recipes, shows the sandstorm effect, or just isn't doing what you intended. But why? It just worked fine without caves, so what's wrong?

The truth is, if your mod is having those issues, then your mod doesn't work at all. It's written incorrectly, and it only worked for you because you've tested it under very specific circumstances. I've seen this problem in countless mods from the workshop, usually from new modders, but sometimes I've even seen this in the Top 3 mods of the week.

So, what's the issue? The issue is that you wrote a mod that only works on the server. That means it only works for the server and only the server. If other players joined that server, then the mod won't work for them. Those players are clients. To simplify the difference between servers and clients: Servers host the game, and clients play the game.

The server and the client don't know what each other knows, so they need to communicate. Information available for the server is not immediately available to the client, and vice versa. For example, the server has access to any entity's components, such as 'health', but the client doesn't. That's why players can't know how much health mobs exactly have, because that information is inaccessible for them.

If your mod is working only if you're hosting a world without caves, then that's because hosting a caveless world works differently from hosting a world with caves. When you host a world with caves enabled, then you're playing on that world as a client. However, when you host a world with caves disabled, then you're playing on that world as the server itself. That is why I highly recommend testing your mods in a world with caves as it allows you to know if it works for clients.

Do you notice how a mod can either be a Client_only_mod, a Server_only_mod, or an All_clients_require_mod? A client-only mod is a mod that is supposed have client-sided code, such as Minimap HUD or Geometric Placement, and you can see them categorised as 'Client Mods' in the game. Server-only and all-clients-require mods are both categorised as 'Server Mods' in the game, but there's a difference between the two. Server-only mods are supposed to only have server-sided code, but all-clients-require mods can have server-sided and client-sided code.

Server-sided code is different from client-sided code because, again, servers and clients don't know what each other knows. Clients don't have access to an entity's components that the server handles, and the server doesn't have access to a client's HUD elements. So how do you separate server-sided code from client-sided code?

You can use TheNet:GetIsMasterSimulation() or TheWorld.ismastersim for this purpose:

--'Server and client code here'

if not TheNet:GetIsMasterSimulation() then
	--'Client-sided code here'
end

if TheNet:GetIsMasterSimulation() then
	--'Server-sided code here'
end

There's more to this, I'm mainly trying to explain basically why a lot of mods have the 'can't work with caves' issue and how you can fix that issue for your mods. You can look up guides on how to do things like having your mod make the server and the client communicate with each other using netvars and RPCs. And again, I really recommended testing your mods in a world with caves enabled to see if they work.

Note that I'm not an expert or anything, just my attempt at helping. If you see mistakes or misinformation, please tell me below.

Edited by JohnWatson
  • Like 1
Link to comment
Share on other sites

Wow that's actually problem that i got 3 hours ago when i tested my mod. Could You please write more about "how to recognize which one is server-sided code and client-sided code?" :wilson_sneaky:

Edited by Weexer
I solved my problem already :) nevermind it
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...