TerrainStreamer
Package: Libraries.Game.Graphics.Worlds · Inherits from: Libraries.Language.Object
TerrainStreamer keeps only the regions near a target, usually the player, loaded in a TerrainData, so a world can be far larger than what fits in memory. The whole world is saved in a folder, one region to a file, and regions are loaded from it as the target comes near and unloaded as it moves away. A region that has changed is saved before it is unloaded, so the change is there when it is loaded again. Regions within the load radius of the target are loaded, nearest first, a few each update so no single frame does too much. Quorum has no background threads, so spreading the work over frames is the way to keep the game smooth. Regions are only unloaded once they are past the unload radius, which is a little larger, so a region near the edge isn't loaded and unloaded over and over as the target moves back and forth. Distances are measured from the target to the nearest point of a region's square, in world units. Terrain3D does not stream regions yet; its documentation says it is planned. This class follows its one-file-per-region layout.
Example Code
use Libraries.Game.Graphics.Worlds.TerrainData
use Libraries.Game.Graphics.Worlds.TerrainStreamer
use Libraries.System.File
TerrainData data
File folder
folder:SetPath("SavedTerrain")
data:LoadSettings(folder)
TerrainStreamer streamer
streamer:SetTerrainData(data)
streamer:SetFolder(folder)
streamer:SetLoadRadius(100)
streamer:LoadAllNear(0, 0)
Actions
GetDistanceToRegion(integer locationX, integer locationZ, number worldX, number worldZ)
This action returns how far a world position is from the nearest point of a region's square. It is 0 inside the square.
Parameters
locationXinteger: The region location along x.locationZinteger: The region location along z.worldXnumber: The position along x, in world units.worldZnumber: The position along z, in world units.
Returns
number: The distance, in world units.
GetFolder()
This action returns the folder the world is saved in.
Returns
Libraries.System.File: The folder.
GetLastLoadedCount()
This action returns how many regions the last update loaded.
Returns
integer: The number of regions loaded.
GetLastSavedCount()
This action returns how many changed regions the last update saved before unloading them.
Returns
integer: The number of regions saved.
GetLastUnloadedCount()
This action returns how many regions the last update unloaded.
Returns
integer: The number of regions unloaded.
GetLoadRadius()
This action returns how near, in world units, a region must be to the target to be loaded.
Returns
number: The load radius.
GetLoadsPerUpdate()
This action returns how many regions may be loaded in one update.
Returns
integer: The number of regions.
GetRegionWorldSize()
This action returns how wide one region is in the world.
Returns
number: The region size times the vertex spacing.
GetTerrainData()
This action returns the terrain whose regions are loaded and unloaded.
Returns
Libraries.Game.Graphics.Worlds.TerrainData: The terrain's data.
GetUnloadRadius()
This action returns how far, in world units, a region must be from the target to be unloaded.
Returns
number: The unload radius.
LoadAllNear(number worldX, number worldZ)
This action loads every saved region within the load radius at once, with no limit per update. It is meant for the start of a game, before anything is drawn.
Parameters
worldXnumber: The target's position along x, in world units.worldZnumber: The target's position along z, in world units.
Returns
integer: How many regions were loaded.
SaveChanges()
This action saves every loaded region that has changed. Call it before the game ends to keep changes that haven't been saved by unloading.
Returns
integer: How many regions were saved.
SetFolder(Libraries.System.File folder)
This action sets the folder the world is saved in. Regions are loaded from it, and changed regions are saved back to it.
Parameters
folderLibraries.System.File: The folder.
SetLoadRadius(number radius)
This action sets how near, in world units, a region must be to the target to be loaded. The unload radius is set to this plus half a region's width, so regions at the edge don't keep loading and unloading.
Parameters
radiusnumber: The load radius, in world units. It must be more than 0.
SetLoadsPerUpdate(integer count)
This action sets how many regions may be loaded in one update. The default is 1.
Parameters
countinteger: The number of regions. It must be at least 1.
SetTerrainData(Libraries.Game.Graphics.Worlds.TerrainData data)
This action sets the terrain whose regions are loaded and unloaded.
Parameters
dataLibraries.Game.Graphics.Worlds.TerrainData: The terrain's data.
SetUnloadRadius(number radius)
This action sets how far, in world units, a region must be from the target to be unloaded. It must be at least the load radius.
Parameters
radiusnumber: The unload radius, in world units.
UnloadAll()
This action unloads every loaded region, saving the changed ones first.
Update(number worldX, number worldZ)
This action unloads regions that have moved out of reach and loads ones that have come into reach, a few at a time. Call it every frame with the target's position. Changed regions are saved before they are unloaded.
Parameters
worldXnumber: The target's position along x, in world units.worldZnumber: The target's position along z, in world units.
Returns
boolean: True if any region was loaded or unloaded.