Scripts / LUA package.path and require

Author
Message
tomjscott
User Banned
Posted: 15th May 2014 18:47
I've been trying to get this to work since last night and nothing has been successful. I want to have my own file called myglobal.lua with all my game's global functions and variables and then just include this file in the existing global.lua file. However, it always complains on the package.path code about it being nil. I was able to determine that the current working directory when executing a LUA script is the Files directory. So, with my script in the scriptbank folder, I have written the code as follows:







I've tried half a dozen other variations on the path, but all with the same result. Has anyone done this successfully?

System Specs: OS - Windows 7 Home Premium 64-bit SP1, CPU - AMD Phenom II X4 945, 3.0Ghz, RAM - 8Gb DDR3, GFX Card - 2048MB NVIDIA GeForce GT 640, FPSC-R Version - Beta 1.006
BarZaTTacKS
10
Years of Service
User Offline
Joined: 3rd Feb 2014
Location:
Posted: 15th May 2014 19:09 Edited at: 17th May 2014 19:35
require "scriptbank/myfile.lua"



Credit goes to @Avram for posting the readme in his script pack. I cannot attach it for some reason.



Link

http://fpscrforum.thegamecreators.com/?m=forum_view&t=208339&b=2



Additional thanks to @AUShadow, @smallg
PM
tomjscott
User Banned
Posted: 15th May 2014 19:10 Edited at: 15th May 2014 19:11
Hmmm. I was sure I tried that one already, but maybe not. I'll give it a try tonight. Thanks.

System Specs: OS - Windows 7 Home Premium 64-bit SP1, CPU - AMD Phenom II X4 945, 3.0Ghz, RAM - 8Gb DDR3, GFX Card - 2048MB NVIDIA GeForce GT 640, FPSC-R Version - Beta 1.006
AuShadow
GameGuru TGC Backer
11
Years of Service
User Offline
Joined: 2nd Dec 2013
Location: Australia
Posted: 15th May 2014 19:15
Lol credit actually to either smallg or Avram as they brought it up in another post I just copied it as I thought was a good idea

PC Specs: Windows 7 home 64-bit, Amd 7900 3gb DDR5 graphics, 8gb DDR3 Ram, Intel i7 3.4ghz

Feel free to visit and edit the public FPSCR resource wiki page: http://fpscrresource.wikispaces.com/home
tomjscott
User Banned
Posted: 16th May 2014 03:12
It works. Thanks again.

System Specs: OS - Windows 7 Home Premium 64-bit SP1, CPU - AMD Phenom II X4 945, 3.0Ghz, RAM - 8Gb DDR3, GFX Card - 2048MB NVIDIA GeForce GT 640, FPSC-R Version - Beta 1.006
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 17th May 2014 19:26
Avram (as far as i'm aware) was the first person to post such a command because it's required for his addon and it's good to see you are using a seperate globals as the original will be overwritten next update

life's one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, geforce gtx460, directx 11
AuShadow
GameGuru TGC Backer
11
Years of Service
User Offline
Joined: 2nd Dec 2013
Location: Australia
Posted: 29th May 2014 13:23
Yeah although I've actually gone back to putting them into global and just keeping a back of them as I started having errors with require myself might have another look tomorrow tho

PC Specs: Windows 7 home 64-bit, Amd 7900 3gb DDR5 graphics, 8gb DDR3 Ram, Intel i7 3.4ghz

Feel free to visit and edit the public FPSCR resource wiki page: http://fpscrresource.wikispaces.com/home
Avram
GameGuru TGC Backer
18
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 29th May 2014 16:45 Edited at: 29th May 2014 16:48
Something has changed regarding LUA paths and a comment from TGC would be helpful.



Up to (and including) version 1.006 the working directory for LUA was GAMEDIR/Files, or in my case "c:\Program Files (x86)\The Game Creators\FPS Creator Reloaded\Files\" so we could easily require a script from "scriptbank/filename.lua". However, from 1.007, the working directory has changed, and require statement now gives an error that the file can not be found at: c:\Program Files (x86)\The Game Creators\FPS Creator Reloaded\Files\lua\scriptbank\filename.lua



I don't know what this directory "lua" is used for but it doesn't exist on my system. I have tried to specify relative path, like require "../scriptbank/filename.lua" but no luck (same error appears).

tomjscott
User Banned
Posted: 29th May 2014 20:27
Yep, it's broken for me too. I took a few minutes to try to run my level in 1.007 and quickly found it complaining about my global lua file that I was including via require. I didn't have time to try to fix it so I just gave up and went back to my Unity project. Maybe I'll try it again in 1.008 whenever that comes out.

System Specs: OS - Windows 7 Home Premium 64-bit SP1, CPU - AMD Phenom II X4 945, 3.0Ghz, RAM - 8Gb DDR3, GFX Card - 2048MB NVIDIA GeForce GT 640, FPSC-R Version - Beta 1.006
DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 29th May 2014 21:57
Scripting has changed in 1.7 it seems. I had several hours of head scratching, not being the most experienced lua coder. Instead of using require you now put your globals at the start of your script. Actually easier once you realize! No need for a separate file or to change the globals at all. I was about to post to let anyone know who already doesn't!



SPECS: Q6600 CPU. Nvidia 260GTX. 8 Gig Memory. Win 7.
tomjscott
User Banned
Posted: 29th May 2014 22:13
Quote: " Instead of using require you now put your globals at the start of your script. Actually easier once you realize! No need for a separate file or to change the globals at all. "




Can you be more specific? I don't see how putting my globals at the start of a script is easier. I need my globals to be in a single place and accessible from all scripts. Maybe I'm not getting what you're saying.

System Specs: OS - Windows 7 Home Premium 64-bit SP1, CPU - AMD Phenom II X4 945, 3.0Ghz, RAM - 8Gb DDR3, GFX Card - 2048MB NVIDIA GeForce GT 640, FPSC-R Version - Beta 1.006
DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 29th May 2014 22:57
Instead of putting your globals in the global file or a standalone one, you put them at the start of your script. I've not tried multiple scripts using them as yet, but I imagine once you have them going any script can grab the variables. I was having serious multiple errors all over trying to use require.

A really quick example.

started=0

function example_main(e)

if started==0 then

started=1

end

if started>0 then

started=started+1

Prompt(started)

end

end



A fairly useless script, but I just made it up to give you a basic example. I'm sure require will be usable again at some point, but for now this is the alternative.



SPECS: Q6600 CPU. Nvidia 260GTX. 8 Gig Memory. Win 7.
tomjscott
User Banned
Posted: 29th May 2014 22:59
That defeats the whole purpose for having globals. If anything, I'll just put mine back in the main global.lua file for now.

System Specs: OS - Windows 7 Home Premium 64-bit SP1, CPU - AMD Phenom II X4 945, 3.0Ghz, RAM - 8Gb DDR3, GFX Card - 2048MB NVIDIA GeForce GT 640, FPSC-R Version - Beta 1.006
DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 29th May 2014 23:12
Yes, I know. I was a bit thrown when Lee recommended this to me. It works for what I have been playing with so far. As I said I have not tested this beyond a single script, so it didn't really matter that the variables were global or not. Once I spend more time messing I will soon discover if there is an issue with other scripts grabbing the data. I just find it easier having the data in your basic script when adjusting the data often to get a different result in my script, rather than switching between files.



SPECS: Q6600 CPU. Nvidia 260GTX. 8 Gig Memory. Win 7.
Avram
GameGuru TGC Backer
18
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 30th May 2014 01:06
How can we make custom (helper) functions and share them between scripts? Seems impossible.

tomjscott
User Banned
Posted: 30th May 2014 01:15
I suspect that the working directory has just changed, which would break the require reference. I don't have time to do it right now, but you can run the following



os.execute("echo %CD%");



That should tell you what the new working directory is. I don't think they could have broken basic LUA functionality so I don't really think the require command is broken, but just that we don't know where we are running from anymore.

System Specs: OS - Windows 7 Home Premium 64-bit SP1, CPU - AMD Phenom II X4 945, 3.0Ghz, RAM - 8Gb DDR3, GFX Card - 2048MB NVIDIA GeForce GT 640, FPSC-R Version - Beta 1.006
tomjscott
User Banned
Posted: 30th May 2014 01:19
You could also try calling os.getenv(varname) and then print the value to a prompt. I haven't tried that one either, but it might be cleaner to implement.

System Specs: OS - Windows 7 Home Premium 64-bit SP1, CPU - AMD Phenom II X4 945, 3.0Ghz, RAM - 8Gb DDR3, GFX Card - 2048MB NVIDIA GeForce GT 640, FPSC-R Version - Beta 1.006
DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 30th May 2014 02:02
You could also try calling os.getenv(varname) and then print the value to a prompt.



Sounds interesting, still very new with Lua myself! Interesting to see if it is that simple! If it works let me know! More detailed answers could be given by TGC I imagine. I am by no means a Lua expert as I have stated before. Just repeating advice I was given to get things working. I noticed the documentation for Lua scripting has not changed at all since the update, despite many new commands and as talked about here, changes to how things seem to work in general. I'm sure if it were a simple directory problem I would have been told when I asked about this, but possibly not of course, just the simplest way to rectify it. More official info is needed!

I do know that in prior versions of Reloaded globals never reset when tested more than once. Perhaps this is part of a rewrite in this area? I know it was not a simple fix from a mail off Lee. I can be tenacious when I notice obvious bugs I'm fairly sure TGC are rewriting the Lua>DB system completely because of the limitations of the prior software.

I'm not saying require isn't needed, but at the moment it seems to be unsupported. Unless you prove me wrong of course! When I try making scripts that require global variables or arrays I'm sure I'll find if it doesn't work from one main script!



SPECS: Q6600 CPU. Nvidia 260GTX. 8 Gig Memory. Win 7.
AuShadow
GameGuru TGC Backer
11
Years of Service
User Offline
Joined: 2nd Dec 2013
Location: Australia
Posted: 30th May 2014 03:19
I was under the impression that when variables where entered into your script file they were LOCAL and not GLOBAL meaning that only that script could access them which i can see use for and am using them as such so that multiple scripts could have a variable that was named the same but they couldn't pull it from a different script as they may need to have different values, if this is no longer the case i have some scripts i am going to need to change now so that my global list of variables is longer ,



I am actually hoping we can still just have LOCAL's however

PC Specs: Windows 7 home 64-bit, Amd 7900 3gb DDR5 graphics, 8gb DDR3 Ram, Intel i7 3.4ghz

Feel free to visit and edit the public FPSCR resource wiki page: http://fpscrresource.wikispaces.com/home
tomjscott
User Banned
Posted: 30th May 2014 15:20
OK, so the require functionality isn't broken and the path for the working directory is still Files. It looks like they fixed this more in line with what it should have been doing in the first place. Here is the solution:







Note the use of your global file name without the .lua after it. The package.path takes care of tacking on the .lua.

System Specs: OS - Windows 7 Home Premium 64-bit SP1, CPU - AMD Phenom II X4 945, 3.0Ghz, RAM - 8Gb DDR3, GFX Card - 2048MB NVIDIA GeForce GT 640, FPSC-R Version - Beta 1.006
RustyNuts
GameGuru TGC Backer
13
Years of Service
User Offline
Joined: 9th Nov 2011
Location: Warwickshire Great Britain
Posted: 3rd Jun 2014 22:42
I knew I'd seen this in one of Lee's blogs
Quote: "Include ("ai_cover.lua") - Use within the init function to ensure the script is pre loaded"

and I've just read it in the newsletter. It's the standard call for header files in c and c++ program's.

All programmers are playwrights and all computers are lousy actors.
PM

Login to post a reply

Server time is: 2024-12-22 05:10:56
Your offset time is: 2024-12-22 05:10:56