Class World

From TrainzOnline
Revision as of 08:32, 25 July 2013 by Frogpipe (Talk | contribs)

Jump to: navigation, search


  • World is a static class used to access various aspects of the Trainz environment.


Contents

Constants

  • These constants are used to define various values and can be used as mnenomics.
  • If accessed from scripts the values must be prefixed World., for example:
World.SetWeather(World.WEATHER_TYPE_LIGHT_SNOW,World.WEATHER_CHANGEABILITY_PERIODIC);


Camera View Modes


  • These values can be used either to choose the current camera viewing mode through World.SetCameraMode(), or to restrict certain camera modes from being accessed through methods such as World.SetCamera().
public define int CAMERA_INTERNAL = 1 Internal cabin camera view.
public define int CAMERA_EXTERNAL = 2 External train camera view
public define int CAMERA_TRACKING = 4 External tracking camera view.
public define int CAMERA_ROAMING = 128 Roaming camera view.


Camera Flags


  • These flags are used in addition to the Camera Mode Flags to allow or disallow specific camera actions.
public define int CAMERA_SWITCH_VIEWS = 8 User permitted to swap between the camera views.
public define int CAMERA_SWITCH_VEHICLES = 16 User permitted to swap vehicles within the current train.
public define int CAMERA_SWITCH_TRAINS = 32 User permitted to swap to other trains which are in the consist menu.
public define int CAMERA_LOCKED = 64 User is NOT permitted to adjust camera (i.e. zoom, pan etc.).
public define int CAMERA_NORMAL = * Normal camera mode where everything is permitted.
* NOTE: CAMERA_NORMAL = CAMERA_INTERNAL | CAMERA_EXTERNAL | CAMERA_TRACKING | CAMERA_SWITCH_VIEWS | CAMERA_SWITCH_VEHICLES | CAMERA_SWITCH_TRAINS


Weather Type Constants


  • Define the current state of the weather in Trainz.
  • A variety of weather conditions are supported and can be changed by using one of these values with the World.SetWeather() method.
public define int WEATHER_TYPE_CLEAR = 0 Clear weather, no clouds, rain or snow.
public define int WEATHER_TYPE_CLOUDY = 1 Cloudy weather, no rain.
public define int WEATHER_TYPE_DRIZZLE = 2 Drizzle, light rain.
public define int WEATHER_TYPE_RAIN = 3 Rainy weather.
public define int WEATHER_TYPE_STORMY = 4 Stormy weather that includes lightning and thunder.
public define int WEATHER_TYPE_LIGHT_SNOW = 5 Snowing lightly.
public define int WEATHER_TYPE_MEDIUM_SNOW = 6 Medium level snow fall.
public define int WEATHER_TYPE_HEAVY_SNOW = 7 Heavy snow fall.


Weather Changeability Constants


  • Define the changeability settings for the weather.
  • Weather changeability refers to variability of weather conditions rather than to a specific weather type.
public define int WEATHER_CHANGEABILITY_NONE = 0 Weather remains static.
public define int WEATHER_CHANGEABILITY_PERIODIC = 1 Periodic changes in weather.
public define int WEATHER_CHANGEABILITY_EXTREME = 2 Extreme and frequent changes in weather.


Time Values


  • Time constants defined in seconds.
public define float TIME_SECOND = 1.0f One second.
public define float TIME_MINUTE = TIME_SECOND * 60.0f One minute, 60.0 seconds.
public define float TIME_HOUR = TIME_MINUTE * 60.0f One hour, 60.0 minutes, 3600.0 seconds.
public define float TIME_DAY = TIME_HOUR * 24.0f One day, 24 hours, 1440.0 minutes, 86400.0 seconds.


Time Rate Constants


  • Values that define the time rates relative to normal time.
  • These constants can be used to set the time rate that Trainz runs out through the World.SetGameTimeRate() method.
public define int TIME_RATE_1X = 0 Real time (i.e. time in Trainz passes at the same rate as in the real world)
public define int TIME_RATE_2X = 1 Time passes at twice normal speed.
public define int TIME_RATE_4X = 2
public define int TIME_RATE_8X = 3
public define int TIME_RATE_16X = 4
public define int TIME_RATE_32X = 5
public define int TIME_RATE_60X = 6
public define int TIME_RATE_120X = 7
public define int TIME_RATE_240X = 8
public define int TIME_RATE_360X = 9
public define int TIME_RATE_480X = 10
public define int TIME_RATE_720X = 11
public define int TIME_RATE_960X = 12
public define int TIME_RATE_1200X = 13
public define int TIME_RATE_1440X = 14


Trainz Modules


  • Trainz module mode constants.
  • These values define the various module environments that Trainz can be in.
public define int NO_MODULE = 0 Trainz not running in a module.
public define int SURVEYOR_MODULE = 1 Trainz is currently running in Surveyor.
public define int DRIVER_MODULE = 2 Trainz is currently running in Driver.


Related Messages


AddDriverCharacter

public native DriverCharacter AddDriverCharacter(Asset driverCharacterAsset)
Parameters
  • driverCharacterAsset = Asset containing character to be added.
Returned Value
  • Reference to the DriverCharacter if successful, null otherwise.
Syntax
DriverCharacter Casey = World.AddDriverCharacter(CaseyJones);
Notes


AddDriverCommand

public native DriverCommand AddDriverCommand(Asset driverCommandAsset)
Parameters
  • driverCommandAsset = Asset containing command to be added.
Returned Value
  • Reference to the DriverCommand if successful, null otherwise.
Syntax
DriverCommand GoFaster = World.AddDriverCommand(accelerate);
Notes


CreateBehavior

public native ScenarioBehavior CreateBehavior(KUID kuid, Soup initialProperties)
Parameters
  • kuid = KUID of scenario.

initialProperties = Soup object containing initial properties for scenario.

Returned Value
  • Reference to the ScenarioBehavior created if successful, null otherwise.
Syntax
ScenarioBehavior scenario = World.CreateBehavior(scenarioAsset, scenarioSoup);
Notes
  • The returned ScenarioBehavior object may be ended by releasing all references to it (resetting all references to null).


CreateTrain

public native Train CreateTrain(KUID[ ] consist, string trackmarkName, bool direction)
public native Train CreateTrain(KUID[ ] consist, TrackMark trackmark, bool direction)
public native Train CreateTrain(KUID[ ] consist, Buildable buildable, string trackName, float position, bool direction)
Parameters
  • consist = An array of vehicle KUIDs used to describe the required Train object.
  • trackmarkName = Name of Trackmark at which the consist is to be formed.
  • buildable = Buildable on which the consist is to be formed.
  • trackName = Name of track within Buildable object.
  • position = Distance at which the Train is to be placed relative to the start of the track.
  • trackmark = Reference to Trackmark at which the consist is to be formed.
  • direction = Orientation of Train relative to direction of track or trackmark.
Returned Value
  • Reference to the Train created if successful, null otherwise.
Syntax
Train train = World.CreateTrain(consist, buildable, "Track 1", 10.0, true);
Notes
  • These methods create a train from a list of vehicles in a consist array.
  • Depending on the variant of the method used, the train can be created at a Trackmark, specified by name or reference, or at a Buildable or Industry object.
  • It is the responsibility of the script programmer to ensure that sufficient track exists at the given location to allow the train to be created.


DeleteTrain

public native void DeleteTrain(Train train)
Parameters
  • train = train to be deleted from map.
Returned Value
  • None
Syntax
World.DeleteTrain(flyingScotsman);
Notes
  • Once a train has been deleted, care should be taken to avoid using its reference.


DeleteVehicle

public native void DeleteVehicle(Vehicle vehicle)
Parameters
  • vehicle = vehicle to be deleted from its train.
Returned Value
  • None
Syntax
World.DeleteVehicle(vehicles[vehicles.size()-1]);
Notes
  • Use Train.GetVehicles() to obtain an array of vehicles for a given train.
  • Use Vehicle.GetMyTrain() to obtain a train reference for a given vehicle.


FindAsset

public native Asset FindAsset(KUID kuid)
Parameters
  • kuid = KUID of Asset to return.
Returned Value
  • Given a valid KUID parameter, this method return an Asset reference if successful, null otherwise.
Syntax
Asset asset = World.FindAsset(kuid);
Notes
  • See also Asset.FindAsset()


FindDriverCommand

public native DriverCommand FindDriverCommand(Asset driverCommandAsset)
Parameters
  • driverCommandAsset = Asset containing command to be added.
Returned Value
  • Reference to the DriverCommand if successful, null otherwise.
Syntax
DriverCommand GoFaster = World.FindDriverCommand(accelerate);
Notes


GetAssetList

public native Asset[ ] GetAssetList(string kind)
Parameters
  • kind = String representing the Kind of asset to return.
Returned Value
  • A list of all assets of the given kind
Syntax
Asset[] Libraries = World.GetAssetList("Library");
Notes
  • All installed assets of the given kind are returned, irrespective of whether or not they are in use on the current map.


GetBadWeatherFog

public native float GetBadWeatherFog(void)
Parameters
  • None
Returned Value
  • Bad weather fog distance as a float in the range 0.0..1.0.
Syntax
float dist = World.GetBadWeatherFog();
Notes


GetBehaviors

public native ScenarioBehavior[ ] GetBehaviors(void)
Parameters
  • None
Returned Value
  • A list of top-level behaviors or rules on the currently loaded route.
Syntax
ScenarioBehavior[] rules = World.GetBehaviors();
Notes


GetCameraPositionX

public native float GetCameraPositionX(void)
Parameters
  • None
Returned Value
  • X Coordinate of the current camera position.
Syntax
float X = World.GetCameraPositionX();
  • Notes


GetCameraPositionY

public native float GetCameraPositionY(void)
Parameters
  • None
Returned Value
  • Y Coordinate of the current camera position.
Syntax
float Y = World.GetCameraPositionY();
  • Notes


GetCameraPositionZ

public native float GetCameraPositionZ(void)
Parameters
  • None
Returned Value
  • Z Coordinate of the current camera position.
Syntax
float Z = World.GetCameraPositionZ();
  • Notes


GetCurrentModule

public native int GetCurrentModule(void)
Parameters
  • None
Returned Value
  • An integer representing the current Trainz module.
Syntax
int module = World,GetCurrentModule();
Notes


GetDriverCharacterList

public native DriverCharacter[ ] GetDriverCharacterList(void)
Parameters
  • None
Returned Value
  • An array of DriverCharacter objects installed in the current world.
Syntax
DriverCharacter[] drivers = World.GetDriverCharacterList();
Notes


GetDriverCommandList

public native DriverCommand[ ] GetDriverCommandList(void)
Parameters
  • None
Returned Value
  • An array of DriverCommand objects installed in the current world.
Syntax
DriverCommand[] commands = World.GetDriverCommandList();
Notes


GetGameTime

public native float GetGameTime(void)
Parameters
  • None
Returned value
  • Current game time.
Syntax
float Now = World.GetGameTime();
Notes
  • The returned time is normalised to a value between 0.0 and 1.0
  • 0.0 = midday.
  • 0.5 = midnight
  • Use the Time Value constants to convert the normalised values to human readable times.


GetGoodWeatherFog

public native float GetGoodWeatherFog(void)
Parameters
  • None
Returned Value
  • Good weather fog distance as a float in the range 0.0..1.0.
Syntax
float dist = World.GetGoodWeatherFog();
Notes


GetGroundDrawDistance

public native float GetGroundDrawDistance(void)
Parameters
  • None
Returned Value
  • Ground draw distance in metres.
Syntax
float dist = World.GetGroundDrawDistance();
Notes


GetIndustryList

public native GameObject[ ] GetIndustryList(void)
Parameters
  • None
Returned Value
  • An array of references to all of the Industry objects for the current route.
Syntax
GameObject[] Industries = World.GetIndustryList();
Notes


GetJunctionList

public native Junction[ ] GetJunctionList(void)
Parameters
  • None
Returned Value
  • An array of references to all of the trackside junction objects for the current route.
Syntax
Junction[] junctions = World.GetJunctionList();
Notes
  • Junction objects attached to fixed track junctions are not currently returned by this method.


GetLibrary

public native Library GetLibrary(KUID kuid)
Parameters
  • kuid = KUID of script library asset to load.
Returned Value
  • A reference to the script library asset if successful, null otherwise.
Syntax
Library codelib = World.GetLibrary(LibraryKUID);
Notes
  • The Library asset is initialised when this method is first called in a session.
  • Since only one instance can exist, subsequent calls return a reference to the existing Library object.
  • See Library for notes on how to use this method.


GetSceneryDrawDistance

public native float GetSceneryDrawDistance(void)
Parameters
  • None
Returned Value
  • Scenery draw distance in metres.
Syntax
float dist = World.GetSceneryDrawDistance();
Notes


GetSeconds

public native float GetSeconds(void)
Parameters
  • None
Returned Value
  • The current value of a floating point number incrementing once per second in real time.
Syntax
float start = World.GetSeconds();
// do stuff
float finish = World.GetSeconds();
float period = finish - start;
Notes
  • Each call will return a float value containing the current value of an internal counter.
  • Subtracting a later value from a value saved at an earlier call will enable the delay between the two calls to be established.


GetSignalList

public native Signal[ ] GetSignalList(void)
Parameters
  • None
Returned Value
  • An array of references to all of the signal objects for the current route.
Syntax
Signal[] signals = World.GetSignalList();
Notes


GetStringTable

public native StringTable GetStringTable(void)
Parameters
  • None
Returned Value
  • The StringTable for the current session.
Syntax
StringTable data = World.GetStringTable();
Notes


GetSurveyorSavedConsist

public native Soup GetSurveyorSavedConsistList(string name)
Parameters
  • name = Name of the consist to retireiev.
Returned Value
  • Returns a Soup definition of the named consist if it exists, null otherwise.
Syntax
Soup consist = World.GetSurveyorSavedConsist("Brighton Belle");
Notes
  • Remember that the returned data is in a Soup format, which is not the same thing as a Train.
  • TrainUtil.CreateTrainFormSoup() can be used to create a train from the returned Soup.
  • This method only works in Surveyor.


GetSurveyorSavedConsistList

public native string[ ] GetSurveyorSavedConsistList()
Parameters
  • None
Returned Value
  • An array of names of all the saved consist objects in Surveyor.
Syntax
string[] consists = World.GetSurveyorSavedConsistList();
Notes
  • This method only works in Surveyor.


GetTrackmarkList

public native TrackMark[ ] GetTrackmarkList(void)
Parameters
  • None
Returned Value
  • An array of references to all of the trackmark objects for the current route.
Syntax
Trackmark[] trackmarks = World.GetTrackmarkList();
Notes


GetTrainList

public native Train[ ] GetTrainList(void)
Parameters
  • None
Returned Value
  • An array of references to all of the trains for the current route.
Syntax
Train[] trains = World.GetTrainList();
Notes


GetTrainzBuild

public native int GetTrainzBuild(void)
Parameters;
  • None
Returned Value
  • Gets the current build number of Trainz.
Syntax
int build = World.GetTrainzBuild();
Notes
  • The value returned corresponds to the build number you can see in the bottom-right side of the main Trainz menu screen.


GetTrainzVersion

public native float GetTrainzVersion(void)
Parameters
  • None
Returned Value
  • The TRS version number.
Syntax
float version = World.GetTrainzVersion();
Notes
  • The value returned corresponds to the version number you can see in the bottom-right side of the main Trainz menu screen.


GetTriggerList

public native Trigger[ ] GetTriggerList(void)
Parameters
  • None
Returned Value
  • An array of references to all of the trigger objects for the current route.
Syntax
Trigger[] triggers = World.GetTriggerList();
Notes
  • This method does not return scenery triggers, ie those attached to SceneryWithTrack objects and their descendants.


GetVehicleList

public native Vehicle[ ] GetVehicleList(void)
Parameters
  • None
Returned Value
  • An array of references to all of the vehicles in the current route.
Syntax
Vehicle[] vehicles = World.GetVehicleList();
Notes


GetWeatherChangeability

public native int GetWeatherChangeability(void)
Parameter
  • None
Returned Value
Syntax
int variability = World.GetWeatherChangeability();
Notes


GetWeatherType

public native int GetWeatherType(void)
Parameter
  • None
Returned Value
Syntax
int weather = World.GetWeatherType();
Notes


LoadMap

public native bool LoadMap(KUID kuid)
Parameters
  • kuid = KUID of map to load.
Returned Value
  • True if successful, false otherwise.
Syntax
if (World.LoadMap(mapKUID) Interface.Print("Map loaded OK");
else Interface.Print("Map load failed");
Notes


Play2DSound

public float Play2DSound(Asset asset, string wave)
Parameters
  • asset = Asset where the sound file is located.
  • wave = Name of the *.wav file to be played, a relative path can be included.
Returned Value
  • The length of the sound file in seconds.
  • In TS12 the returned value is undefined, see notes below under "PlaySound"
Syntax
// Play the sound and continues script execution.
   World.Play2DSound(soundAsset,"sounds/allaboard.wav");        
// Wait until sound has completed before continuing.
   Sleep(World.Play2DSound(soundAsset,"sounds/allaboard.wav")); 
Notes
  • Plays a .wav" sound file from the given asset's folder.
  • The sound is non directional and can be mono or stereo.
  • This method does not return after the sound has finished playing, it returns with the length of the sound as soon as it is called.
  • This means your code will keep on executing while Trainz is playing the sound.
  • If you want to wait for the sound to complete playing before moving on, wrap the Play2DSound() call up in a Sleep() call. Doesn't work in TS12, see notes in PlaySound below.
  • Remember that Sleep() can't always be used.


PlaySound

public native float PlaySound
    (Asset asset, string wave, float vol, float near, float far, GameObject target, string pt)
    (Asset asset, string wave, float vol, float near, float far, GameObject target, string pt, bool loop)
Parameters
  • asset = Asset where the sound file is located.
  • wave = Name of the *.wav file to be played, a relative path can be included.
  • vol = Volume level to play the sound at
  • near = Distance from the target asset at which the sound will be played at maximum volume.
  • far = Distance from the target asset at which the sound will become inaudible.
  • target = GameObject to which the sound will be attached if a 3d sound is required, null for 2D.
  • pt = Name of attachment point in the target object if a 3d sound is required, null for 2D.
  • loop = (Optional) If true the sound will loop continuously.
Returned Value
  • The length of the sound file in seconds.
  • NOTE: In TS12, the returned value is now undefined, and currently always returns '10'. This is to remove the performance cost of having to read the file from disk immediately to determine the sound length before the function can complete. While a fixed value Sleep() would be more efficient, you can can also use Sleep(wave.size()) immediately following the PlaySound to pause execution for the duration of the sound.
Syntax
// Play the sound and continues script execution.
   World.PlaySound(soundAsset,"sounds/allaboard.wav",1000.0,10.0,100.0,Station,"a.loudspeaker");        
// Wait until sound has completed before continuing.
   Sleep(World.PlaySound(soundAsset,"sounds/allaboard.wav",1000.0,10.0,100.0,Station,"a.loudspeaker")); 
Notes
  • Plays a .wav sound file from the given asset's folder.
  • If the sound is to be directional a mono sound file is required.
  • This method does not return after the sound has finished playing, it returns with the length of the sound as soon as it is called.
  • This means your code will keep on executing while Trainz is playing the sound.
  • If you want to wait for the sound to complete playing before moving on, wrap the PlaySound() call up in a Sleep() call.
  • Remember that Sleep() can't always be used.
  • The sound volume will be at a maximum for a camera position at or less than the value of the near parameter and will reduce with distance until the hearing position reaches far, when the sound will be inaudible.


RemoveDriverCharacter

public native void RemoveDriverCharacter(DriverCharacter driver)
Parameters
  • driver = DriverCharacter to remove.
Returned Value
  • None
Syntax
World.RemoveDriverCharacter(dave);
Notes


RemoveDriverCommand

public native void RemoveDriverCommand(DriverCommand command)
Parameters
  • command = DriverCommand to remove.
Returned Value
  • None
Syntax
World.RemoveDriverCommand(driverCommand);
Notes


SetBadWeatherFog

public native void SetBadWeatherFog(float distance)
Parameters
  • distance = Bad weather fog distance to set.
Returned Value
  • None
Syntax
World.SetBadWeatherFog(0.5);
Notes


SetCamera

public native bool SetCamera(MapObject focus)
public native void SetCamera(Junction focus)
public native void SetCamera(Vehicle focus, int cameraMode)
public native void SetCamera(Train focus, int cameraMode)
Parameters
  • focus = Object on which the camera is to be focused.
  • cameraMode' = One of the Camera View Mode constants defining the view which should be assigned to the camera.
Returned Value
  • The first variant returns a boolean to indicate success or failure.
  • Remaining variants do not return a value

'Syntax

World.SetCamera(train.GetVehicles[0],World.CAMERA_TRACKING); // sets the camera to track the first vehicle in the train.


SetCameraAngle

public void SetCameraAngle(int yaw, int pitch, float radius)
Parameters
  • yaw = Camera rotation in degrees, 90 is a direct front view.
  • pitch = Camera angle relative to the horizon, 0 is horizontal, 90 points vertically upwards.
  • radius = Distance in metres to the object in focus.
Returned Value
  • None
Syntax
World.SetCameraAngle(90,0,25); // Sets the camera to a direct frontal view at 25 metres from the currently focused object.
Notes
  • Radius is measured from the object origin point (0,0,0).


SetCameraFlags

public native void SetCameraFlags(int flags)
Parameter
  • flags integer value defining what the user is permitted to do with the game camera.
Returned Value
  • None
Syntax
// User is permitted to select External or Tracking views only.
   World.SetCameraFlags(World.CAMERA_EXTERNAL | World.CAMERA_TRACKING);
Notes


SetCameraMode

public void SetCameraMode(int mode)
Parameter
  • mode = Camera mode to select.
Returned Value
  • None
Syntax
World.SetCameraMode(World.CAMERA_TRACKING);
Notes


SetGameTime

public void SetGameTime(float time)
Parameters
  • time = new gane time to set.
Returned Value
  • None
Syntax
World.SetGameTime(0.5);  // sets time to midnight
Notes
  • Game time is normalised to a value between 0.0 and 1.0
  • 0.0 = midday.
  • 0.5 = midnight
  • Use the Time Value constants to convert human readable times to normalised values.


SetGameTimeRate

public native void SetGameTimeRate(float rate)
Parameters
  • rate = New time rate to set.
Returned Value
  • None
Syntax
World.SetGameTimeRate(World.TIME_RATE_8X); // sets game time to advance by 8 minutes per minute of real world time.
Notes


SetGoodWeatherFog

public native void SetGoodWeatherFog(float distance)
Parameters
  • distance = Good weather fog distance to set.
Returned Value
  • None
Syntax
World.SetGoodWeatherFog(0.5);
Notes


SetGroundDrawDistance

public native void SetGroundDrawDistance(float distance)
Parameters
  • distance = Ground draw distance in metres.
Returned Value
  • None
Syntax
World.SetGroundDrawDistance(1000.0);
Notes
  • Compatibility warning: Use of this function is not recommended, it may become non-functional in future Trainz versions.


SetSceneryDrawDistance

public native void SetSceneryDrawDistance(float distance)
Parameters
  • distance = Scenery draw distance in metres.
Returned Value
  • None
Syntax
World.SetSceneryDrawDistance(1000.0);
Notes
  • Compatibility warning: Use of this function is not recommended, it may become non-functional in future Trainz versions.
  • In TS12 is OBSOLETE.


SetTargetObserver

public void SetTargetObserver(GameObject observer)
Parameters
  • observer = ?
Returned Value
  • None
Syntax
?
Notes
  • One of the great unsolved mysteries of Trainz


SetWeather

public native void SetWeather(int type, int changeability)
Parameters
Returned Value
  • None
Syntax
World.SetWeather(World.WEATHER_TYPE_HEAVY_SNOW,World.WEATHER_CHANGEABILITY_EXTREME); // April in Paris.
Notes


UserSetCamera

public native bool UserSetCamera(GameObject target)
Parameters
  • target = GameObject to set focus to.
Returned Value
  • True if successful, false otherwise.
Syntax
World.UserSetCamera(Router,GetGameObject("Junction 5"));
Notes


UserSetCameraMode

public native void UserSetCameraMode(int mode)
Parameters
Returned Value
  • None
Syntax
World.UserSetCameraMode(World.CAMERA_ROAMING);  // switches to roaming view if permitted.
Notes
  • This method respects the current Camera Flag settings, disallowing any change which the flags do not permit.



Categories

Personal tools