Saving player stats to variables

Posted by Blixel on Wed 09 Aug 2017 03:17 PM — 5 posts, 23,854 views.

#0
In the game I'm playing, there is a "stats" command that will return the player's statistics. I'm trying to create a combination of triggers that will capture these stats as variables for later use.

The output of the stats command is as follows:

>stats

Strength:     18  (+2)
Dexterity:    7   (+1)
Intelligence: 6 
Wisdom:       8 
Charisma:     8 
Constitution: 18

Hit Points:   79    Hit Max: 82 
ArmorClass:   4     Level:   9  

You are a old chaotic-evil male dwarf fighter. You are wearing 0 rings. You are
wearing platemail and are armed with a greatsword.
Your rank is Lord.


Then I'll save several of these values as playerStr, playerDex, playerInt, and so on. I'd also like to be able to save playerLevel and playerClass from the other section.

So far I've just been focusing on capturing playerStr. I assume if I can figure that out, then getting the other 5 primary stats will be a copy/paste.

I've attempted a dozen or more regular expressions but I really struggle with these. This is as far as I've gotten, which I know is wrong, but this is the only thing I've done so far that gives me any thing at all:

^Strength:*(.+)


This captures...

     18  (+2)


...which of course I can't use. I can't figure out how to get just the 18.

So my specific question is, how do I capture the output of that stats command so that I can save the values as variables?

Thanks for any help.
Amended on Wed 09 Aug 2017 03:18 PM by Blixel
Australia Forum Administrator #1
Template:regexp
Regular expressions
  • Regular expressions (as used in triggers and aliases) are documented on the Regular expression tips forum page.
  • Also see how Lua string matching patterns work, as documented on the Lua string.find page.


Stay calm and just look at what the string contains. In your case try:


^Strength:\s+(\d+)


This is matching:

  • ^ --> start of line
  • Strength: --> literally that word and the colon
  • \s+ --> one or more spaces
  • (\d+) --> a capture group, consisting of digits only
#2
Ok that worked. Thank you, I appreciate the fast reply. I keep thinking that the asterisks is the wildcard to match any number of arbitrary characters. At least that's how it works at the command line. ls *.txt for example.
#3
I have another question relating to this same topic. I'm also wanting to setup a playerClass variable. The information from the game comes from the paragraph underneath of the stats.

You are a old chaotic-evil male dwarf fighter. You are wearing 0 rings. You are
wearing platemail and are armed with a greatsword.
Your rank is Lord.


The keywords for the class can be one of these words:
fighter, magic-user, cleric, ninja, assassin, fighter/cleric, or fighter/magic-user


My regular expression is working, but if the player is a fighter/cleric, it seems to find the word "cleric" first. And it doesn't help if I place the fighter/cleric class before the word cleric in the regular expression. It will still find "cleric" first.

So my question is, how can I prioritize the multi-class characters so that the regular expression will look for those first?

^You are a .*(fighter\/cleric|fighter\/magic-user|fighter|magic-user|cleric|ninja|assassin)
Australia Forum Administrator #4
Your problem here is that the .* is "greedy". See the link above about what greedy means.

When it sees ".*(fighter\/cleric|cleric" it "greedily" consumes "fighter" as part of .* and can still match on "cleric" thus satisfying the regexp.

Make it non-greedy and it works:


^You are a .*?(fighter\/cleric|fighter\/magic-user|fighter|magic-user|cleric|ninja|assassin)


Note that ".*" is now ".*?"