PDA

View Full Version : Is it this hard, or am I just that dense?



Saurven
02-04-2012, 02:40 PM
So to make a script with definitions, that remembers a string throughout, I believe I hafta use a $.
For instance, I'm making a script to keep track of who I'm supposed to give boxes back to after my script picks them.

$customer = []

is at the top of my script.

In my accept line I've tried so many variations, I'll just put in the last one that didn't work:


def startup
waitfor "offers you"
fput "accept"
line = get
customer = i if line =~ /(You accept) (?i:[A-Z][a-z])'s offer/
$customer.push(customer.noun)
fput "glance #{$customer}"
x = matchwait /You have no offers|You accept/
if x =~ /You have no offers/
waitfor /#{customer} offers you/
fput "accept"
checkbox
elsif x =~ /You accept/
checkbox
end
end


I'm trying to glance at the customer so I know if it's working before I empty Hell of all his boxes. I end up just glancing at my hands. I realize this customer = line is way off. I was trying to figure out how Waggle took it's names and incorporate that into my script. FAIL!

Saurven
02-04-2012, 02:44 PM
Incidentally, I realize that:


x = matchwait /You have no offers|You accept/
if x =~ /You have no offers/
waitfor /#{customer} offers you/
fput "accept"
checkbox
elsif x =~ /You accept/
checkbox

makes no damned sense here. I'm altering the script as I go to make the .push work. I've got some cleanup of old attempts to do in that entire def.

DaCapn
02-04-2012, 05:58 PM
Making a global variable is not strictly necessary but it makes it less annoying. You could define your function within your script to accept and return variables (i.e. `def function(vara,varb,varc); return "#{vara}, #{varb}, and #{varc}"; end; function("Tom","Dick","Harry")`)

The 'customer' variable is a string. If you want to use a proper game object, you could use `GameObj[customer]` and then you could use methods like noun, id, etc. Also, '$customer' is of type array so `fput "glance #{$customer}"` would send "glance TomDickHarry".

Here's basically what I would do:



customer=nil
while customer.nil?
line = get
if line =~ /[A-Z][a-z]* offers you.*(box|chest|coffer|strongbox|trunk).*ACCEPT.*D ECLINE/
customer = GameObj[line.gsub(/ offers you.*/,"")]
end
end
fput "accept"


'customer' is the customer's game object so all of the popular methods can be used, and it will ignore non-box offers.

Also, looks like you could use a little help with regex. This is a handy quick reference and testing web app: http://rubular.com/

Saurven
02-04-2012, 06:01 PM
You are the man. Thanks for the explanation, and the link.

Saurven
02-05-2012, 03:05 PM
Just to make sure I've got it right,

customer = GameObj[line.gsub(/ offers you.*/,"")]

the line.gsub designates customer as the GameObj directly preceding offers you?

vamosj
02-05-2012, 03:30 PM
...أنا أكره روبي، أنه لا معنى سخيف بالنسبة لي

DaCapn
02-05-2012, 04:35 PM
Just to make sure I've got it right,

customer = GameObj[line.gsub(/ offers you.*/,"")]

the line.gsub designates customer as the GameObj directly preceding offers you?

Not at all. Well, what you said sounds like gibberish, anyway.

It's all order of operations. Think about what you've done to initiate the whole thing. You grabbed a string that says "Guy offers you..." That's all you have, a sentence with a name in it. What comes next is taking that string and making it useful.

The data in 'line' is of the type String. The 'gsub' method does string substitution, replacing the regex in the first argument with what's in the second. That leaves you with the name as a string. Feeding that string into the GameObj class gives you the matching game object that you can use all of the usual methods on (name, noun, id, etc).

A lot of this is totally apparent if you just break up all of your steps and play on the command line with things like this:



;e string="Guy offers you a something thing. Type ACCEPT..."; echo string.gsub(/ offers you.*/,"")
;e fput "tap ##{GameObj["Guy"].id}"


Unless I'm 100% sure of myself, that's what I do while I write every script. I do a bunch of one-liner tests in-game.