Lua scripting question

Case number:845813-2010389
Topic:Game: Tools
Opened by:Bruno Kestemont
Status:Open
Type:Question
Opened on:Tuesday, September 22, 2020 - 16:25
Last modified:Wednesday, September 23, 2020 - 19:31

Does anybody know why the following script by spvincent seems to work:

seg_backbone_frozen, seg_sidechain_frozen = freeze.IsFrozen ( integer segmentIndex )--Returns whether the backbone and/or sidechain is frozen.

when backbone is frozen, seg_backbone_frozen is true

when sidechain is frozen, seg_sidechain_frozen is true

If I do:
seg_frozen= freeze.IsFrozen ( integer segmentIndex )

seg_frozen is true if sidechain OR backbone are frozen.

??

(Tue, 09/22/2020 - 16:25  |  4 comments)


spvincent's picture
User offline. Last seen 4 hours 27 min ago. Offline
Joined: 12/07/2007
Groups: Contenders

Lua is a bit unusual in that a function can return multiple values, as freeze.IsFrozen() does. I'm not sure what the defined behaviour is when a function returns multiple values but they are assigned to a single variable: nothing good I imagine.

LociOiling's picture
User offline. Last seen 25 min 27 sec ago. Offline
Joined: 12/27/2012
Groups: Beta Folders

The return values are positional. For freeze.IsFrozen, the first return value indicates whether the backbone is frozen, and the second return value indicates whether the sidechain is frozen.

The called function (freeze.IsFrozen in this case) isn't aware of how many return values the caller has supplied. It always supplies the same two return values, regardless of whether the caller is paying attention.

Multiple Results in Programming in Lua has various examples of the rules for multiple return values.

The following code demonstrates freeze.IsFrozen. The segment to freeze, FRZ, was selected for puzzle 1892.

About the only limitation is that there doesn't seem to be a way to omit return values. So if you want to see whether the sidechain is frozen, you must also supply a return value for the backbone, even if you then ignore it.

FRZ = 182

print ( "--" )
print ( "test 1 - freeze backbone" )
freeze.UnfreezeAll ()
freeze.Freeze ( FRZ, true, false )
bb, sc = freeze.IsFrozen ( FRZ )
print ( "froze backbone segment " .. FRZ )
print ( "two return values" )
print ( "backbone frozen = " .. tostring ( bb ) )
print ( "sidechain frozen = " .. tostring ( sc ) )
print ( "one return value" )
bb2 = freeze.IsFrozen ( FRZ )
print ( "backbone frozen = " .. tostring ( bb2 ) )


print ( "--" )
print ( "test 2 - freeze sidechain" )
freeze.UnfreezeAll ()
freeze.Freeze ( FRZ, false, true )
bb, sc = freeze.IsFrozen ( FRZ )
print ( "froze sidechain segment " .. FRZ )
print ( "two return values" )
print ( "backbone frozen = " .. tostring ( bb ) )
print ( "sidechain frozen = " .. tostring ( sc ) )
print ( "one return value" )
bb2, sc2 = freeze.IsFrozen ( FRZ )
print ( "sidechain frozen = " .. tostring ( sc2 ) )

freeze.UnfreezeAll ()

The results are:

test 1 - freeze backbone
froze backbone segment 182
two return values
backbone frozen = true
sidechain frozen = false
one return value
backbone frozen = true
--
test 2 - freeze sidechain
froze sidechain segment 182
two return values
backbone frozen = false
sidechain frozen = true
one return value
sidechain frozen = true
Joined: 09/24/2012
Groups: Go Science

Waa! that's interesting. If I understand well, the only 2 Foldit functions that return 2 results are:

boolean,boolean v2 / freeze.IsFrozen (integer segmentIndex)
integer,integer v2 / freeze.GetCount ()

simply because it's written in the first colonn of the excel lua help file ;)

May be adding this property to IsLocked would be usefull ?

I would have saved a lot of complications in my recipes if I had known this before (I used a lot of time to try to print matrices or to select right sidechains ... it seems now obvious with unpack and this multiple results property).

Thank you both for your answers !

jeff101's picture
User offline. Last seen 5 hours 43 min ago. Offline
Joined: 04/20/2012
Groups: Go Science
In Loci's code above, I question the lines below:

print ( "one return value" )
bb2, sc2 = freeze.IsFrozen ( FRZ )
print ( "sidechain frozen = " .. tostring ( sc2 ) )

Did you not mean the following instead?

print ( "one return value" )
sc2 = freeze.IsFrozen ( FRZ )
print ( "sidechain frozen = " .. tostring ( sc2 ) )

Would the last group of lines then print as follows?

one return value
sidechain frozen = false
Sitemap

Developed by: UW Center for Game Science, UW Institute for Protein Design, Northeastern University, Vanderbilt University Meiler Lab, UC Davis
Supported by: DARPA, NSF, NIH, HHMI, Amazon, Microsoft, Adobe, RosettaCommons