Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Lua
➜ More efficient way of running a script
More efficient way of running a script
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Batista
(15 posts) Bio
|
Date
| Sat 31 Dec 2005 12:25 PM (UTC) |
Message
| I have been working on a healing system for Achaea since I first started. It works well, it's just really slow since it runs off of a timer. I'll give you a bit of an example:
A timer runs my healing script (one ginormous if statement) every .7 seconds.
If it sees that one of my affliction variables are set to '1' then it heals it.
Triggers turn the variables on and off.
This works pretty well for me, and I don't exactly get any system lag from it. However, it is very slow. I tried just using triggers and aliases, but that presents its own huge set of problems. It creates large gaps for exploits in my healing, and I more or less have to have the scripts running by the prompt.
Any ideas? | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #1 on Sat 31 Dec 2005 07:52 PM (UTC) |
Message
| In what was is it "very slow"? The script itself takes a long time to run? Lua should be pretty fast, it would have to be very big and inefficient to run for a long time. Also, as you say you run it every 0.7 seconds, I presume that it in fact takes less than 0.7 seconds to run, or you would always be in the script.
What you probably mean is that the script is slow to respond to the need for healing, is that it? That tends to be the flaw in "polled" type programming, where you check every x seconds for whether to do something. Say you need something done, and it happened just after the script ran, you need to wait another 0.7 seconds for the script to notice.
Without seeing the script it is hard to help more. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Batista
(15 posts) Bio
|
Date
| Reply #2 on Sat 31 Dec 2005 09:09 PM (UTC) |
Message
| Yeah, that's what I mean. It is slow to respond to my need for healing. The timers are nice and reliable, but I'm wondering if there is something I can do that is faster than the "polling" thing.
Also, the script is like:
if pbal == "1" then
if paralysis == "1" then
Execute("br")
else
if stupidity == "1" then
Execute("gol")
else
repeat for every affliction
lots of ends for every affliction if
end | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #3 on Sat 31 Dec 2005 09:24 PM (UTC) Amended on Sat 31 Dec 2005 09:25 PM (UTC) by Nick Gammon
|
Message
| For one thing, you can use "elseif" to save a bit of space, like this:
if paralysis == "1" then
Execute("br")
elseif stupidity == "1" then
Execute("gol")
elseif
-- repeat for every affliction
end -- big if statement
However a thing like that is crying out to be made into a table. I seem to remember doing an affliction curing script recently, but taking the way you have done it you could make it simpler with a table, like this:
afflictions = {
paralysis = "br",
stupidity = "gol",
-- and so on
}
function f (key, value)
if _G [key] == "1" then
Execute (value)
return true
end -- if
end -- f
table.foreach (afflictions, f)
This is using the trick of indexing into the _G table (global variables) to find if a variable is set. Effectively, these two are the same:
if paralysis == "1"
if _G ["paralysis"] == "1"
What my code above does it use table.foreach to check each item in the table, find if that variable is set to "1", and if so, do the Execute of the cure. Then it returns true to break out of the loop, so only one cure is done at once.
However I think a better approach is to put all the afflictions into their own table, rather than using global variables.
Take a look at this post, I have covered a lot of this ground before:
http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=6074
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #4 on Sat 31 Dec 2005 09:27 PM (UTC) |
Message
| Also, my other script doesn't use timers, but responds to triggers, so it should work much faster. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Batista
(15 posts) Bio
|
Date
| Reply #5 on Sat 31 Dec 2005 09:50 PM (UTC) |
Message
| How do you run it off of triggers reliably? I find that when I try to run scripts purely off triggers, I have to put in more failsafes than is worth it.
Like, if I get paralysis and stupidity, and I want to cure paralysis first, I'll try to eat bloodroot. However, stupidity causes some of your commands to get jumbled with emotes when you try to do something. It gets even worse when amnesia sets in (you do something and nothing happens at all, just a new prompt pops up).
Therefore, the timer seemed like the easy way out, but it just kind of... heals slowly. I would love to work off triggers, but I can't find a way to do it reliably.
I love the tables thing you came up with. It seems like a neat idea, even though I don't know how it works exactly. I'll play with it here in a bit.
I guess just trying to script just using the functions on the function page or the in-client help file kind of limits me in what I do.
Thanks for the help, by the way! It's much appreciated. :) | Top |
|
Posted by
| Metsuro
USA (389 posts) Bio
|
Date
| Reply #6 on Sun 01 Jan 2006 01:25 AM (UTC) |
Message
| well with the stupiditiy affliction when you are affected with it have it activate a timer to re-eat the herb ever so many seconds till the cure output is displayed and such. |
Everything turns around in the end | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #7 on Sun 01 Jan 2006 02:53 AM (UTC) |
Message
| The example script I gave had a table in which you had the order in which you wanted cures done. So, you would put stupidity high in the list. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Tsunami
USA (204 posts) Bio
|
Date
| Reply #8 on Mon 02 Jan 2006 06:59 PM (UTC) |
Message
| I have a script similar to your Batista which I've designed to run one of two ways. The first way is similar to yours, cept my timer is .3 seconds. I haven't noticed any reduction in speed or anything though. When I evaluate everything I do take into account balance, whic I can't tell if you are doing, so if I don't have herb balance I don't even check any of the herb cures, etc... The second method is trigger based. As you've probably realized, attempting to cure off the prompt is prone to infinite looping, therefore I only cure off the prompt after a new affliction as been detected, and other than that, cure on balance recovers. I have yet to finish this part entirely, but it seems to be working pretty well. | 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.
29,177 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top