WestSideRailways
Active member
Seeing that I have Many Locos on my Route, was wondering if there was a way to randomly view all the locos, instead of selecting myself.
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
I haven`t heard of one, but I can confirm that it is entirely possible with scripting. As usual, the actual code to do it is far smaller than the boilerplate needed to make it work at all, and also far smaller than the code needed to make it work correctly.I don't know of a way to change view to different drivers randomly unless there is session rule to do it.
I have done a little programing a while a go... what is required to learn scripting for Trainz....That certainly looks like it would work. It also looks like it would quickly get tiring to set up if you have any significant number of locomotives to choose from.
If I wasn`t deeply engrossed in issues of my own, I think that I could whip up a single Rule that would automatically focus on any locomotive found anywhere on the route, with no configuration required. Unfortunately, one of the issues I`m having to deal with is getting my own Rule (which only loads a library that doesn`t do much yet either) to run without errors.
class
statement, the object-oriented aspect will transfer well. Other aspects of the language will not. The language itself is uses a syntax a little more like C++. If you find yourself able to read and understand the "include files", you will learn a lot about the scripting language by doing so. You will also learn a lot about the scripting environment, as many of the files are well-commented. There are pages on the Wiki that describe the language, but I learned by reading code, years ago, when I ran Trainz 2004.Consists[] = world.gettrainslist()
, then loop through the resulting list and query each consist for its locomotive. I don`t recall off-hand what the correct call, but there should be a call on the consist class to retrieve the loco. If there isn`t, you can loop through the vehicles on each consist and test for locomotiveness. Either way, you can then select at random from the resulting list. You will need to capture the length of the list and feed that to the random number generator. I can do some research for you (unfortunately, now, as i type this, is inconvenient for me); just let me know if you want me to.That would be Much Appreciated.Without doing specific research, I would expect that an approach similar to that is feasible. First getConsists[] = world.gettrainslist()
, then loop through the resulting list and query each consist for its locomotive. I don`t recall off-hand what the correct call, but there should be a call on the consist class to retrieve the loco. If there isn`t, you can loop through the vehicles on each consist and test for locomotiveness. Either way, you can then select at random from the resulting list. You will need to capture the length of the list and feed that to the random number generator. I can do some research for you (unfortunately, now, as i type this, is inconvenient for me); just let me know if you want me to.
public void FocusRandomLoco(void)
{
Vehicle[] LocoList;
Vehicle[] VehicleList = World.GetVehicleList();
for (i = 0; i < VehicleList.size(); i++)
{
Vehicle ThisVehicle = VehicleList[i];
if (ThisVehicle.GetVehicleTypeFlags() == ThisVehicle.TYPE_LOCOMOTIVE)
LocoList[LocoList.size()] = ThisVehicle;
};
//Select one at random.
World.SetCamera(LocoList[math.Rand(0, LocoList.size()-1)]);
};
//==========================================================================================
// File: RandomSelectTrain.gs
// Desc: View Trainz Randomly
//==========================================================================================
include "ScenarioBehavior.gs"
include "TrainzAssetSearch.gs"
Include "World.gs"
//=====================================
// Get List of Trainz
//=====================================
class RandomSelectTrain isclass ScenarioBehavior
{
public void FocusRandomLoco(void)
{
Vehicle[] LocoList;
Vehicle[] VehicleList = World.GetVehicleList();
for (i = 0; i < VehicleList.size(); i++)
{
Vehicle ThisVehicle = VehicleList[i];
if (ThisVehicle.GetVehicleTypeFlags() == ThisVehicle.TYPE_LOCOMOTIVE)
LocoList[LocoList.size()] = ThisVehicle;
};
//Select one at random.
World.SetCamera(LocoList[math.Rand(0, LocoList.size()-1)]);
};
Thanks for the tip............ been a long long time since i have do anything like this......I click on your link and I get an error dialog centered in my browser window. I`m assuming that the error dialog is a screen-capture of the error you are referring to.
Your code looks reasonably correct to me, but I didn`t test it before posting. I am not familiar (yet!) with attempting to compile scripts with TrainzUtil. I do know, however, that Trainz has to be running to use it, even if only attempting to generate the help output. It apparently does not require any particular window to be open, but I cannot verify that at this time either.
I`ve always done my compiling by attempting to Submit the edited asset, or by using View Errors and Warnings. Sometimes it requires both for me to figure out what, if anything, is going wrong with my code. Judging by your error message, it appears to me that you are trying to compile the script outside of any asset. I do not know that TrainzUtil is able to do that. I suggest putting your file inside an asset, perhaps a Rule asset, and trying to compile it in-context. If everything works as anticipated, the Rule should focus the camera on a randomly selected locomotive.
It occurred to me just now that you would likely want the locomotive to be active for driving also. If we can get the Rule working, the required changes would be trivial. If we cannot get the code to run correctly inside Trainz, the required changes would be pointless.
//==========================================================================================
// File: RandomSelectTrain.gs
// Desc: View Trainz Randomly
//==========================================================================================
include "ScenarioBehavior.gs"
include "TrainzAssetSearch.gs"
//=====================================
// Get List of Trainz
//=====================================
class RandomSelectTrain isclass ScenarioBehavior
{
public void FocusRandomLoco(void)
{
Vehicle[] LocoList;
Vehicle[] VehicleList = World.GetVehicleList();
for (i = 0; i < VehicleList.size(); i++)
{
Vehicle ThisVehicle = VehicleList[i];
if (ThisVehicle.GetVehicleTypeFlags() == ThisVehicle.TYPE_LOCOMOTIVE)
LocoList[LocoList.size()] = ThisVehicle;
};
//Select one at random.
World.SetCamera(LocoList[math.Rand(0, LocoList.size()-1)]
};
Vehicle[] LocoList;
should be Vehicle[] LocoList = new Vehicle[0];
, assuming that I got the syntax correct.