Modding TDD: Script.Load -vs- require
Wyzcrak
Pot Pie Aficionado Join Date: 2002-12-04 Member: 10447Forum Moderators, Constellation, NS2 Playtester, Squad Five Blue
in Modding
<div class="IPBDescription">How to load scripts from NS2 -and- test runner?</div>Scenario: I'm trying to create a server mod, and I'm trying to do it with test-driven development. I'm falling down when my lunity unit tests eventually execute actual NS2 Lua files.
Question: What same/shared script-loading syntax can I use to satisfy both the NS2 and test runner runtimes?
<!--sizeo:3--><span style="font-size:12pt;line-height:100%"><!--/sizeo-->Details:<!--sizec--></span><!--/sizec-->
I see two ways to load files at runtime: <b>Script.Load()</b> or <b>require</b>. Essentially, I'm running into errors no matter which I use.
<u><b>Script.Load(): test runner error</b></u>
The first line of my clientGreeter.lua looks like this:
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->Script.Load("path/to/script.lua")<!--c2--></div><!--ec2-->
When used in/from the actual NS2 game, it loads the script just fine (and goes on to greet the client -- yay!). But lunity returns this error at runtime:
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->------------------------- Run Tests -------------------------
lua: ..\ta\notifications\clientGreeter.lua:1: attempt to index global 'Script' (a nil value)
stack traceback:
..\ta\notifications\clientGreeter.lua:1: in main chunk
[C]: in function 'require'
test_clientGreeter.lua:1: in main chunk
[C]: ?<!--c2--></div><!--ec2-->
<u><b>require: NS2 runtime error</b></u>
If the first line of clientGreeter.lua uses Lua's <b>require</b> syntax instead:
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->require "path.to.script"<!--c2--></div><!--ec2-->
Then NS2's runtime complains:
<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'>Script Error #3: ta/notifications/clientGreeter.lua:4: module 'path.to.script' not found:
no field package.preload['path.to.script']
no file '.\path\to\script.lua'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\lua\path\to\script.lua'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\lua\path\to\script\init.lua'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\path\to\script.lua'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\path\to\script\init.lua'
no file 'C:\Program Files (x86)\Lua\5.1\lua\path\to\script.luac'
no file '.\path\to\script.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\path\to\script.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\loadall.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\clibs\path\to\script.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\clibs\loadall.dll'
no file '.\path\to\script51.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\path\to\script51.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\clibs\path\to\script51.dll'
no file '.\ta.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\ta.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\loadall.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\clibs\ta.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\clibs\loadall.dll'
no file '.\ta51.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\ta51.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\clibs\ta51.dll'
Call stack:
#1: require [C]:-1
#2: Load ta/notifications/clientGreeter.lua:4
path = "path.to.script"
#3: ta/notifications/clientGreeter.lua:7
#4: Load [C]:-1
#5: ta/events/clientConnected.lua:1
#6: Load [C]:-1
#7: ta/ServerInitialize.lua:1
#8: Load [C]:-1
#9: ta/Server.lua:2</div>
Question: What same/shared script-loading syntax can I use to satisfy both the NS2 and test runner runtimes?
<!--sizeo:3--><span style="font-size:12pt;line-height:100%"><!--/sizeo-->Details:<!--sizec--></span><!--/sizec-->
I see two ways to load files at runtime: <b>Script.Load()</b> or <b>require</b>. Essentially, I'm running into errors no matter which I use.
<u><b>Script.Load(): test runner error</b></u>
The first line of my clientGreeter.lua looks like this:
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->Script.Load("path/to/script.lua")<!--c2--></div><!--ec2-->
When used in/from the actual NS2 game, it loads the script just fine (and goes on to greet the client -- yay!). But lunity returns this error at runtime:
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->------------------------- Run Tests -------------------------
lua: ..\ta\notifications\clientGreeter.lua:1: attempt to index global 'Script' (a nil value)
stack traceback:
..\ta\notifications\clientGreeter.lua:1: in main chunk
[C]: in function 'require'
test_clientGreeter.lua:1: in main chunk
[C]: ?<!--c2--></div><!--ec2-->
<u><b>require: NS2 runtime error</b></u>
If the first line of clientGreeter.lua uses Lua's <b>require</b> syntax instead:
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->require "path.to.script"<!--c2--></div><!--ec2-->
Then NS2's runtime complains:
<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'>Script Error #3: ta/notifications/clientGreeter.lua:4: module 'path.to.script' not found:
no field package.preload['path.to.script']
no file '.\path\to\script.lua'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\lua\path\to\script.lua'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\lua\path\to\script\init.lua'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\path\to\script.lua'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\path\to\script\init.lua'
no file 'C:\Program Files (x86)\Lua\5.1\lua\path\to\script.luac'
no file '.\path\to\script.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\path\to\script.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\loadall.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\clibs\path\to\script.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\clibs\loadall.dll'
no file '.\path\to\script51.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\path\to\script51.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\clibs\path\to\script51.dll'
no file '.\ta.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\ta.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\loadall.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\clibs\ta.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\clibs\loadall.dll'
no file '.\ta51.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\ta51.dll'
no file 'C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\clibs\ta51.dll'
Call stack:
#1: require [C]:-1
#2: Load ta/notifications/clientGreeter.lua:4
path = "path.to.script"
#3: ta/notifications/clientGreeter.lua:7
#4: Load [C]:-1
#5: ta/events/clientConnected.lua:1
#6: Load [C]:-1
#7: ta/ServerInitialize.lua:1
#8: Load [C]:-1
#9: ta/Server.lua:2</div>
Comments
Lunity obviously reads '/' as some sort of command as it fails with what comes after it. Either you need to modify Lunity's handling of Lua code, or you need to write your own auto tester.
Require does not work in NS2, you have to use the format Script.Load().
NS2 runs on custom LUA, especially crafted by Max :)
Not only do you need to run lunity under Rocket, UWE's custom lua implementation, you need to have lunity run from <b>in game</b>.
The reason for this is Script.Load and a bunch of other functions (stuff under Shared.*, etc) are provided by the Spark Engine itself. So have your mod run lunity or make a seperate mod that runs it.