[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Need assistance with Script.

Need assistance with Script.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Zubby   (7 posts)  [Biography] bio
Date Tue 08 Aug 2023 10:29 PM (UTC)
Message
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!
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #1 on Tue 08 Aug 2023 11:14 PM (UTC)

Amended on Tue 08 Aug 2023 11:17 PM (UTC) by Fiendish

Message
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.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Zubby   (7 posts)  [Biography] bio
Date Reply #2 on Tue 08 Aug 2023 11:21 PM (UTC)
Message
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!
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #3 on Wed 09 Aug 2023 01:56 AM (UTC)

Amended on Wed 09 Aug 2023 03:56 PM (UTC) by Fiendish

Message
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.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #4 on Wed 09 Aug 2023 02:11 AM (UTC)
Message
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

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Zubby   (7 posts)  [Biography] bio
Date Reply #5 on Wed 09 Aug 2023 04:06 AM (UTC)
Message
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?
[Go to top] top

Posted by Nick Gammon   Australia  (22,966 posts)  [Biography] bio   Forum Administrator
Date Reply #6 on Wed 09 Aug 2023 04:17 AM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (22,966 posts)  [Biography] bio   Forum Administrator
Date Reply #7 on Wed 09 Aug 2023 04:20 AM (UTC)
Message
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

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


2,969 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]