n00b script/trigger question.

Share your scripts and packages with other Mudlet users.
Post Reply
MUDgate
Posts: 7
Joined: Tue Oct 21, 2014 10:14 pm

n00b script/trigger question.

Post by MUDgate »

So I'm going to just say this right off. I have, almost, no clue as to what I need to be doing to make this happen.

Anyhow here's my boggle:

A character I have paces back and forth between two dead-end rooms thanks to some triggers, he gets exhausted and sleeps, also thanks to triggers, as soon as he's rested he wakes (timers and triggers) and starts pacing again. The only problem is the better he gets at pacing, the more stamina he gets, so the room he passes out in keeps changing, disrupting up my triggers.

So i would like to know how to set up a script that does something like this:

If a desired direction, lets call it south, is available then the character moves south, if it isn't then he moves in the inverse direction, north.

The way I imagine it is the output for the available direction he sees when waking up triggers an "if/else" type of script.

Essentially I'd like to have my character wake up and go either north or south depending on which of the two rooms he passed out in. I would like to avoiding disrupting the trigger-loop every time he wakes up.

Is this even possible? Is it reasonable? Your thoughts? Thank you.

Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: n00b script/trigger question.

Post by Jor'Mox »

Yes, you can. First, obviously, you need a trigger to capture the available exits when you enter a room. The code to go with that trigger might look like this:
Code: [show] | [select all] lua
cur_exit = cur_exit or "south"
old_exit = old_exit or "north"
if not string.find(matches[2],cur_exit) then
   cur_exit, old_exit = old_exit, cur_exit
end

if cur_stamina < 10 then -- the 10 is just some random number, obviously use a value that makes sense
   send("sleep")
else
   send(cur_exit)
end
You may obviously want to do other things in each branch of the if statement, so just add them in as needed. And when you wake from your nap, you can just move immediately in the cur_exit direction.

MUDgate
Posts: 7
Joined: Tue Oct 21, 2014 10:14 pm

Re: n00b script/trigger question.

Post by MUDgate »

Jor'Mox wrote:Yes, you can. First, obviously, you need a trigger to capture the available exits when you enter a room. The code to go with that trigger might look like this:
Code: [show] | [select all] lua
cur_exit = cur_exit or "south"
old_exit = old_exit or "north"
if not string.find(matches[2],cur_exit) then
   cur_exit, old_exit = old_exit, cur_exit
end

if cur_stamina < 10 then -- the 10 is just some random number, obviously use a value that makes sense
   send("sleep")
else
   send(cur_exit)
end
You may obviously want to do other things in each branch of the if statement, so just add them in as needed. And when you wake from your nap, you can just move immediately in the cur_exit direction.
When you say "...a trigger to capture the available exits when you enter a room. The code to go with that trigger..." you loose me. I don't know much of anything about code. I know how to use triggers; I can make a trigger that response to the available exits, but i don't know how to make a trigger capture anything in code, if that's what your meaning.

Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: n00b script/trigger question.

Post by Jor'Mox »

When you make a trigger, there is a section for the pattern you are using, and below that, there is a big blank box. That is where you put code. The code will be executed when the trigger fires. To make a trigger capture something, you put the part you want to capture out of your trigger pattern in parentheses.

So, if you saw this for exits: Exits: [North South East West]
Then you could use this as a pattern to capture that line: ^Exits: \[[\w\s]+\]$
But that wouldn't pass anything to the code to use. So to capture the actual exits, you would change the pattern to look like this: ^Exits: \[([\w\s]+)\]$
With the added parentheses, the text inside them would be recorded in matches[2] (matches[1] would have everything that matched the pattern, in this case: Exits: [North South East West]), so matches[2] would be "North South East West". If you had a second set of parentheses, the text in that would be stored in matches[3], and so on. The code in the big box below can then use those values to do useful things, much the way I did in the small example I wrote.

There were a few important aspects of the whole end result you were trying to accomplish that I left out. For example, you would need a trigger to capture and store how much stamina your character had left, and you would need something to wake your character up and start walking again when you were sufficiently rested. There are a lot of different ways to make those things happen, but if you need help, I'm sure I can come up with something to help get you started.

User avatar
Zaphob
Posts: 180
Joined: Wed May 09, 2012 8:07 am
Location: mg.mud.de

Re: n00b script/trigger question.

Post by Zaphob »

Maybe check out the Trigger Manual

MUDgate
Posts: 7
Joined: Tue Oct 21, 2014 10:14 pm

Re: n00b script/trigger question.

Post by MUDgate »

Shaved it down to the basics but i finally got it working perfectly.

cur_exit = "south"
old_exit = "north"
if not string.find(matches[2],cur_exit) then
cur_exit, old_exit = old_exit, cur_exit
end

send(cur_exit)

Thanks for that bit of code, it helps when you have something to experiment and tinker with, versus trying to rout through pages of documentation.

Once I get a good handle on patterns and capture groups I think things will really open up for me.

Post Reply