I am just starting writing scripts for Foldit. I have no experience of Lua, but I have been a professional programmer for many years, so my status is "noob with insight". In my first script, I want to print out contactMap data. I have copied and pasted the functions from http://foldit.wikia.com/wiki/Foldit_Lua_Functions. The error I get is:
*** attempt to index global 'contactmap' (a nil value) ***
Here is my code:
segmentCount = get_segment_count() print ("Segment count: " .. segmentCount) for source = 1, segmentCount do for target = source + 1, segmentCount do inContact = contactmap.IsContact(source, target) heat = contactmap.GetHeat(source, target) print("Segments "..source..", ".. target..": in contact = "..inContact..", heat = "..heat) end end
Thanks in advance for any enlightenment.
Brian
I'd look for recipes like the following and see if their code gives any insight.
Band All Contacts Adaptive v2.2
http://fold.it/portal/recipe/101238
Maybe somewhere else in your code you used a line like:
contactmap={}
so that contactmap is now a local variable instead of a Foldit command.
My code is exactly what you see in my initial message. Nowhere do I redefine contactmap. If I change the first line to...
segmentCount = structure.GetCount()
... then I get the error
*** attempt to index global 'structure' (a nil value) ***
This would imply either that:
- My script has no access to the global objects, and that I need to declare them before I use them
- The version of foldit that I downloaded three days ago is not version 2, and does not know anything about these global objects
Is there a way of displaying the current version of foldit?
The problem is that I had originally loaded a V1 script and made changes to that.
I looked at Band All Contacts Adaptive v2.2 and did a slash and burn to get to the script that I had first imagined:
-- segmentCount = get_segment_count() -- now causes an error segmentCount = structure.GetCount() print ("Segment count: " .. segmentCount) for source = 1, segmentCount do for target = source + 1, segmentCount do inContact = contactmap.IsContact(source, target) heat = contactmap.GetHeat(source, target) print("Segments "..source..", ".. target..": heat = "..heat) end end
This runs fine, with no errors, because the original script that I was modifying was already V2.
On the top of the Editor's windows, the version of Lua is written (V1 or V2). A V1 script does not recognize V2 commands and reverse. Any "new" recipe will now be edited in V2. If you want to script in V1, you have to take an old V1 recipe and "save as " it to your new one with V1 commands. To code in V2, simply open a new window in the editor.
The currently available Lua functions in foldit can be found at http://foldit.wikia.com/wiki/Foldit_Lua_Functions
Instead of this line (which I think is for LUA V1):
segmentCount = get_segment_count()
I would use this line (which I think is for LUA V2):
segmentCount = structure.GetCount()
Using an editor with line numbers helps decipher the error messages Foldit gives.
On what line number did the error occur? What is the code on that line?
Also, I'd like for Recipes that do similar things and imitate or adapt their code.
This is a good way to learn the syntax for various LUA and Foldit functions.