Need assistance with Script.

Posted by Zubby on Tue 08 Aug 2023 10:29 PM — 8 posts, 20,236 views.

#0
Hello All,

I'm new to LUA scripting and was wondering if there is any way for my script to detect certain keywords and act on them WITHOUT setting up an external trigger.

Is there a Lua Function that will check for a Certain string upon entering a new room?

Specifically, I am running a auto-path and want the script to detect who is in the room (Friendly or Evil) and either attack and stay in that room until mob is dead or move on if it is a friendly.

I've figured out how to iterate through a String Array that will fire off each path name in a loop, but I want it to pause in each room and attack/heal until finished then continue on.

Here is what I have so far for the movement and it works:

gopath = {"nw", "nw", "nw", "n", "n", "e", "e", "e", "se", "se", "sw", "s", "sw"}

loc = loc or 0

for loc = 1, 13 do
Send (gopath[loc])
end

Thanks in advance!
USA Global Moderator #1
Quote:
detect certain keywords and act on them WITHOUT setting up an external trigger

Technically, yes, but I think honestly that doing it that way will quickly become far too complex code-wise.

I suggest instead to break the problem apart into smaller pieces. You have a list of the steps you want to move. That's good. But instead of a loop that sends all the parts of the path, define what happens in a single room as a step in your process.

1. a trigger on the players in the room that checks for enemies and either starts combat (if enemies) or gets the next direction from the list and moves in that direction (if no enemies)
2. a trigger on the end of combat that gets the next direction from the list and moves in that direction

Now it should be clear that all you need for this to work is a variable that keeps track of which movement step you're currently at.
Amended on Tue 08 Aug 2023 11:17 PM by Fiendish
#2
Hi Fiendish,

Thanks for the response. I've been scouring the forums and my conclusion is I think I need to use a combination of Alias (To house the main script) Triggers, to enable and disable upon entering each room, and possibly some timers that check health/spellpoints and perform actions based on how much I have left.

That being said, is it easy to send information (variables) between Alias, Trigger, Timer? I'm new to LUA and I understand that you can define global variables and local variables. I'm guessing local variables only apply to the function they are contained in and never make it out, whereas global variables should be able to be used throughout all scripts, which in turn can be used with triggers/timers etc?

Sorry if that question is confusing, I'm not sure how else to explain it.

Thanks!
USA Global Moderator #3
Your concept of local and global is basically correct, but we should be careful because there are local and global variables inside the Lua script itself and there are also local and global variables with respect to working between multiple different scripts. The best way we have to verbally differentiate between them is to refer to "script" variables and "MUSHclient" variables. The MUSHclient variables are the ones that will let you talk between your aliases and triggers. For your purposes the Lua script local and global variable difference doesn't matter, and you only care about the script vs MUSHclient variable difference.

Lua script variables are like your gopath.

MUSHclient variables are set with SetVariable("variable_name", "variable_value") and read with GetVariable("variable_name")

Unfortunately MUSHclient does not automatically convert complex types like tables into strings and back when you use SetVariable and GetVariable, so that part becomes manual.

But you can, for example, create a MUSHclient variable named gopath with the value
nw,nw,nw,n,n,e,e,e,se,se,sw,s,sw

Note, it is important here that I used only commas between the steps and no quotation marks or spaces.

And you can have another MUSHclient variable called gopath_step with the value
1


And then in a trigger or alias you can do

script_gopath = utils.split(GetVariable("gopath"), ",")    -- read the comma-separated path from the MUSHclient variable and split it into components
script_gopath_step = tonumber(GetVariable("gopath_step"))  -- read the current step number along the path
script_current_step = script_gopath[script_gopath_step]    -- pick the step from the path for that step number
if script_current_step then
  Send(script_current_step)                                -- if there's a next step, move in that direction
end
SetVariable("gopath_step", script_gopath_step + 1)         -- add 1 to the step number so that we do the next step next time


That would do the next step in the sequence, whatever it happens to be.
Earlier in the thread I mentioned two triggers for moving to the next room. That's how you would do the movement in those triggers. No loops involved.
Amended on Wed 09 Aug 2023 03:56 PM by Fiendish
USA Global Moderator #4
Nick has a good video on the different types of variables and how to use them to get values between different triggers/aliases here: https://mushclient.com/forum/?id=10863
#5
Is there a way to enable/disable triggers within a script? So lets say I have a main script that has a bunch of nested for / while statements. Could I call a trigger from within there, and then disable it when not needed?
Australia Forum Administrator #6
Template:function=EnableTrigger
EnableTrigger

The documentation for the EnableTrigger script function is available online. It is also in the MUSHclient help file.



Quote:

Could I call a trigger from within there, and then disable it when not needed?


You don't call triggers, the program does that when a line matches. You could enable it, see above.
Australia Forum Administrator #7
Quote:

That being said, is it easy to send information (variables) between Alias, Trigger, Timer? I'm new to LUA ...


It's Lua, not LUA. It's not an acronym.

Lua variables, in the main script space (outside a plugin) or within a particular plugin, are available between aliases, triggers and timers. While what Fiendish said is correct, in a particular session, you can just rely upon setting a variable in an alias (a Lua variable) and it being immediately available to triggers and timers.

However Lua variables do not persist (after closing the world/program) unless you take steps to save them. See: http://www.gammon.com.au/forum/?id=4960