Hugo: Getting the number of arguments in a routine

Hello - I’m trying to create a separate random() function** that will allow authors to randomly select items from a list (pretty much like what the Inform random() does). For example:

random(Treehouse, Park, Market) However, I haven’t found anything in the manual that would allow the author to get the number of arguments provided in a routine. :wink: Is there a way to do so? If not, is there a more elegant way (than the one I’m thinking) to code this in Hugo?

[size=85]**This routine will be named differently of course. Probably something like rnd() or rand().[/size]

I think you can do this by creating a Hugo array, and then getting the number of elements in the array. Check out page 78 of the Hugo book. I don’t think you want to find the number of arguments; if you’re passing them in from a routine you could create an array with them first and then count them.

That’s what I was seeing here. However, is it possible to enter a list of items enclosed in brackets instead as an argument?
For example: random([Item1,Item2,Item3])

With any luck Merk will chime in here. My guess is that Hugo doesn’t support that, sorry I can’t help you more than that :slight_smile:.

I hope you can get this to work. Library support for randomized events, etc. is an area in which Hugo seems to be lacking. I’m far to much of a novice (and a non-programmer) to offer my own suggestions. Thanks for going through the effort to look into this deficiency! :slight_smile:

I would need to do some research. I think when I’ve done this before (check my “IsEqual” function in utils.hug in the Traveling Swordsman source) I get the number of parameters by checking to see if the value passed in with zero. If the function allows 5 parameters (for instance) and you only send 2, then the last 3 will be received by the function automatically as 0’s. That’s probably not ideal if 0 (or the “nothing” object) is a valid value for your function.

Another way – not hands-off, but should work – would be to specify the number of arguments as the first parameters. I.e., MyFunction(parmcount, parm1, parm2, parm3, parm4, parm5, …) called as MyFunction(2, houseobj, w_obj).

If I come up with a better idea I’ll let you know. It seems like there should be a parameter count function automatically, but offhand I don’t know what it would be.

(edit) Another thing you could do, kind of thinking outside the box on this, is just randomize among the maximum number of parameters, and keep looking until the randomly picks one that’s non-zero. As long as at least one non-zero parameter (i.e., a real object reference) is sent in, the routine should pick up one without looping too many times. I suspect you wouldn’t see a delay or anything in running the code, but you’d want to try it first.

! Based on code by Roger Firth
array randArgs[8]

routine rand(a1, a2, a3, a4, a5, a6, a7)
{   local n, r
    randArgs[1] = a1, a2, a3, a4, a5, a6, a7
    for (n=7; n>0; n--) {
        if (randomArgs[n])
            break
    }

    if (n>1)
        r = randArgs[( random[n] )]
    else
        r = random(a1)

    for (n=7; n>0; n--)
        randArg[n] = 0

    return r
}

This may not work properly if you hand rand a false Hugo value (0, “”, false, etc.).

Somewhat patronising explanation. I think it’s pretty clear, but really quick: 1. The arguments are passed into the array randArgs. 2. The array is checked from its end for a false value, in which case it breaks, and n is equal to the index of the last non-zero value. 3. If n is greater than one, a random element of the array, one of the arguments you passed rand, is assigned to r. Otherwise, if only one argument was passed, it behaves as if you just called random. 4. We flush the array, so that it’s ready to use again. 5. We return r.

This will probably not play well if you hand it a single string or object. Improvements? This could be expanded to take up to 13 arguments; written to use randArg[] (to get the length of the array) in the for loops; written not to throw away randArg[0]; written to play nice with false values.

Unfortunately, I haven’t needed any of that… I may have an improved version sitting around on some hard drive. I’ll post it if that happens to be the case.

Well, good luck!

It was said in the Hugo Book (I can’t remember where exactly) that Hugo only allows a certain number of arguments for routines. I can’t remember the exact number, but I do remember it was a bigger number than that for Inform.

In any case, I’ll check vamping’s code out and see. Sorry for the delayed reply. :slight_smile: