I am confused about how to define a JS function in the Nexus web client.
I have a code block that works perfectly when pasted into an "Execute Script" action, however the same block of code pasted into a "Function" reflex fails to take affect when called from the "call function" action in an alias or trigger.
I have tried multiple ways of decorating the JS code in the function declaration but I can not find what I am doing wrong.
Can anyone offer some advice?
0
Comments
It happened for me when I was trying to do what you describe in a package. The resolution is to write all of your code in the onLoad function
You start it with
client.packagename = (function () {
"use strict";
//all of your code stuff here for example
const exampleFunction1 = function() {
//stuffff
};
const exampleFunction2 = function(avariable) {
//thingys
};
return Object.freeze({
exampleFunction1,
exampleFunction2
});
}());
Then you can call the functions using Execute Script, for example packagename.exampleFunction1. Someone over on Achaea figured it out a while back and there's a tutorial somewhere on their forums that covers parts of it.
Have you looked at this http://nexus.ironrealms.com/Functions
I've written and called lots of functions in Nexus and not had any issues.
It happens automatically around the same time as commune/city messages pop up when you're logging in, though that does mean that if you've got a bug and you just quit/login, it will still be buggy for those first few seconds, it's generally noticable when it's something called by onGMCP
There won't be any issues with the script, you can even directly copy everything from the other package and into the main package to get the script to work fine. But yeah, if the function is not in the main package then it just won't run, someone figured out why it happens and it's been sent through as feedback, not sure if it's meant to be fixed or not but if it's still happening then probably not.
and using this format works just fine
run_function("combatLoad","", "Offense")
This is being called out of onLoad in the web client on both those IRE(s) and working properly. When I get a chance to start dropping packages into the system here I'll double check.
I have the main package of reflexes with generic utility reflexes and then separate packages of reflexes for influence, hunting, there will eventually be one for pvp also.
In the influence package I created a background reflex to run continuously on a timer randomly rotating mindsets.
it works something like this:
CMD1:
set variable loopmindset =1
Repeat while loopmindset = 1
{
set variable randomMinimum=1
set varable randomMaximum=3
execute script: JS using Math.random and some manipulation to give a random number within the specified min/max
set a mindset according to the random value determined by JS
wait for a few seconds
}
CMD2:
set variable loopmindset=0
This command pair works well and does exactly what I want.
Now to the problem!
Having done this and considered that there are many other users for random numbers, either for debating, RP or possibly to add a wild factor into hunting/combat, I decided that my implementation was not very elegant, and that it would be much better to have a "getRandomNumber(min,max)" function defined that I could call from multiple packages where I wanted to use it.
As an initial step I tried porting the trivial random number JS from the "execute script" action of CMD1 in the influence package to a Function in the influence package and replacing "execute script" with "call function".
This is where I have drawn a total blank.
I select the "call function" sub action and get no errors, but no results either.
I also noted that the "call function" action does not allow any arguments.
How do you recommend I fix this?
1. instead of using the call function option just execute a script and call the function either as a JavaScript function or call it like I do with the run_function call that I showed in the last post
2. While and for loops are a bad thing in JavaScript, why you ask because JavaScript is single threaded and they are blocking functions, which means while either are in effect nothing else is processing. So if you have an array you are looping through or a counter you are stepping through,nothing else is going on inside the client. Affliction notices are not coming in, attack notices, GMCP, etc... this is one of the biggest issues with Nexus vs Mudlet, MUSH etc... They can all have these nice big arrays and for loops and while loops and not impact the client. Now you might think oh hey it only takes a few milliseconds to process them so no biggie, I will tell you it gets to be a major issue in PvP when your for/while loops are firing over and over.
@Saran I just checked and I am able to directly call functions on Lusty in packages just like I do on the other IRE games, so perhaps the drop down box run function does not work but you can still do it.
It's actually a bit easier in the long run, so I have no plans to stop using it unless they actually do something to prevent its use. Which would probably shut down the various examples I've found in achaea that use it.
eta*
now I've not done any testing with things like worker queues or node.js to try and address this since the client update but they did not work on the past builds of the client
Worst case I can write perfect code, copy past it as needed and pray to never make a change ever......
(It used to be that you could overload the default commands by tagging them ,false in the javascriptsend_command function so that you could use "look" to trigger something and not have it drop into an infinite loop. Alas, no more).
If you store the time of the last time you changed mindsets, you can then compare it to the current time and if the difference is more than however long you want you can then fire the changing function.
Also, if you like I can send you Jairani, it's an influencer that I've written for nexus, not as complete as i'd like and at the moment it requires stratagems and trans influencing. (It'll also set mindsets for you, try to apply oils, and wear scarecrow hats).
this is more than mindsets, I want to use random numbers to generate random reactions for several things.
examples:
Random RP action from a set of similar emotes
Random mindset for debates
Random direction chooser when exploring new areas
I would prefer to have a good way of handling random numbers since this seems to be lacking in the client as a general "quality of life" improvement.
On the other topic - please send me Jairani - I would prefer to build my own system based on my capabilities, but it would be interesting to see another implementation and learn from the approach taken and areas focused on.
I have never used strategems yet so that could be an interesting side learning opportunity!
So if you set an alias for "look" you can attach the following as a script:
One oddity you may notice is that the display notice actually processes before the look command. That was something we noticed a lot of in MKO too, so many of the notices for things actually had a delay attached. Also only "look" is overloaded "l" will send look as normal.
As for the random emote thing. How about this. Set an alias for "crazy" and attach this as a script:
That will send a random emote anytime you type "crazy". If you want to type it once and have it on a timer you could use setInterval(). If you further want it to sometimes do nothing, just increase the random number and leave some of the cases, and/or the default blank.
first in onLoad put:
var randnum //this lets us call this variable across our functions. There is probably a way to do the same thing with namespacing, but it was (redacted) me off.
randomnumber=
getnum=
//Enter the function here
That works just fine and returns a random number when you type getnum.
Say you want to pass the function a min and max number, and have it calculate from that. You can grab the arguments and pass them along, though I couldn't get it to send them separately ie (min,max) only passes the second argument, and min is lost. (min+max) passes them along as one thing, which you can separate on arrival. Here are the test functions I was using for this, again both are in a package called Functions:
argtest
//Enter the function here
otherfunction
//Enter the function here
so typing "argtest 4 5" returns:
min: 4
max: 5
all args: 45
args sorted: 4 5
I'll leave it up to you to figure out the code to turn the min max numbers into an appropriate range for your random variable.
FWIW here is the maths to return a random number between min and max:
Here are my updated functions for random numbers with an example that works for any number of digits in min and max with a little default error handling thrown in.
Package:-- main package --
Function:onLoad
Package:Functions
Function:randomnumber
Package:Functions
Function:rmind