Random viewing of Locos

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.
 
Start Trainz.
At the Main Menu, choose My Collection. This will allow you to see your installed locomotives, consists, and train cars. Changing a locomotive to something else here, will update the one found on the main screen. This is in some ways similar to the old train shed in TS12 and below.

In Content Manager, you can set your view to thumbnails and sort on Category Locomotives. These same categories are available in Surveyor and the Session Editor as well. In Classic Surveyor, press CTRL+shift+F to bring up the search filter (where pick-lists are located). You can set up the search for locomotives, change the region, etc., and then make your own pick-list here.
 
I think he means in Driver, John. I don't know of a way to change view to different drivers randomly unless there is session rule to do it.
 
I don't know of a way to change view to different drivers randomly unless there is session rule to do it.
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.
 
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.
 
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.
I have done a little programing a while a go... what is required to learn scripting for Trainz....
:sneaky:(y)
Know a little python if that helps....
 
You know a little Python? If you`re comfortable with the Python 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.
 
how do I access the Route's list of Trainz( locos ) not consists.

Much appreciated.

would it be something like this.....

Vehicle{} = world.gettrainlist();
 
Without doing specific research, I would expect that an approach similar to that is feasible. First get 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.
 
Without doing specific research, I would expect that an approach similar to that is feasible. First get 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:giggle:.
 
Ok. Will do, and get back to you. I`ll bring the results here to this message.

Do note that this message, despite appearing in the forum database, will not be complete until I explicitly say that I have finished it, so keep checking back until you see that I have said so, ok?

Skimming through World.gs, I fail to find any methods for getting a list of all motive power on the route, so we will have to do this ourselves.

Try something like this:
Code:
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)]);
};
This message is now finished. Please feel free to reply to it now.

Edit: Oops! Found a few errors to tweak after declaring it finished.
 
Last edited:
No problem. Between the research and the code-writing that I did as I was researching, it took me only about an hour. While researching this, I found the designated solution to the problem I was trying to help another poster with, and directed him to the place that I had found a well-documented work-around. Let me know what you think, OK?
 
Code:
//==========================================================================================
// 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)]);
};

Is this correct.?
when i try to compile it i get this error
Screenshot

Does Content Manager need to be open when compiling.?
 
Last edited:
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.
 
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.
Thanks for the tip............ been a long long time since i have do anything like this......
Have just make a NEW Asset in CM and will go from there by looking at other Rules by opening for edit.

Like this sentence in another thread....

I can do the impossible easily;

it`s the easy stuff that I find impossible.
:D(y)
 
Code:
//==========================================================================================
// 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)]
};

So far so good... :)

Have a asset in CM, but it says that there is a parse error in line 27.
Which is the last line, if i move the "};" to line 26... it says the same.

for some reason i am now getting the same error in line 16, which is the public void line....

going crazy trying to sort this out...... will give it a rest for a while....
 
Last edited:
I discovered an error in my original code while looking at something else this morning: Vehicle[] LocoList; should be Vehicle[] LocoList = new Vehicle[0];, assuming that I got the syntax correct.

Speaking of syntax, I notice that you have removed the semicolon from the SetCamera line. Is there a reason for this? I also note unbalanced braces; you need an additional close-brace at the end. Unfortunately, I`m a bit unclear on exactly where close-braces need semicolons and where they are forbidden.

Additionally, to operate the rule, the class will require an Init(Asset) method; for something this simple, renaming FocusRandomLoco should suffice.

Don`t forget to tell the config.txt file which script file and what class are required. It won`t work if you forget.

That`s about all I can think of at this time; I have no idea why it doesn`t like the function header. I`ll check back later to see how this is working out for you.

That quote does seem to apply here, doesn`t it?
 
Back
Top