I7 - How to remove the text "ms" from the player's command?

Hello,

I’m trying to help a friend, and can’t figure out how to catch a command like WAIT 4200MS and change it to WAIT 4200 MS

Here’s the example game we are testing with:

"Wait N Milliseconds Tester" by KV

The Time Capsule is a room. "This location is quite nondescript."

WaitingMilliseconds is an action applying to one number.

Understand "wait [number] ms" or "wait [number] milliseconds" as WaitingMilliseconds.

Carry out WaitingMilliseconds: say "You wait [the number understood] milliseconds, for some reason.".

After reading a command when the player's command matches the regular expression "^wait (\d.*)ms$":
	let T be the player's command;
	replace the regular expression "(\d.*)ms" in T with "\1";
	say "CHANGED TO '[T]'.";
	change the text of the player's command to T.

Things we’ve tried:

replace the text "ms" in T with "";
replace the regular expression "(\d+)ms" in T with "\1";
replace the regular expression "(\d.*)ms$" in T with "\1";
replace the regular expression "ms$" in T with "";

This (from an example online) doesn’t even change T: replace the regular expression “\d+” in T with “…”;

>wait 3000ms
CHANGED TO "wait 3000ms".
I only understood you as far as wanting to wait.

>wait 3000 ms
You wait 3000 milliseconds, for some reason.

>wait 3000 milliseconds
You wait 3000 milliseconds, for some reason.

>wait 42ms
CHANGED TO "wait 42ms".
I only understood you as far as wanting to wait.

How am I messing this up? =)

…or is there a different approach?

I think it’s easier to utilize the built-in parser for this.

A duration is a kind of value.
[I'm not sure if you can have both of the following.
If you can, that would be perfect.
If not, you can use the one without a space and add an understand rule
for the one with a space.]
99 ms specifies a duration.
99ms specifies a duration.
99 milliseconds specifies a duration.

Understand "wait [duration]" as WaitingMilliseconds.
5 Likes

Thank you very much!

Celtic’s answer will save you a lot of text matching grief, but for the record: the reason you were having trouble lies in your defining of T.

The Time Capsule is a room. "This location is quite nondescript."

WaitingMilliseconds is an action applying to one number.

Understand "wait [number] ms" or "wait [number] milliseconds" as WaitingMilliseconds.

Carry out WaitingMilliseconds: say "You wait [the number understood] milliseconds, for some reason.".

After reading a command when the player's command matches the regular expression "^wait (\d*)ms$":
	let T be the "[player's command]";
	replace the regular expression "ms" in T with " ms";
	say "CHANGED TO '[T]'.";
	change the text of the player's command to T.

The important point is what I did here:

let T be the "[player's command]";

Simply going ‘let T be the player’s command;’ doesn’t work, but ‘let T be the “[player’s command]”;’ does! It might be something with how snippets and such are handled that’s the cause. I can’t quite remember off the top of my head.

1 Like

That converts the command from a snippet to a string. A snippet is merely a reference to a span of words in the parsing buffer; the player’s command is the span of all the words currently in the buffer. However, regular expressions only work on strings, so you need to convert it first.

1 Like

Okay! So I was remembering correctly. Yeah, the idea is to forcefully convert the command from a snippet to a string via defining T as such.

Be careful, though:

> X DAMSEL
You don’t see any da msel here.

This is why Celtic’s solution is the better one.

(This is with Ephemeral’s regex; the original one, with the \d in front, is a bit safer.)

1 Like

OH! Apologies if I messed something up. I was just throwing something together quickly to demonstrate the “[player’s command]“ distinction.

…Although, am I missing something? Doesn’t that only fire off if it’s already matched to the longer statement?

After reading a command when the player's command matches the regular expression "^wait (\d*)ms$":

>X DAMSEL
You can’t see any such thing.

(For the record, the only reason I made that edit to begin with is that it seemed the desire was to put a space between a number and ms, since there was no “understand ‘wait [a number]’ line in the original code.)

Ah, yes, I was too hasty!

You can still run into issues with X DAMSEL THEN WAIT 30MS but that’s less snappy as a demonstration. My point is that regex replacements should be a weapon of last resort because they so often have unintended side effects in more complicated commands. Better to leave the parsing to the parser.

1 Like

Yeah. For the record, now that I’m messing with it in more depth, I certainly concede this is safer.

After reading a command when the player's command matches the regular expression "^wait (\d*)ms$":
	let T be the "[player's command]";
	replace the regular expression "(\d*)ms" in T with "\1 ms";
	say "CHANGED TO '[T]'.";
	change the text of the player's command to T.

(Though it still wouldn’t work out with any multi-command input.)

Point stands that Celtic’s example is the best bet here.

2 Likes

Yeah, this seems to do the trick:

After reading a command when the player's command matches the regular expression "^wait (\d+)\Sms$":
	let T be the "[player's command]";
	replace the regular expression "(\d+)\Sms$" in T with "\1 ms";
	[say "CHANGED T TO '[T]'!";]
	change the text of the player's command to T.

He’s going with the safe approach by Celtic in the game, and he sends many thanks.

Meanwhile, I’m trying to break things with this approach in a test game, with no luck. =)

I think "[player's command]" along with the \S in the regexp pattern to replace were the missing keys, just as far as what I was attempting goes. Also had to use \d+ instead of \d.* to avoid catching WAIT 42 MS, ha-ha.

Thanks, again, everyone!

Have you tried inputting multiple actions at once? Just as a warning, it seems to break fairly consistently there on my end, even without the regex shenanigans.

The Time Capsule is a room. "This location is quite nondescript."
The damsel is a person in the time capsule.

WaitingMilliseconds is an action applying to one number.

Understand "wait [number] ms" or "wait [number] milliseconds" as WaitingMilliseconds.

Carry out WaitingMilliseconds: say "You wait [the number understood] milliseconds, for some reason.".

Time Capsule
This location is quite nondescript.

You can see a damsel here.

>wait 3 ms
You wait 3 milliseconds, for some reason.

>wait 3 ms. take damsel.
I only understood you as far as wanting to wait.

Celtic’s suggestion is working perfectly though.

The Time Capsule is a room. "This location is quite nondescript."
The damsel is a person in the time capsule.

A duration is a kind of value.
[I'm not sure if you can have both of the following.
If you can, that would be perfect.
If not, you can use the one without a space and add an understand rule
for the one with a space.]
99 ms specifies a duration.
99ms specifies a duration.
99 milliseconds specifies a duration.

WaitingMilliseconds is an action applying to one duration.

Understand "wait [duration]" as WaitingMilliseconds.

Carry out WaitingMilliseconds: say "You wait [the number understood] milliseconds, for some reason.".

(Once again, thrown together quickly for the sake of demonstration.)

Time Capsule
This location is quite nondescript.

You can see a damsel here.

>wait 3ms
You wait 3 milliseconds, for some reason.

>wait 3ms. take damsel.
You wait 3 milliseconds, for some reason.

I don’t suppose the damsel would care for that.

3 Likes