Page 1 of 2

How can I add lua modules in engine scripts?

PostPosted: Wed Aug 22, 2018 9:54 am
by TrabantDeLuxe
Hi there, in the interest of keeping things organised, I've been wanting to create libraries of more common functions that I use. Suppose for instance I have a speedometer that I want to give some fancyness. I have two .lua files, one being the engine script (we shall name in EngineScript.lua), the other being the library of functions for the speedometer (call it flaman.lua). Both files reside in the same folder in my main engine folder. I've used this tutorial for reference purposes.

The contents of flaman.lua are:
Code: Select all
local flaman = {}

   -- this is just to try things out.
   function flaman.update(speed)
      return 3.6*speed
   end

return flaman


The (relevant) contents of EngineScript.lua are:
Code: Select all
-- Load modules
flaman = require("flaman")

... So here's where initialise() and setup() happen but I can't be bothered to write pseudocode for that ...

-- Then do stuff in update()
function update(dTime)
    -- Query TS about how fast we're going.
    local speed = Call("GetSpeed")

    -- Let's do something with the speedometer!
    local TheSpeedToDisplay = flaman.update(speed)
end


However, this blows up. In fact, I am confident that it blows up at the line where the module is loaded. The daft thing is that I don't really see what I'm doing wrong here!

Re: How can I add lua modules in engine scripts?

PostPosted: Wed Aug 22, 2018 7:06 pm
by JamesLit
I can't say I know anywhere near enough about LUA to help you - other than that I can assure you that there are already a large number of advanced locos and units etc out there that use a modular system for their LUA with great success, even with a very large number of LUA scripts doing different jobs, some at least 10 I think! So it should definitely be possible to do this successfully, so do persevere!

Re: How can I add lua modules in engine scripts?

PostPosted: Wed Aug 22, 2018 8:56 pm
by TrabantDeLuxe
Thanks James. I'm fairly confident this is 1) not impossible, and 2) just me not doing proper syntax.

Re: How can I add lua modules in engine scripts?

PostPosted: Thu Aug 23, 2018 8:36 am
by AndiS
Maybe it does not like your parenthesis. The tutorial has
Code: Select all
formatter = require "printFormatter"

while you have
Code: Select all
flaman = require("flaman")

which would be ok for a function call but maybe require is special, syntactically.

I never used it with parenthesis. And I never had the patience to try their modules. But then my scripts look like COBOL.

Re: How can I add lua modules in engine scripts?

PostPosted: Thu Aug 23, 2018 11:10 am
by TrabantDeLuxe
Hi Andi,

With single-argument functions, such as Require(), the parenthesis are optional.

Code: Select all
-- These two are the exact same thing:
NiceFood = require("Potato")
NiceFood = require "Potato"


Recall from many languages that it is customary to write:
Code: Select all
print "Eat your potatoes!"


Without parenthesis.

Cobol huh? I spoke to a guy saying that there's a lot of cobol out there in desperate need of people who are able to write in it, but it's becoming a lost skill.

Cheers!

Re: How can I add lua modules in engine scripts?

PostPosted: Thu Aug 23, 2018 11:49 am
by AndiS
You see, I never learnt Lua properly. Life is too short for all the syntax creativity of a zillion language designers.
That is why I dearly hope that I will never be so short of money that I would have to unearth my distant memories of COBOL. SQL is bad enough and slightly younger and hard to avoid.

Re: How can I add lua modules in engine scripts?

PostPosted: Tue Aug 28, 2018 9:11 am
by VictoryWorks
I'm pretty sure "Require" doesn't work, DTG disabled it a while back.

Instead they implemented their own version as follows:

--include=..\BaseScripts\Generic Variables.lua
--include=..\BaseScripts\Control Manager.lua
etc.

It basically works by just bolting the "included" scripts onto the calling script making the functions available throughout.
It means you can re-use code easily and have different physical files to keep things in some kind of order and swap out easily. You can also do a basic function override by calling a function name in your main script and then having different mocde with that function depending on which script you choose to "include".

Re: How can I add lua modules in engine scripts?

PostPosted: Tue Aug 28, 2018 12:11 pm
by AndiS
I use require all the time, as in

require "Assets/AndiS/FPSignals/scripts/Shared by All.out"

but without messing with modules.

Note 1: Note the path starting in the railworks folder - could this be the reason why the original example did not work? I assumed the file to be in all possible places.

Note 2: I compile my files one by one because then I get reasonable messages from the compiler with line numbers that refer to the real file.


When I still used --include, line numbers always referred to the amalgamated temporary file that is created when the Blueprint Editor reads these lines.

Re: How can I add lua modules in engine scripts?

PostPosted: Tue Aug 28, 2018 12:32 pm
by VictoryWorks
Yeah I'm an idiot, of course Require works.. it's how you can add the Autocoach stuff we do to any loco! :oops:
I'm going to put that brain fart down to 2 weeks on holiday and just STFU :oops:

Re: How can I add lua modules in engine scripts?

PostPosted: Tue Aug 28, 2018 2:13 pm
by AndiS
Let's rename the thread "ageing scripters need breaks".
I hope that Trabant really just forgot the full path, then the club will be complete. :lol: