Conditionals in Init.txt


Problems running or configuring the software, commands & options, compiling, different platforms, using the interface, documentation, etc.

User avatar

Vestibule Violator

Posts: 1459

Joined: Sunday, 19th December 2010, 05:45

Location: New England

Post Saturday, 5th May 2012, 03:12

Conditionals in Init.txt

Can anyone explain how you're supposed to get conditional statements working in init.txt? I decided to try and experiment with automating some of the inscriptions I do, but Crawl just seems to ignore my if statements and executes what's inside whether they're true or not.

A simple (if useless) example- autoinscribe books with !r if you're a Troglodyte.

  Code:
if you.god("Trog")
    autoinscribe = book:!r
end

Problem? It autoinscribes all books with !r, regardless of religion. What am I doing wrong?

How do the you.foo terms work? Are they all functions? Ie, you.foo(test) returns true if you test the right value? Or are they variables I have to check with == etc? A list of all the you.foos aviable would be nice too- I'm not sure what I have to work with.

Dungeon Master

Posts: 1612

Joined: Thursday, 16th December 2010, 21:54

Post Saturday, 5th May 2012, 04:21

Re: Conditionals in Init.txt

Section 7 of options_guide.txt has a little bit of detail on inline Lua, in this particular example you do have the syntax wrong. It should be:
  Code:
: if you.god() == "Trog" then
    autoinscribe = book:!r
: end

For a list of available Lua hooks I think unfortunately you just have to dive into the source (l_you.cc for example), since they're not currently documented as far as I know.

For this message the author Kate has received thanks:
mageykun
User avatar

Vestibule Violator

Posts: 1459

Joined: Sunday, 19th December 2010, 05:45

Location: New England

Post Saturday, 5th May 2012, 04:48

Re: Conditionals in Init.txt

Dangit. I had the :'s and the then in there at one point. I tried == "Trog" too, but without the parenthesis. So close! :lol:

Too bad the hooks aren't documented, but now that I have a working bit of syntax to go off of, it shouldn't be too hard to trial and error how the various you.foo()s work. (Or... maybe I'll finally go poke around in the source).

Thanks.
User avatar

Vestibule Violator

Posts: 1459

Joined: Sunday, 19th December 2010, 05:45

Location: New England

Post Monday, 7th May 2012, 06:15

Re: Conditionals in Init.txt

It finally occurred to me that lua is a coding language in it's own right and I can just look up what's different instead of being continually befuddled when my C conventions don't work. Gosh, I'm an idiot sometimes.

Anyways, got me source, a working coding environment, a sampling of lua hooks and I'm happily setting up the idiot proofing I've always wanted. In addition to conditional autoinscriptions, I've also discovered you can build macros to install confirmation prompts basically anywhere you want. Woot.

The only strangeness I've encountered so far is when you change parameters (for example, if you.god() == "Trog" changes from true to false) behavior doesn't always update to match unless you save/load. (Not that this is a problem- you don't change god / race / class very often :p). I assume this is an inherent limitation of working in init.txt, outside the game as it were? The ifs are only getting checked on first read, or the lua hooks don't get continually updated, or something. Interesting.
User avatar

Mines Malingerer

Posts: 38

Joined: Friday, 13th April 2012, 05:35

Location: Palm Bay, Florida

Post Monday, 18th June 2012, 04:13

Re: Conditionals in Init.txt

mageykun wrote:The only strangeness I've encountered so far is when you change parameters (for example, if you.god() == "Trog" changes from true to false) behavior doesn't always update to match unless you save/load.


As described here https://crawl.develz.org/tavern/viewtopic.php?f=9&t=4397

MarvinPA wrote:The main advantage of doing it entirely as a Lua function like that and then using "add_autopickup_func(ammo_pickup)" is that you don't need to save and reload the game for it to work. If you just have a list of if/else lines then they'll only get called when you first load the game, so you'd have to save and reload once a skill hits 3 in this example. Not really a big deal, but nice to avoid since it's possible to do so! At some point I want to rewrite all the conditional parts of my autopickup config to work that way.
One out of fouw people suffews fwom Ewmuh Fudd Syndwome. Guess whaaat?
User avatar

Vestibule Violator

Posts: 1459

Joined: Sunday, 19th December 2010, 05:45

Location: New England

Post Monday, 18th June 2012, 16:45

Re: Conditionals in Init.txt

Oh, hey, thanks! So there is a way to make these kind of things dynamic instead of static.

How would you apply this to auto-inscriptions though? I mean, I could write a function that returns true for the things I want to inscribe, but then what do I do with it? Is there an equivalent of add_autopickup_func for inscriptions?

Return to Technical Support

Who is online

Users browsing this forum: No registered users and 2 guests

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.