GMCP Tutorial Part 4

  Table of Contents

Written by Vadim Peretokin

Triggering on GMCP

Lastly, we’ll learn how to trigger on GMCP events - this means doing something right when you got the event, not on some line/prompt from the MUD. Why would you want to do that? There might be times when you receive a GMCP event and an input from the line won’t come right away, and you want to act on the event fast - or you might want to do it for neater system organization.

Remember how you unshrouded GMCP by enabling Debug? As an example, we'll make an event trigger on the gmcp.Char.Vitals event. Go to Scripts, Add Item, and give the script a name, in this instance, 'my_vitals_function', and paste:

gmcp.Char.Vitals

into the Add User Defined Event Handler field:

GMCP 

 And press enter to add it to the list. This will have Mudlet run whatever function we tell it to when that event comes along - and we'll do that by using the function name as the scripts name, plus defining it in the script

GMCP 

That's it! Now you can add code into your function to do whatever you'd like when you receive a GMCP event.

Tip: Mudlet not only raises an event for the exact GMCP event that you receive, but it also raises events for parts of the name, allowing you to create a handler that deals with more than one event, if you find it convenient. So when you receive a Char.Skills.Groups event, for example, events gmcp.Char, gmcp.Char.Skills, and gmcp.Char.Skills.Groups events will be raised, in that order.

Bonus

We can apply our learned GMCP knowledge to create a simple health/mana tracker (this example would be applicable easier to more IRE games than a health sipper). It'll show us our differences in health/mana on each prompt! It's very simple for a purpose – so you can easily see how to improve on it and do it.

Start a new prompt trigger, that has the same setup as the sample one we did previously -

return isPrompt()

As a Lua function pattern type. Next, add this as the code:

-- create variables that'll track our previous health, so we can use them to compare

oldhealth = oldhealth or 0

oldmana = oldmana or 0

 

-- capture our current health & mana

local myhealth, mymana =

tonumber(gmcp.Char.Vitals.hp), tonumber(gmcp.Char.Vitals.mp)

 

-- compute the difference with our previous amount

local healthdiff = myhealth - oldhealth

local manadiff = mymana - oldmana

 

echo(string.format("[%dh/%dm]", healthdiff, manadiff))

 

-- and lastly, store our current health as old health now, for the next prompt

oldhealth = myhealth

oldmana = mymana

So it all looks like this:

GMCP 

That's it. Now you'll see it working below:

GMCP 

 That's it!

We've covered how to retrieve GMCP data, trigger on GMCP events, and ask for more GMCP information, should covers all of the basics. One last parting tip - not all GMCP modules are activated by default; if you'd like to enable one (like you'd need to do for Ire.Rift, for example), check out the Mudlet wiki.

Tip: You can also utilize GMCP for things like antitheft! Take a look at some examples posted on Achaean forums.

Have comments, ran into problems, or want to post a tip? Write below!

Written by Vadim Peretokin