Modding:WorldGen API: Difference between revisions

From Vintage Story Wiki
Line 219: Line 219:
Hooking into the world gen api is very similar to how we registered our command. We will pass a delegate callback that gets called when a chunk column is generated.
Hooking into the world gen api is very similar to how we registered our command. We will pass a delegate callback that gets called when a chunk column is generated.


Add the following to StartServerSide
Add the following to '''StartServerSide'''
<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
this.api.Event.ChunkColumnGeneration(OnChunkColumnGeneration, EnumWorldGenPass.Vegetation);
this.api.Event.ChunkColumnGeneration(OnChunkColumnGeneration, EnumWorldGenPass.Vegetation);
</syntaxhighlight>
</syntaxhighlight>


The first argument is a delegate, which is a reference to a method. You will get a compiler error after adding that line because we have not yet created the OnChunkColumnGeneration method. We will create that next. The second argument is an Enum that indicates which world generation pass we need to hook into. Vintage story uses several passes to generate the world. Different features are available during different world gen passes. Since we will be placing chests next to trees, we need trees to be in the world so we choose to be notified during the EnumWorldGenPass.Vegetation which tells the engine that we need neighbor chunks, block layers, tall grass, bushes and trees to be available.
The first argument is a delegate, which is a reference to a method. You will get a compiler error after adding that line because we have not yet created the '''OnChunkColumnGeneration''' method. We will create that next. The second argument is an Enum that indicates which world generation pass we need to hook into. Vintage story uses several passes to generate the world. Different features are available during different world gen passes. Since we will be placing chests next to trees, we need trees to be in the world so we choose to be notified during the '''EnumWorldGenPass.Vegetation''' which tells the engine that we need neighbor chunks, block layers, tall grass, bushes and trees to be available.
<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
private void OnChunkColumnGeneration(IServerChunk[] chunks, int chunkX, int chunkZ)
private void OnChunkColumnGeneration(IServerChunk[] chunks, int chunkX, int chunkZ)
Line 230: Line 230:
}
}
</syntaxhighlight>
</syntaxhighlight>
The above code is our empty OnChunkColumnGeneration which will be called when a chunk is generated. Now we need to look at how to place blocks in this method. We have already seen the IBlockAccessor interface and we will be using that to place our blocks but we can't use the implementation we used before. In the next section we will see how to get the IBlockAccessor we need and use it to place our chest.
The above code is our empty '''OnChunkColumnGeneration''' which will be called when a chunk is generated. Now we need to look at how to place blocks in this method. We have already seen the '''IBlockAccessor''' interface and we will be using that to place our blocks but we can't use the implementation we used before. In the next section we will see how to get the '''IBlockAccessor''' we need and use it to place our chest.
 


== Placing blocks during world gen ==
== Placing blocks during world gen ==
256

edits