Debugging problems during mod-loading phase

Place to get help with not working mods / modding interface.
Post Reply
hoho
Filter Inserter
Filter Inserter
Posts: 677
Joined: Sat Jan 18, 2014 11:23 am
Contact:

Debugging problems during mod-loading phase

Post by hoho »

Being an absolute noob when it comes to Lua I tend to make a whole lot of trivial errors all the time. I do use an external lua interpreter to catch simple syntax errors but occasionally some problems only occur depending on specific input data. When that happens during load-time I only get a non-descript error message (e.g "attempt to perform arithmetic on field '?' (a nil value)").

Is there a way to write a log file or to get some sort of information out of the mod script during load-time? I know 0.10 added some basic file manipulation things but I can't seem to find any information on it at all. Any sort of information would be greatly appreciated :)

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Debugging problems during mod-loading phase

Post by FreeER »

hoho wrote:Is there a way to write a log file or to get some sort of information out of the mod script
game.makefile is shown at the bottom of this post by drs9999 so you could use it, though it seems to overwrite the file each time with no (apparent) option to allow appending so you'd somehow need to create a new log each time the code is ran (assuming you'd like a history. Its also, as far as I know, not possible to read that data back so you couldn't read it and append the new data and then write it all back out)...

Personally I just use serpent.block print statements, above the code where it says it's getting a nil value, to show each of the variables that are being used (and comment out the 'offending' code so that I can actually see the debug statements), once I know exactly which variable was coming in as nil it's usually relatively easy to trace it back to where it should have been assigned a value and find out why it wasn't :)

edit: added new 0.10 game methods to wiki's Lua/Game object (by essentially copying drs9999's post for those methods onto the wiki)

hoho
Filter Inserter
Filter Inserter
Posts: 677
Joined: Sat Jan 18, 2014 11:23 am
Contact:

Re: Debugging problems during mod-loading phase

Post by hoho »

When I tried using the game.createfile thingy it complained "attempt to index global 'game' (a nil value). Would that be because the object doesn't exist when mod is being loaded or the sample code isn't meant to work when copy-pasted? Do I need to include/import something? Is it only meant to work player is already in game?
FreeER wrote:Personally I just use serpent.block print statements, above the code where it says it's getting a nil value, to show each of the variables that are being used (and comment out the 'offending' code so that I can actually see the debug statements), once I know exactly which variable was coming in as nil it's usually relatively easy to trace it back to where it should have been assigned a value and find out why it wasn't :)
That's essentially exactly what I'm trying to do but not being able to actually print stuff somewhere while game is still being loaded it's kind of hard :)

[edit]

Apparently game.createfile doesn't work when it's inside data.lua but works just fine inside control.lua.
Only problem is, for some reason I can't do this inside control.lua:

Code: Select all

local tmp = serpent.block(data.raw.technology)
game.makefile("foo.txt",tmp)
It is giving me "attempt to index global 'data' (a nil value)" when I generate a new map.

[edit2]
Looking around the wiki it seems as I can access data.raw only from within data.lua and not from within control.lua. If this is so and if game.makefile is not accessible inside data.lua then I'm all out of options for half-decent debugging it seems.

Could I somehow make a copy of the data.raw so I could access it later from control.lua and dump it's contents so I could have a look at the data and figure out what exactly is failing?
Last edited by hoho on Tue Jul 01, 2014 8:49 pm, edited 1 time in total.

Colombo
Long Handed Inserter
Long Handed Inserter
Posts: 76
Joined: Mon May 19, 2014 11:25 am
Contact:

Re: Debugging problems during mod-loading phase

Post by Colombo »

hoho: with 10.1, there were bug repaired and when you run factorio from console, all errors are properly logged.

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Debugging problems during mod-loading phase

Post by FreeER »

Colombo wrote:hoho: with 10.1, there were bug repaired and when you run factorio from console, all errors are properly logged.
This fails to work for me (on windows with cmd and powershell ...yes, I know neither is a proper console lol)
hoho wrote:Apparently game.createfile doesn't work when it's inside data.lua but works just fine inside control.lua. [snip] Looking around the wiki it seems as I can access data.raw only from within data.lua and not from within control.lua.
Both of these are true, the game object does not exist until ingame and data does not exist ingame (it's split into the proper locations of game.force.recipes/technologies etc., but it's only partially accessible). From the error I was assuming that you were working from the control.lua (data.lua, as far as I can remember, has always given me the name of the nil variable like it did with game)... Sorry for the assumption there. No way to write data from data.lua to a file since the lua io (and os) library are not available.
hoho wrote:Could I somehow make a copy of the data.raw so I could access it later from control.lua
The data.raw is just the prototypes added by the base game (and mods) via data:extend so you don't need to dump it from control.lua, just look at the files. It's layout has been created on the wiki here (essentially each type has it's own index which is then indexed by the name of the prototypes). If you needed to actually use some of that data from the control.lua then first call .help() on one to see what data they contain (ex. game.player.force.recipes["iron-plate"].help(), ingame. if you do it in the control.lua you'd need to print it out yourself or pass it to makefile), if it's not there then in the belt-replacer mod that I'm working on I created recipes (that will never be unlocked) with the information I needed as the name of the recipe (the recipe names can be accessed quite easily in the control.lua, at which point I can split the name with string.find or string.match into the various pieces of info that I'd placed into the name)...be aware that the names can not contain a '.' (and possibly a few other things) and Factorio responds with a somewhat cryptic message about a 'note' recipe not having a name lol (I needed the belt speeds so to remove the '.' I multiplied the speed by 1000000, if it was something else I probably could have used string.gsub to replace the '.' with some other string of characters that I could reliably convert back in control.lua)

why not post the code and let one of us see where the problem is being caused if you can't find it? (it seems obvious enough that somewhere you are doing math, intentionally or not, on a variable that doesn't have an actual value, either because it wasn't given one or because it's not being accessed properly (ie misspelled)), the error should give the line number so that helps narrow it down further if you are using math in multiple places :)

hoho
Filter Inserter
Filter Inserter
Posts: 677
Joined: Sat Jan 18, 2014 11:23 am
Contact:

Re: Debugging problems during mod-loading phase

Post by hoho »

FreeER wrote:
Colombo wrote:hoho: with 10.1, there were bug repaired and when you run factorio from console, all errors are properly logged.
This fails to work for me (on windows with cmd and powershell ...yes, I know neither is a proper console lol)
It also failied when I ran it from bash and even DebugView didn't show anything.
FreeER wrote:
hoho wrote:Apparently game.createfile doesn't work when it's inside data.lua but works just fine inside control.lua. [snip] Looking around the wiki it seems as I can access data.raw only from within data.lua and not from within control.lua.
Both of these are true, the game object does not exist until ingame and data does not exist ingame (it's split into the proper locations of game.force.recipes/technologies etc., but it's only partially accessible).
Does this also mean that if a mod changes things in data.raw these WILL be active no matter if the mod is turned on in the mods menu/config? That's what I saw happening, at least. I would consider it a bug as then the code in data.lua will be run no matter what and if a person turns the mod off it still can change the game.
FreeER wrote:From the error I was assuming that you were working from the control.lua (data.lua, as far as I can remember, has always given me the name of the nil variable like it did with game)... Sorry for the assumption there. No way to write data from data.lua to a file since the lua io (and os) library are not available.
That's what I thought. Would be nice to have a way to log stuff out somehow from there, though. Once the game is up and running showing popups/printing to lua console is sort of good enough.

What about allowing some sort of basic write-to-stderr in case when the game is launched with --debug or something similar?
FreeER wrote:
hoho wrote:Could I somehow make a copy of the data.raw so I could access it later from control.lua
The data.raw is just the prototypes added by the base game (and mods) via data:extend so you don't need to dump it from control.lua, just look at the files.
I know but when I need to have a look at a ton of stuff not only from the base game but also mods it makes it quite hard to parse the massive amount of text just to find the things that I was interested in :)
FreeER wrote:why not post the code and let one of us see where the problem is being caused if you can't find it?
I'll be posting my mod (makes nearly all research harder) as soon as I figure out how to get my offline git repo to github as I was finally able to figure out the problem :)

Good thing was that it wasn't quite my problem in sense that I wasn't a typo or misusing Lua in general. Just Dysoch has some weird science pack usage in some of his tech that I wasn't thinking anyone would use. It was a special case of data that I wasn't handling properly.

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Debugging problems during mod-loading phase

Post by FreeER »

hoho wrote:Does this also mean that if a mod changes things in data.raw these WILL be active no matter if the mod is turned on in the mods menu/config? That's what I saw happening, at least. I would consider it a bug as then the code in data.lua will be run no matter what and if a person turns the mod off it still can change the game.
If this is happening then it is definitely a bug, as long as you restarted Factorio after disabling the mod (mod status isn't changed until Factorio is closed, of course Factorio crashing will fail to save the changes properly as well)...it's possible that data changes in a save would not be reversed when a mod is disabled without a player manually running commands (eg. game.force.resetrecipes()) but I would have thought that Factorio would handle that.
hoho wrote:What about allowing some sort of basic write-to-stderr in case when the game is launched with --debug or something similar?
That would be nice :)
hoho wrote:I know but when I need to have a look at a ton of stuff not only from the base game but also mods it makes it quite hard to parse the massive amount of text just to find the things that I was interested in :)
hm, I usually open all the files in notepad++ and use the 'find all in all opened documents' option, so not a major problem for me :)
hoho wrote:I'll be posting my mod (makes nearly all research harder) as soon as I figure out how to get my offline git repo to github as I was finally able to figure out the problem :)
create a github repo (if you haven't already) and when you do it gives the commands to run to connect it, though it usually says to add the origin remote and in my experience pre made local repos already have an origin remote (and of course you can't have two remotes of the same name, nor can you delete the 'last' remote if I recall correctly) and I have to instead use git remote set-url origin <url-from-github> instead, then you just use git push origin master -u (-u, from my understanding sets it as the primary upstream repo so you can then just use git push) to 'sync', assuming you've commited your work already :)
hoho wrote:Just Dysoch has some weird science pack usage in some of his tech that I wasn't thinking anyone would use. It was a special case of data that I wasn't handling properly.
Oh? would that be related to the use of two science-pack-twos? I think I remember a post in his subforum about that :lol:

hoho
Filter Inserter
Filter Inserter
Posts: 677
Joined: Sat Jan 18, 2014 11:23 am
Contact:

Re: Debugging problems during mod-loading phase

Post by hoho »

Here is the promised mod-post: https://forums.factorio.com/forum/vie ... =32&t=4660
As said, it's REALLY barebones, ugly and doesn't handle some things quite well.
FreeER wrote:If this is happening then it is definitely a bug, as long as you restarted Factorio after disabling the mod (mod status isn't changed until Factorio is closed, of course Factorio crashing will fail to save the changes properly as well)...it's possible that data changes in a save would not be reversed when a mod is disabled without a player manually running commands (eg. game.force.resetrecipes()) but I would have thought that Factorio would handle that.
I'll probably have a better look at it tomorrow, it could have been that I didn't close Factorio quite properly.
FreeER wrote:hm, I usually open all the files in notepad++ and use the 'find all in all opened documents' option, so not a major problem for me :)
I use the "find from files on this specific path" a LOT and it saves me from opening dozens of files. Though I m still not sure if I could have found the issue in reasonable amount of time had I looked for it with such grepping.
FreeER wrote:
hoho wrote:I'll be posting my mod (makes nearly all research harder) as soon as I figure out how to get my offline git repo to github as I was finally able to figure out the problem :)
create a github repo (if you haven't already) and when you do it gives the commands to run to connect it, though it usually says to add the origin remote and in my experience pre made local repos already have an origin remote (and of course you can't have two remotes of the same name, nor can you delete the 'last' remote if I recall correctly) and I have to instead use git remote set-url origin <url-from-github> instead, then you just use git push origin master -u (-u, from my understanding sets it as the primary upstream repo so you can then just use git push) to 'sync', assuming you've commited your work already :)
Thanks, I'll probably test this some times later. For now I thought saving the commit history of 3 commits wasn't crucial enough so I just made a new repo instead.
FreeER wrote:
hoho wrote:Just Dysoch has some weird science pack usage in some of his tech that I wasn't thinking anyone would use. It was a special case of data that I wasn't handling properly.
Oh? would that be related to the use of two science-pack-twos? I think I remember a post in his subforum about that :lol:
Yep, those were the same recipes :)

I basically put "if XXX == nil" nearly everywhere and if nil was encountered in a place where it shouldn't have I set various parameters of the research into magic number so I could see what was I triggered. In the end all I had to do was to visually scan through the research to see if some numbers were those magic numbers to figure out what was going on.

Post Reply

Return to “Modding help”