How can I add lua modules in engine scripts?

How can I add lua modules in engine scripts?

Postby TrabantDeLuxe » Wed Aug 22, 2018 9:54 am

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!
TrabantDeLuxe
Passed Fireman
 
Posts: 247
Images: 7
Joined: Mon Mar 21, 2016 10:10 pm
Location: Delft, NL
Has thanked: 176 times
Been thanked: 263 times

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

Postby JamesLit » Wed Aug 22, 2018 7:06 pm

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!
The Forge Simulation | Like us on Facebook!
Owner & Director | Content built with care, not compromises.
User avatar
JamesLit
Driver
 
Posts: 370
Images: 26
Joined: Mon Apr 07, 2014 3:26 pm
Location: Kent
Has thanked: 433 times
Been thanked: 141 times

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

Postby TrabantDeLuxe » Wed Aug 22, 2018 8:56 pm

Thanks James. I'm fairly confident this is 1) not impossible, and 2) just me not doing proper syntax.
TrabantDeLuxe
Passed Fireman
 
Posts: 247
Images: 7
Joined: Mon Mar 21, 2016 10:10 pm
Location: Delft, NL
Has thanked: 176 times
Been thanked: 263 times

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

Postby AndiS » Thu Aug 23, 2018 8:36 am

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.
AndiS
Top Link Driver!
 
Posts: 736
Joined: Wed Apr 09, 2014 5:48 pm
Has thanked: 268 times
Been thanked: 308 times

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

Postby TrabantDeLuxe » Thu Aug 23, 2018 11:10 am

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!
TrabantDeLuxe
Passed Fireman
 
Posts: 247
Images: 7
Joined: Mon Mar 21, 2016 10:10 pm
Location: Delft, NL
Has thanked: 176 times
Been thanked: 263 times

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

Postby AndiS » Thu Aug 23, 2018 11:49 am

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.
AndiS
Top Link Driver!
 
Posts: 736
Joined: Wed Apr 09, 2014 5:48 pm
Has thanked: 268 times
Been thanked: 308 times

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

Postby VictoryWorks » Tue Aug 28, 2018 9:11 am

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".
User avatar
VictoryWorks
Driver
 
Posts: 333
Joined: Mon Apr 07, 2014 1:22 pm
Has thanked: 41 times
Been thanked: 224 times

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

Postby AndiS » Tue Aug 28, 2018 12:11 pm

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.
AndiS
Top Link Driver!
 
Posts: 736
Joined: Wed Apr 09, 2014 5:48 pm
Has thanked: 268 times
Been thanked: 308 times

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

Postby VictoryWorks » Tue Aug 28, 2018 12:32 pm

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:
User avatar
VictoryWorks
Driver
 
Posts: 333
Joined: Mon Apr 07, 2014 1:22 pm
Has thanked: 41 times
Been thanked: 224 times

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

Postby AndiS » Tue Aug 28, 2018 2:13 pm

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:
AndiS
Top Link Driver!
 
Posts: 736
Joined: Wed Apr 09, 2014 5:48 pm
Has thanked: 268 times
Been thanked: 308 times

Next

Return to Rolling Stock General

Who is online

Users browsing this forum: No registered users and 1 guest

cron