Modding:WorldGen API: Difference between revisions

From Vintage Story Wiki
no edit summary
No edit summary
Line 232: Line 232:
</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 ==
The server does not generate chunks on the main game thread because the game's frame rate would drop significantly and it would really be disruptive to game play. The server spawns a separate thread for this and there is a special IBlockAccessor that is used in that thread that we need to get a reference to in order to place our chest. First lets add two variables at the top of our class of type IBlockAccessor. One to hold our game thread IBlockAccessor and one to hold the one used by the server world gen thread.
<syntaxhighlight lang="c#">
private IBlockAccessor chunkGenBlockAccessor;
private IBlockAccessor worldBlockAccessor;
</syntaxhighlight>
Let's initialize worldBlockAccessor at the top of StartServerSide. This is the IBlockAccessor we used in our /treasure command. Later we will refactor that code to use this variable.
<syntaxhighlight lang="c#">
this.worldBlockAccessor = api.World.BlockAccessor;
</syntaxhighlight>
<syntaxhighlight lang="c#">
this.api.Event.GetWorldgenBlockAccessor(OnWorldGenBlockAccessor);
</syntaxhighlight>
Then add this method:


== Finding where to place the chest ==
== Finding where to place the chest ==
256

edits