Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Problem with passing/processing macro parameters (Sugarcane, Javascript)

edited March 2016 in Help! with 1.x
I want my playsound macro to work both with constant names and expressions, like
<<set $wav=".wav";i=2>>
<<playsound "fall"+$i+$wav>>

In the handler
handler: function (a, b, c, d) {
      var s = (d.fullArgs());
domacro is the function that does the main job:
domacro: function(b, s) {
      if (s) {
alert(s);//for debugging
// cetera...
<<playsound "fall2.wav">>
s becomes "fall2.wav", all works.
<<playsound "fall"+$i+$wav>>
s becomes state.history[0].variables.wav == null && (state.history[0].variables.wav = 0);state.history[0].variables.i == null && (state.history[0].variables.i = 0);"fall"+state.history[0].variables.i+state.history[0].variables.wav
(yes,I copy-pasted this string exactly as it was displayed by the alert box)
So far so good, unless I need a second parameter:
<<loopsound "fall"+$i+$wav $times>>
s becomes state.history[0].variables.times == null && (state.history[0].variables.times = 0);state.history[0].variables.wav == null && (state.history[0].variables.wav = 0);state.history[0].variables.i == null && (state.history[0].variables.i = 0);"fall"+state.history[0].variables.i+state.history[0].variables.wav state.history[0].variables.times

So, the problem is: I need to split s to send each parameter to eval(Wikifier.parse()), but the parameters codes are separated by spaces, and the same spaces are used inside their codes. There is no way to understand where one parameter ends and the next begins!
I can replace " == " with "==" and " = " with "=" and " && " with "&&", but I'm not sure if any more complex expressions can produce more kinds of spaces. Any ideas?


  • yunyun
    edited March 2016
    BTW, is there any way to get the amount of parameters passed to macro? Some property of d (the last argument passed to the handler(a,b,c,d)), maybe?
    c.length will not do the job, as is case of an expression it returns its parts as different parameters:
    <<loopsound "fall"+$i+$wav $times>>
  • The value of c.length is correct because unless told otherwise quotes and white-space are used to delimit a parameter, so your last example consists of three parameters:
    <<loopsound "fall"+$i+$wav $times>>

    You can use open and close parenthesis to indicate that a parameter is an expression like so
    <<loopsound ("fall"+$i+$wav) $times>>
Sign In or Register to comment.