Engine script only used for player controlled trains

Fire up your 3D modelling tool and start building some rolling stock, or find out what others are previewing here. If you have questions about how to make the blueprints work or what names mean what in models, here's the place to find out your answers.

Moderator: Moderators

Locked
clkgtr
Getting the hang of things now
Posts: 48
Joined: Sun Jul 15, 2012 3:00 am

Engine script only used for player controlled trains

Post by clkgtr »

Hi there,
recently I am making an loco, as the functions of the loco become more and more, the script is larger and larger.
But if I put many locos in the scenario ,the FPS will drop very low.
So I wonder if there is any way to make the engine script only used for Player controlled train,not usable for AI train.
I once wrote in the script like this

Code: Select all

function Update (time)
if (Call( "GetIsPlayer" ) == 1 then 
    ...................
     engine script 
   ......................
end
end
But this caused the player controlled train's script is not usable either.
so could someone please point out where I was wrong in the script
or is there any good idea?
Thanks
User avatar
DaveDewhurst
Very Active Forum Member
Posts: 1990
Joined: Wed Nov 28, 2007 10:21 am
Location: Birkenhead
Contact:

Re: Engine script only used for player controlled trains

Post by DaveDewhurst »

That should work, count your brackets,

Dave
nschichan
Been on the forums for a while
Posts: 135
Joined: Thu Sep 15, 2011 10:52 pm

Re: Engine script only used for player controlled trains

Post by nschichan »

clkgtr wrote:Hi there,
recently I am making an loco, as the functions of the loco become more and more, the script is larger and larger.
But if I put many locos in the scenario ,the FPS will drop very low.
So I wonder if there is any way to make the engine script only used for Player controlled train,not usable for AI train.
I once wrote in the script like this

Code: Select all

function Update (time)
if (Call( "GetIsPlayer" ) == 1 then 
    ...................
     engine script 
   ......................
end
end
But this caused the player controlled train's script is not usable either.
so could someone please point out where I was wrong in the script
or is there any good idea?
Thanks
Good evening,

You can further reduce script overhead in the AI case with the call "GetIsNearCamera" recently documented by DTG. When the camera is far (by how much, I don't know, but it looks to be more than enough in practice) the call returns 0 and the far away vehicles scripting can be avoided.

Also in some case, it can be beneficial to differenciate the trailing vehicles of a player train from the driven vehicle of a player train by using the GetIsEngineWithKey call.

You our Update() function would therefore look like this:

Code: Select all

function Update(time)
    local with_key = Call("GetIsEngineWithKey") == 1
    local player = (with_key and Call("GetIsPlayer") == 1)
    local is_near = Call("GetIsNearCamera") == 1

    if with_key and player then
        -- driven vehicle of a player train
    elseif player then
        -- trailing vehicle of a player train
    elseif is_near then
        -- pure AI vehicle near the player camera
    end
end
Depending on the situation, you can get a substential increase in performance by using "GetIsNearCamera". For instance on a london to brighton scenario with 30+ AI trains a was able to get a performance increase from 27 fps to 30 fps at scenario start just by using the GetIsNearcamera call on the Class 377 scripts.

Also avoiding unnecessary Call() can help a bit.

I hope this helps :)

Regards,
--
nschichan
clkgtr
Getting the hang of things now
Posts: 48
Joined: Sun Jul 15, 2012 3:00 am

Re: Engine script only used for player controlled trains

Post by clkgtr »

Many thanks,guys
That helps me a lot.
the FPS was 16 ,now 40+
Great!
Locked

Return to “[TS] Building Rolling Stock”