Making my Script respond faster.

Posted by Natasi on Thu 03 Mar 2005 05:23 PM — 14 posts, 54,330 views.

#0
Ok, I've got over 5k lines of code in my script and I'm looking to redo the entire thing so it will run smoother. I've been getting help and know a few things I need to change already and it was suggested to use Arrays, but those are just insanely confusing so far. Any tips on a smoother/faster way to write the script would be great. I use VBscript and below is an example of how I have my script.

******************************************************
Sub Diagpurgheal (a, b, c)
if getvariable ("affliction_aeonD") = "1" then
exit sub
end if
if getvariable ("affliction_slicknessD") = "1" then
exit sub
end if
if getvariable ("affliction_crotamineD") = "1" then
world.sendpush "drink antidote"
exit sub
end if
if getvariable ("affliction_asthmaD") = "1" then
world.sendpush "drink melancholic"
exit sub
end if
if getvariable ("affliction_vomitingD") = "1" then
world.sendpush "drink choleric"
exit sub
end if
end sub

**************************************************

Like Flannel pointed out that instead of "world.sendpush, I should have just "send". Anything like that is appreciated.
USA #1
Well, my personal preference in auto-healers like this is not to operate directly on MC variables, but on VBScript variables. TO be honest, I don't know for sure that this is faster, but it would seem to me that it should be. However, if you need to use a hastbale/associative array type structure for some other reason, this doesn't really work. I suppose you could maybe use the Dictionary class or something, but that's beyond my knowledge.
#2
VBscript Variables? would you mind giving an example of how that would look/work?
USA #3
Well, instead of using:

getvariable ("affliction_aeonD") = "1"


you could declare the variable:

dim affliction_aeonD


initialize when the plugin loads:

sub OnPluginInstall()
  affliction_aeonD = cbool(getVariable("afflcition_aeonD"))
end sub


and use that variable:

if affliction_aeonD = vbTrue then
  ...
end if


and then save it at the end:

sub OnPluginSaveState()
  setVariable("affliction_aeonD",affliction_aeonD)
end sub
[\code]

hope that helps
#4
The variables are in an ever changing state though. I heard that VBscript variables wouldn't work in such a situation
USA #5
I'm not quite sure what you mean, as the definition of a variable in any language is to hold a value that may change, unless you declare it (in Java for example)
final
.

Of course you can change the value of a VBScript variable whenever you want.

dim v
v = "hello"
v = v & " Tim"
etc...

The MushClient variables will not be changing unless you tell them to do so either. If you have another plugin that affects this plugin's variables, in which case you must know the plugins unique id, then you might have a problem. However, I doubt that this is the case.
#6
After you initialize your variables in script as indicated above, you'd call a general catchall sub in your script with the appropriate triggers. Since I'm familiar with the application you're working on, we'll call this:


Sub AfflictOn(a,b,c)

DoCures()

End Sub


Name your trigger, for example:

Trigger: "your movement through the time stream is slowed."

The name of that trigger could be "Aeon1".

Within the affliction catch sub, we'd then do a simple check.


Sub AfflictOn(a,b,c)
If a = "Aeon1" Then
  affliction_aeon = vbTrue
Endif

DoCures()

End Sub


However, since you've got tons of these you'll be checking, use a Select statement instead of multiple Ifs. You'll pick up some speed there.

Now, whenever you get hit with your various affliction triggers, you'll call this Sub which will set the VBScript variable. Since that information only has to be passed once (per trigger hit) rather than making a callback to MC for every logic check, this will improve your speed nicely. Simply put, we're reducing the number of times that information has to get from MC to the script (getvariable).

You would do the same for cures, too.


Sub AfflictOff(a,b,c)
If a = "AeonCure1" Then
  affliction_aeon = vbFalse
Endif
End Sub


Pardon if the code is poor, it's been a while since I've dealt with VBScript. I'm pretty sure it's clean, though.
Amended on Sun 06 Mar 2005 04:57 PM by BBAlpha
Australia Forum Administrator #7
Quote:

The variables are in an ever changing state though. I heard that VBscript variables wouldn't work in such a situation


Variables which are *outside* a sub or function should retain their value for that session. You would need to initialise them (if necessary) when the script loads, and save them (say) at world close time.

You might consider using Lua, I did an example a while ago in the Lua section which shows how you can use a Lua variable to automatically get/put from MUSHclient variables with a lot less fiddling around, although "under the hood" it does the same thing, so it doesn't save time, it just looks neater.
#8
The best part about that little script, Nick, is the layer of abstraction it provides for my scripts. I tweaked it a little bit to return a number when it's a number, or true/false when it's a boolean keyword. I didn't have to change dozens of lines of code throughout my scripts. I simply changed the variable table script and everything that relies on it was automatically fixed. I love a good, clean interface (though my code can sometimes get messy and complex)...
USA #9
I've done a similar thing for variable management (in VBScript).
(I've already posted it in another thread, but since this was what it was designed for (and for Natasi too, for that matter), it makes sense to post here, just a straight copy and paste):

I've written a script that handles working with MCVariables as script variables (and specifically tailored to working with an array of related variables, a set of afflictions in this case, but it really could be anything, they could be as related as belonging to the same character, which would provide the same as Nick's table thing in lua, except it doesn't update to the world automatically).
You can download it here:
http://mud.bussett.com/arrays.zip

Documentation is in the script file, and the world file that comes with it shows a couple of examples (check the aliases) which basically do what you need. You can also email me and ask (emails at the top of the script file) about whatever.
It basically allows for arrays to be used (without being much more complicated than storing a value per affect, or spells, or defense in this case) as associative arrays (or a set of three arrays to be used as complete listings, see the aliases) since VBScript doesn't do associative arrays. As well as providing subroutines to save the values to MCVariables at connect/disconnect or whatever.
Amended on Thu 31 Mar 2005 05:42 PM by Flannel
#10
Oh! Flannel, your script looks just like what I need (Something like a java ArrayList or a ZMud List where you can add stuff, get rid of stuff at arbitrary locations, ect) but its in VB and I don't think Mushclient can run scipts from two languages at the same time. Any chance you could translate it into javascript for me? *grin* Guess its too much to hope for, so instead could you point me to any javascript equivalents if they exist?

EDIT: Actually I don't need this any more, someone has informed me that SQL does what I want and I realized I can use that in Mushclient.
Amended on Thu 19 May 2005 07:36 PM by Nexes
#11
If you make use of plugins, you can use a separate language in each plugin. One could use VBscript, another Python, and another one could use PerlScript.
#12
I guess I could try doing that, but then if I wanted to make any modifications I'd be in trouble since I can't read VB.
Amended on Fri 20 May 2005 06:10 AM by Nexes
#13
In my experience, learning one language well makes it very easy to learn other languages at least enough to know basically what they're doing. They've got similar constructs at the lower level, like conditional statements, looping commands, etc. Some will do classes, inline functions, or other more advanced things, but you can generally find a way to relate the language to something else you already know.

VBscript isn't so different in its syntax from Python, JavaScript, or Lua that you couldn't figure it out with maybe a little bit of help from others here.