CCG/Example: DriverCommand
(→Required Files: link to Texture Files added) |
|||
| (4 intermediate revisions by one user not shown) | |||
| Line 24: | Line 24: | ||
'''drivetoschedulecommand.gs''' - A trainz script file which is a dependency of "drivercommand.gs". | '''drivetoschedulecommand.gs''' - A trainz script file which is a dependency of "drivercommand.gs". | ||
| + | |||
| + | ===Version hint=== | ||
| + | !!! This example is an older version without async search of industries and the newer menu building methods !!! | ||
| + | The current Drive To driver command asset contains a commented newer code version. | ||
===Config.txt File Listing=== | ===Config.txt File Listing=== | ||
| Line 65: | Line 69: | ||
===DriverToCommand.gs File Listing=== | ===DriverToCommand.gs File Listing=== | ||
| − | |||
// | // | ||
// DriverToCommand.gs | // DriverToCommand.gs | ||
| Line 84: | Line 87: | ||
class DriveToCommand isclass DriverCommand | class DriveToCommand isclass DriverCommand | ||
{ | { | ||
| − | + | // | |
| − | + | // Initialize parent object and add handlers to process messages for this driver command object. | |
// | // | ||
public void Init(Asset asset) | public void Init(Asset asset) | ||
{ | { | ||
| − | + | inherited(asset); | |
| − | + | AddHandler(me, "DriveToIndustry", null, "DriveTo"); | |
| − | + | AddHandler(me, "DriveToIndustryTrack", null,"DriveToTrack"); | |
} | } | ||
| + | |||
// | // | ||
| − | + | // Adds industry destination menu items for all industries to the given menu along with submenus of | |
| − | + | // destination tracks for all industries added. | |
| − | + | // | |
| − | + | public void AddCommandMenuItem(DriverCharacter driver,Menu menu) | |
{ | { | ||
| − | + | Train train; | |
| − | + | if (driver) | |
| − | + | { | |
| − | + | train = driver.GetTrain(); | |
| − | + | if (!train) | |
| − | + | return; | |
| − | + | } | |
| − | + | StringTable strTable = GetAsset().GetStringTable(); | |
| − | + | Menu industriesMenu = Constructors.NewMenu(); | |
| − | + | GameObject[] industryList = World.GetIndustryList(); | |
| − | + | int i, industryCount = industryList.size(); | |
| − | + | if (!industryCount) | |
| − | + | // we dont bother with a `Drive To' command if there are no industries | |
| − | + | return; | |
| − | + | for (i = 0; i < industryCount; i++) | |
| − | + | { | |
| − | + | Industry industry = cast<Industry>(industryList[i]); | |
| − | + | string localisedName = industry.GetLocalisedName(); | |
| − | + | string[] locationNames = new string[0]; | |
| − | + | string[] locationTracks = new string[0]; | |
| − | + | industry.AppendDriverDestinations(locationNames,locationTracks); | |
| − | + | if (localisedName.size()) | |
| − | + | if (locationNames.size()) | |
| − | + | { | |
| − | + | Menu submenu = Constructors.NewMenu(); | |
| − | + | int j; | |
| − | + | for (j = 0; j < locationNames.size(); j++) | |
| − | + | if (locationNames[j] and locationNames[j].size() and locationTracks[j] and locationTracks[j].size()) | |
| − | + | submenu.AddItem(locationNames[j], me, "DriveToIndustryTrack", industry.GetId() + " " + locationTracks[j]); | |
| − | + | else | |
| − | + | if (train) | |
| − | + | train.Exception("Error in `track names' of industry `" + localisedName + "'"); | |
| − | + | industriesMenu.AddSubmenu(localisedName + " >",submenu); | |
| − | + | } | |
| − | + | else | |
| − | + | industriesMenu.AddItem(localisedName, me,"DriveToIndustry", industry.GetName()); | |
| − | + | industriesMenu.SubdivideItems(); | |
| − | + | menu.AddSubmenu(strTable.GetString("driver_command_drive_to") + " >", industriesMenu); | |
| − | + | } | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
} | } | ||
| + | |||
// | // | ||
// Called by either DriveTo() or DriveToTrack() to play one of random driver acknowledgments. | // Called by either DriveTo() or DriveToTrack() to play one of random driver acknowledgments. | ||
// | // | ||
| − | + | void PlayConfirmation(void) | |
| − | + | { | |
| − | + | KUID kuid = GetAsset().LookupKUIDTable("command-sounds"); | |
| − | + | Library libCommandSounds = World.GetLibrary(kuid); | |
| − | + | if (libCommandSounds) | |
| − | + | libCommandSounds.LibraryCall("PlayConfirmation",null, null); | |
| − | + | } | |
| − | + | ||
| − | + | // | |
| − | + | // Create the schedule command | |
| − | + | // | |
| − | + | DriverScheduleCommand CreateScheduleCommand(DriverCharacter driver, Soup soup) | |
| − | + | { | |
| − | + | DriveToScheduleCommand cmd = new DriveToScheduleCommand(); | |
| − | + | cmd.Init(driver, me); | |
| − | + | cmd.SetParent(me); | |
| − | + | cmd.SetProperties(soup); | |
| − | + | return cast<DriverScheduleCommand>cmd; | |
| − | + | } | |
| − | + | ||
| − | + | // | |
| − | + | // Handler method to drive a train to an industry (no specific destination track though). | |
| − | + | // | |
| − | + | void DriveTo(Message msg) | |
| − | + | { | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
DriverCommands commands = GetDriverCommands(msg); | DriverCommands commands = GetDriverCommands(msg); | ||
| − | |||
DriverCharacter driver = cast<DriverCharacter>(msg.src); | DriverCharacter driver = cast<DriverCharacter>(msg.src); | ||
| − | string | + | string industryName = msg.minor; |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
// schedule our command | // schedule our command | ||
| − | |||
Soup soup = Constructors.NewSoup(); | Soup soup = Constructors.NewSoup(); | ||
| − | soup.SetNamedTag("industryName", | + | soup.SetNamedTag("industryName", industryName); |
| − | + | ||
| − | + | ||
DriveToScheduleCommand cmd = cast<DriveToScheduleCommand>CreateScheduleCommand(driver, soup); | DriveToScheduleCommand cmd = cast<DriveToScheduleCommand>CreateScheduleCommand(driver, soup); | ||
| − | |||
commands.AddDriverScheduleCommand(cmd); | commands.AddDriverScheduleCommand(cmd); | ||
| + | if (driver) PlayConfirmation(); | ||
| + | } | ||
| − | + | // | |
| − | + | // Handler method to drive a train to a specific track in an industry. | |
| − | + | // | |
| − | + | void DriveToTrack(Message msg) | |
| − | + | { | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
DriverCommands commands = GetDriverCommands(msg); | DriverCommands commands = GetDriverCommands(msg); | ||
DriverCharacter driver = cast<DriverCharacter>(msg.src); | DriverCharacter driver = cast<DriverCharacter>(msg.src); | ||
string msgData = msg.minor; | string msgData = msg.minor; | ||
int industryId = Str.UnpackInt(msgData); | int industryId = Str.UnpackInt(msgData); | ||
| − | |||
string trackName = Str.UnpackString(msgData); | string trackName = Str.UnpackString(msgData); | ||
| − | |||
GameObject industry = Router.GetGameObject(industryId); | GameObject industry = Router.GetGameObject(industryId); | ||
| − | + | if (!industry) return; | |
| − | if (!industry) | + | |
| − | + | ||
| − | + | ||
// schedule our command | // schedule our command | ||
| − | |||
Soup soup = Constructors.NewSoup(); | Soup soup = Constructors.NewSoup(); | ||
soup.SetNamedTag("industryName", industry.GetName()); | soup.SetNamedTag("industryName", industry.GetName()); | ||
soup.SetNamedTag("trackName", trackName); | soup.SetNamedTag("trackName", trackName); | ||
| − | + | DriveToScheduleCommand cmd = cast<DriveToScheduleCommand>CreateScheduleCommand(driver, soup); | |
| − | + | ||
| − | + | ||
commands.AddDriverScheduleCommand(cmd); | commands.AddDriverScheduleCommand(cmd); | ||
| − | + | if (driver) PlayConfirmation(); | |
| − | + | } | |
| − | + | }; | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
And the dependent script | And the dependent script | ||
| Line 384: | Line 227: | ||
class DriveToScheduleCommand isclass DriverScheduleCommand | class DriveToScheduleCommand isclass DriverScheduleCommand | ||
{ | { | ||
| − | + | public string industryName; // Name of the industry to drive to. | |
| − | + | public string trackName; // Name of the track in the industry to drive to. | |
| − | + | DriveToCommand parent; | |
| − | + | ||
| − | + | // | |
| − | + | // | |
| − | + | // | |
| − | + | public void SetParent(DriveToCommand newParent) | |
| − | + | { | |
| − | + | parent = newParent; | |
| + | } | ||
| + | |||
| + | // | ||
| + | // Starts executing this schedule command on the given driver character. | ||
| + | // | ||
public bool BeginExecute(DriverCharacter driver) | public bool BeginExecute(DriverCharacter driver) | ||
{ | { | ||
| − | + | Train train = driver.GetTrain(); | |
if (!train) | if (!train) | ||
| − | + | // cant drive if we dont have a train | |
| − | + | return false; | |
| − | + | Industry industry = cast<Industry>(Router.GetGameObject(industryName)); | |
if (!industry) | if (!industry) | ||
| − | + | // cant drive to an industry which doesn't exist | |
| − | + | return false; | |
return driver.NavigateToIndustry(industry, trackName); | return driver.NavigateToIndustry(industry, trackName); | ||
| − | + | } | |
| − | + | ||
| − | + | // | |
| − | + | // we should really implement EndExecute() to allow the game to determine the success of this command | |
| − | + | // | |
| − | + | ||
| − | + | // | |
| − | + | // Provides an icon for this command so it can be seen on the driver's schedule. Uses the industry | |
| − | + | // icon to indicate the destination. | |
| + | // | ||
| + | public object GetIcon(void) | ||
| + | { | ||
| + | Industry industry = cast<Industry>(Router.GetGameObject(industryName)); | ||
return cast<object>industry; | return cast<object>industry; | ||
| − | + | } | |
| − | + | ||
| − | + | // | |
| + | // | ||
| + | // | ||
| + | public string GetTooltip(void) | ||
| + | { | ||
StringTable strTable = GetAsset().GetStringTable(); | StringTable strTable = GetAsset().GetStringTable(); | ||
string userTrackName = trackName; | string userTrackName = trackName; | ||
| − | + | Industry industry = cast<Industry>(Router.GetGameObject(industryName)); | |
if (industry) | if (industry) | ||
{ | { | ||
| − | + | string[] destNames = new string[0]; | |
| − | + | string[] destTracks = new string[0]; | |
| − | + | industry.AppendDriverDestinations(destNames,destTracks); | |
| − | + | int i; | |
| − | + | for (i = 0; i < destNames.size(); i++) | |
| − | + | { | |
if (destTracks[i] == trackName) | if (destTracks[i] == trackName) | ||
{ | { | ||
| − | + | userTrackName = destNames[i]; | |
| − | + | break; | |
} | } | ||
| − | + | } | |
| − | + | } | |
| − | + | return strTable.GetString("driver_command_drive_to") + industryName + " (" + userTrackName + ")"; | |
| − | + | } | |
| − | + | ||
| − | + | // | |
| − | + | // | |
| − | + | // | |
| − | + | public Soup GetProperties(void) | |
| − | + | { | |
| − | + | Soup soup = Constructors.NewSoup(); | |
| − | + | // Save support | |
| − | + | // Save the properties to the soup, then return the soup | |
| − | + | soup.SetNamedTag("industryName", industryName); | |
| − | + | soup.SetNamedTag("trackName", trackName); | |
| − | + | return soup; | |
| − | + | } | |
| − | + | ||
| − | + | // | |
| − | + | // | |
| − | + | // | |
| − | + | public void SetProperties(Soup soup) | |
| − | + | { | |
| + | // Load support | ||
| + | // Setup the properties from the passed in soup | ||
| + | industryName = soup.GetNamedTag("industryName"); | ||
| + | trackName = soup.GetNamedTag("trackName"); | ||
| + | } | ||
| + | }; | ||
===Download this asset=== | ===Download this asset=== | ||
| Line 464: | Line 326: | ||
A version of this asset is a ''built-in'' for TS2010, TS12. | A version of this asset is a ''built-in'' for TS2010, TS12. | ||
| − | |||
| − | |||
==Return to CCG Index== | ==Return to CCG Index== | ||
Latest revision as of 04:17, 16 February 2025
Contents |
[edit] Driver-Command
[edit] Directory Structure
A typical asset of this kind has the following File\Directory Structure:
[edit] Required Files
config.txt - The config file for the asset.
thumb.jpg - The thumbnail image for this asset. A 240x180 jpeg.
driveto.texture.txt - The texture.txt file. See the section on Texture.txt files for more information.
driveto.tga - The texture file for the drivercommand icon.
shows image and alpha channel for 64x64 pixel TGA.
drivetocommand.gs - The trainz script file referenced in the config.txt.
drivetoschedulecommand.gs - A trainz script file which is a dependency of "drivercommand.gs".
[edit] Version hint
!!! This example is an older version without async search of industries and the newer menu building methods !!!
The current Drive To driver command asset contains a commented newer code version.
[edit] Config.txt File Listing
kuid <kuid:56113:1268>
trainz-build 2.5
category-class "YD"
category-region "00"
category-era "1800s;1810s;1820s"
username "testDriverCommand"
kind "drivercommand"
supports-null-driver-character 1
script "DriveToCommand.gs"
class "DriveToCommand"
description "Test command. This does the same thing as `Drive To'."
string-table
{
description "Allows a driver character to take a train either to a
destination industry or a specific track in a destination industry."
driver_command_drive_to "Drive To "
}
kuid-table
{
command-sounds <kuid:-3:10219>
}
thumbnails
{
0
{
image "thumb.jpg"
width 240
height 180
}
1
{
image "driveto.tga"
width 64
height 64
}
}
[edit] DriverToCommand.gs File Listing
//
// DriverToCommand.gs
//
// Copyright (C) 2003 Auran Developments Pty Ltd
// All Rights Reserved.
//
include "DriverCommand.gs"
include "World.gs"
include "Browser.gs"
include "KUID.gs"
include "Industry.gs"
include "DriveToScheduleCommand.gs"
//
// Driver command that allows a driver character to take a train either to a destination industry
// or a specific track in a destination industry.
//
class DriveToCommand isclass DriverCommand
{
//
// Initialize parent object and add handlers to process messages for this driver command object.
//
public void Init(Asset asset)
{
inherited(asset);
AddHandler(me, "DriveToIndustry", null, "DriveTo");
AddHandler(me, "DriveToIndustryTrack", null,"DriveToTrack");
}
//
// Adds industry destination menu items for all industries to the given menu along with submenus of
// destination tracks for all industries added.
//
public void AddCommandMenuItem(DriverCharacter driver,Menu menu)
{
Train train;
if (driver)
{
train = driver.GetTrain();
if (!train)
return;
}
StringTable strTable = GetAsset().GetStringTable();
Menu industriesMenu = Constructors.NewMenu();
GameObject[] industryList = World.GetIndustryList();
int i, industryCount = industryList.size();
if (!industryCount)
// we dont bother with a `Drive To' command if there are no industries
return;
for (i = 0; i < industryCount; i++)
{
Industry industry = cast<Industry>(industryList[i]);
string localisedName = industry.GetLocalisedName();
string[] locationNames = new string[0];
string[] locationTracks = new string[0];
industry.AppendDriverDestinations(locationNames,locationTracks);
if (localisedName.size())
if (locationNames.size())
{
Menu submenu = Constructors.NewMenu();
int j;
for (j = 0; j < locationNames.size(); j++)
if (locationNames[j] and locationNames[j].size() and locationTracks[j] and locationTracks[j].size())
submenu.AddItem(locationNames[j], me, "DriveToIndustryTrack", industry.GetId() + " " + locationTracks[j]);
else
if (train)
train.Exception("Error in `track names' of industry `" + localisedName + "'");
industriesMenu.AddSubmenu(localisedName + " >",submenu);
}
else
industriesMenu.AddItem(localisedName, me,"DriveToIndustry", industry.GetName());
industriesMenu.SubdivideItems();
menu.AddSubmenu(strTable.GetString("driver_command_drive_to") + " >", industriesMenu);
}
}
//
// Called by either DriveTo() or DriveToTrack() to play one of random driver acknowledgments.
//
void PlayConfirmation(void)
{
KUID kuid = GetAsset().LookupKUIDTable("command-sounds");
Library libCommandSounds = World.GetLibrary(kuid);
if (libCommandSounds)
libCommandSounds.LibraryCall("PlayConfirmation",null, null);
}
//
// Create the schedule command
//
DriverScheduleCommand CreateScheduleCommand(DriverCharacter driver, Soup soup)
{
DriveToScheduleCommand cmd = new DriveToScheduleCommand();
cmd.Init(driver, me);
cmd.SetParent(me);
cmd.SetProperties(soup);
return cast<DriverScheduleCommand>cmd;
}
//
// Handler method to drive a train to an industry (no specific destination track though).
//
void DriveTo(Message msg)
{
DriverCommands commands = GetDriverCommands(msg);
DriverCharacter driver = cast<DriverCharacter>(msg.src);
string industryName = msg.minor;
// schedule our command
Soup soup = Constructors.NewSoup();
soup.SetNamedTag("industryName", industryName);
DriveToScheduleCommand cmd = cast<DriveToScheduleCommand>CreateScheduleCommand(driver, soup);
commands.AddDriverScheduleCommand(cmd);
if (driver) PlayConfirmation();
}
//
// Handler method to drive a train to a specific track in an industry.
//
void DriveToTrack(Message msg)
{
DriverCommands commands = GetDriverCommands(msg);
DriverCharacter driver = cast<DriverCharacter>(msg.src);
string msgData = msg.minor;
int industryId = Str.UnpackInt(msgData);
string trackName = Str.UnpackString(msgData);
GameObject industry = Router.GetGameObject(industryId);
if (!industry) return;
// schedule our command
Soup soup = Constructors.NewSoup();
soup.SetNamedTag("industryName", industry.GetName());
soup.SetNamedTag("trackName", trackName);
DriveToScheduleCommand cmd = cast<DriveToScheduleCommand>CreateScheduleCommand(driver, soup);
commands.AddDriverScheduleCommand(cmd);
if (driver) PlayConfirmation();
}
};
And the dependent script
[edit] DriveToScheduleCommand.gs File Fisting
//
// DriveToScheduleCommand.gs
//
// Copyright (C) 2003 Auran Developments Pty Ltd
// All Rights Reserved.
//
include "DriverCommand.gs"
include "World.gs"
include "Browser.gs"
include "KUID.gs"
include "Industry.gs"
include "DriveToCommand.gs"
include "Schedule.gs"
//
// Driver schedule command used by DriveToCommand to get a driver character to take a train to
// a specific track on an industry.
//
class DriveToScheduleCommand isclass DriverScheduleCommand
{
public string industryName; // Name of the industry to drive to.
public string trackName; // Name of the track in the industry to drive to.
DriveToCommand parent;
//
//
//
public void SetParent(DriveToCommand newParent)
{
parent = newParent;
}
//
// Starts executing this schedule command on the given driver character.
//
public bool BeginExecute(DriverCharacter driver)
{
Train train = driver.GetTrain();
if (!train)
// cant drive if we dont have a train
return false;
Industry industry = cast<Industry>(Router.GetGameObject(industryName));
if (!industry)
// cant drive to an industry which doesn't exist
return false;
return driver.NavigateToIndustry(industry, trackName);
}
//
// we should really implement EndExecute() to allow the game to determine the success of this command
//
//
// Provides an icon for this command so it can be seen on the driver's schedule. Uses the industry
// icon to indicate the destination.
//
public object GetIcon(void)
{
Industry industry = cast<Industry>(Router.GetGameObject(industryName));
return cast<object>industry;
}
//
//
//
public string GetTooltip(void)
{
StringTable strTable = GetAsset().GetStringTable();
string userTrackName = trackName;
Industry industry = cast<Industry>(Router.GetGameObject(industryName));
if (industry)
{
string[] destNames = new string[0];
string[] destTracks = new string[0];
industry.AppendDriverDestinations(destNames,destTracks);
int i;
for (i = 0; i < destNames.size(); i++)
{
if (destTracks[i] == trackName)
{
userTrackName = destNames[i];
break;
}
}
}
return strTable.GetString("driver_command_drive_to") + industryName + " (" + userTrackName + ")";
}
//
//
//
public Soup GetProperties(void)
{
Soup soup = Constructors.NewSoup();
// Save support
// Save the properties to the soup, then return the soup
soup.SetNamedTag("industryName", industryName);
soup.SetNamedTag("trackName", trackName);
return soup;
}
//
//
//
public void SetProperties(Soup soup)
{
// Load support
// Setup the properties from the passed in soup
industryName = soup.GetNamedTag("industryName");
trackName = soup.GetNamedTag("trackName");
}
};
[edit] Download this asset
This asset is available for download from the TRS2006 website at:
Sorry, no download available.
A version of this asset is a built-in for TS2010, TS12.
