require function

LDiCesare

Deity
Joined
Dec 22, 2005
Messages
2,612
Location
France
"require" is one of the most commonly used functions in lua.
It's like #include in C.
However, it appears that if you want to see a map script in the world builder or Civ V proper, it must not contain, at the time that the list of possible scripts is created, the line
require "math".
Possibly true of other packages too.
If you delete that line and add it later, it works fine (the scripts are parsed again when you change them).
But you can't use require.
You can, however, use include instead, which is a nonstandard and unknown method that probably does the same thing.

I suppose that, maybe for some weird reason, at the moment the script files are scanned, they are loaded, and at that time the lua runtime isn't totally set up so that require "math" doesn't work. Why didn't they replace _G["require"] with their own include function so that this issue wouldn't be seen by the end user I have no idea.

So be warned. If you're used to lua, don't use "require" when scripting lua for CivV, it will make your script unavailable.
You can probably work around it... If the script is loaded after the launch screens, it might even work.

Overall, this is a very good way to cause people to lose a few hours, thank you Firaxis.
 
Are you sure that require isn't failing because the Lua library paths aren't setup yet? Do you see an error message? Can you post it?
 
I see nothing. Which is part of the problem.
I am loading the game, or the worldbuilder. I click to see the map scripts and can't see mine.
That is the total output I get from the game.
I looked in one log file and found nothing. There may be other debug/console outputs hidden somewhere but I couldn't find them.
In the end, I lost a few hourS because of that, and just wanted to warn others not to use the most useful lua function because it may not work, but use the custom include function instead.
 
I'm having issue with the require, and include functions as well. I have a theory that the global functions are not being passed into the new environments. Alas I am not well versed in lua so I am not sure how to check for this or not.
 
Double Post! Progress!

Based on the lua book
Code:
for n in pairs(_G) do print(n) end
will print everything in the global table which is stored in _G. "require" is in the environment Main State.

However, when I switch to any other environment, including the one for my script, _G does not exist. It is nil.

The trick is either finding a way into the main state on creation to pass _G to all other states, find a way for the script to pull on _G from Main State, or somehow have the script temporarily switch to the main state, pass _G to the proper environment, and switch back.

I'm in the chatroom hacking away at this if anyone is interested in helping me out with this.
 
Top Bottom