Tab targetting for mudlet

edited January 2016 in Mechanic's Corner
So, I'm not sure if anyone has already made something like that but I couldn't find anything so I made this topic (sorry for being a complete newbie!). So, for some reason I decided I wanted to play with a gamepad whenever I wanted to bash some mobs and use the keyboard for simple commands and talking only. Well, so far so good. I also decided to use this as a sort of GMCP tutorial from newbie to newbies hehe. You are all free to use my code as you please since I'm no professional scripter or whathever, plus there may be more efficient ways of doing what I've done and I'd appreciate for anyone to point it out! Anyway, let's start it!

First of all you'll want to get to the scripts menu, just click that button on the top. Now press the add item button and it'll create a new script, name it something you like, I've named mine tab_targetting. Do note that you'll have to set the same name for the script and the function we'll create later on so that it'll work properly. And if you don't know what a function is, it's basically some code that'll run whenever you call it. I've compared the same name thing to the constructors in C++ since I'm more familiar with those but if you don't have any previous experience with coding, just remember to call it by name! Alright, now for the next field (the blank stuff where you write).

In the 'Add User Defined Event Handler:' field you've gotta use some GMCP, it's where the weird stuff begins. I'd suggest looking at the GMCP tutorials in the mudlet page if you have no idea how to use these but basically you can click the debug button and it'll show some additional messages (which you'll use for debugging). Oh, the debug button is in the menu to the left, if it's hidden just click those double arrows pointing down. With this you can see all the GMCP messages the MUD sends you. GMCP are basically hidden messages so you don't have to bother looking at even more text. And trust me, these are pretty uninteresting to read! Anyway, you should also get the run lua code script that allows you to test out your GMCP by typing in 'lua gmcp.*whichever one you want to see*', for instance 'lua gmcp.Char'! By the way, you type it where you'd usually write give, take, sit, stand, look, glance, etc and then just press enter. Neat!

Anyway, I've said a lot of stuff just now, type in gmcp.Char.Items (please do note the capital letters, they really do matter) in that field and we're good to go. Any time the MUD sends this GMCP our function will run. Oh, but you've gotta press that little plus button first! If it doesn't add your code to the 'Registered Event Handlers:' list just try running around clicking stuff and maybe close the scripts window and open it back again.

Now for the actual code, sweet! First of all, write this:

function tab_targetting()

That's where our code begins. If I'm not completely mistaken, that's called defining something, as in saying to the computer this exists because you said so! Also name your function the same name you gave your scripts, in my experience that was the problem I kept forgetting! If you're not using GMCP and just want to call it whenever then there is no need, but for this reason we actually need to. Right, now let's do some more coding! Write this down:

current_target = 0
mobs_in_room = {}
mobs_in_room_names = {}

Nice, that's some weird stuff! Basically a single equal sign means you're setting some 'variable' as something and two equal signs means you want to check if it was set. like in x = 1, y = 2, if x + y == 2 then punch me in the face! No, don't! Anyway, these little {} signs means you made a table, a lot of GMCP are tables so that's nice. Also, by putting these inside the 'loop' (inside the function that runs every time the GMCP is called) we reset it everytime! Great, that's useful! If you want to set it only once you can put it before you define that function! But for our purpose this is great hehe. anyway, for more weird stuff! Write these:

for k, v in pairs(gmcp.Char.Items.List.items) do
if gmcp.Char.Items.List.items[k] == "m" then

Here we can see why people shy away from coding, hopefully you'll read it as close to english as possible! That for loop actually iterates through your list of items (remember it is a table such as the one we created) and the if is looking for the "m" thing that defines the mobs. That means our tab targetting script will only look for mobs, you can change this by adding an or to it such as 'if gmcp.Char.Items.List.items[k] == "m" or gmcp.Char.Items.List.items[k] == "whatever"' but we're going to target mobs only here. Alright, now that we've checked if what the item is actually means mob we can write down this:

table.insert(mobs_in_room, gmcp.Char.Items.List.items[k].id)
table.insert(mobs_in_room_names, gmcp.Char.Items.List.items[k].name)

That's pretty simple. The table.insert function (yes, that's a function too!) basically puts what we want in a table, then we write the name of the table, and then what we want to add to it! I did a table for the id and one for the name so that it actually targets a single particular mob and displays it's name. now for more weird stuff, type in this:

end
end
end

Yep, three ends. First you need to end that if question, then you end that for iteration and then you end your function. That's it for the script, now it actually creates tables with a list of mobs in the room and resets your target everytime it does so! Now, to the hotkeys! Just save and then click the Keys button on the left.

Alright, click Add Item and name it whathever you want. I named mine tab, 'cause it's the button I'll use to tab the targets...? Anyway, click the Grab New Key button and click whatever button you want to use for tab targetting. Do note that some buttons do not work such as... tab... yeah. I used F12 since I never really do press it, just set it to whatever you want. Leave that command line blank and let's get to the last box in that stuff! Write in:

if current_target == nil then
current_target = 0
end

This basically means that if, for whathever weird reason, our script hasn't set a current_target (which should already be 0), then our hotkey will. It's pretty unnecessary, really. I just like writing. Alright, next!

if current_target < #mobs_in_room then
current_target = current_target + 1
else
current_target = 1
end

Yep, that's simple as well. This checks if the number of our current target is less than the number of mobs in the room, this way we're not passing the number of it. Oh, if you don't understand it I'll show ya! You see, our table has a number of mobs equal to the number of mobs in the room so if we get the number of mobs using the # thing, we get, for example, 3! Now, if our current_target is the second mob we get 2! Now, if we press 'tab' the number is less than 3 and will increase by one! But if it's 3 it resets to 1. Neat! Now we can cycle through mobs! Alright, next we'll do:

if mobs_in_room[current_target] ~= {} then
send("t " .. mobs_in_room[current_target])
cecho("<red>Target: " .. mobs_in_room_names[current_target])
end

Right, so first we check if the table isn't empty, that's what the ~= is for! Then, we send a command, I did "t " since it's my ingame target alias. The .. is for adding stuff to the text you send, the mobs_in_room[current_target] is the id of the mob. The current_target variable is being used to point to a position in our table! Now, the cecho is something we use to write text only the user can see! I painted ir red. It also shows the name of the current target instead of the id. Finally we end our code. Great, now you can use it as you like! For simplicity reasons, I'm adding the full codes now. Have fun and I hope I've been useful!

Script Name: tab_targetting

Event Handlers: gmcp.Char.Items

function tab_targetting()

current_target = 0
mobs_in_room = {}
mobs_in_room_names = {}

for k, v in pairs(gmcp.Char.Items.List.items) do
if gmcp.Char.Items.List.items[k].attrib == "m" then
table.insert(mobs_in_room, gmcp.Char.Items.List.items[k].id)
table.insert(mobs_in_room_names, gmcp.Char.Items.List.items[k].name)
end
end
end

Key name: Tab
Keybinding: F12

if current_target == nil then
current_target = 0
end

if current_target < #mobs_in_room then
current_target = current_target + 1
else
current_target = 1
end

if mobs_in_room[current_target] ~= {} then
send("t " .. mobs_in_room[current_target])
cecho("<red>Target: " .. mobs_in_room_names[current_target])
end


There you go, if there's any problems with the code or if I made some mistakes do let me know! It's pretty weird writing code in a forum post. Also, I recommend binding a key to look and pressing it whenever you kill a mob, I hope doing so will prevent it from staying inside the table. Good luck!

ps: the k in the for loop is for key and the v is for value. Forgot to say that before, if you want to know more do search for pair function in lua code using google.
Sign In or Register to comment.