Recursive Function (bug or my own stupidity?)

Post Reply
jonhutch01
Posts: 6
Joined: Tue May 04, 2010 3:29 pm

Recursive Function (bug or my own stupidity?)

Post by jonhutch01 »

Let me first explain the reason for this little system, how I implemented it, and what it's doing. Then afterwards feel free to point out my stupidity. ;)

After a fight I want to pick up (in my case "palm") the corpse I just killed. I have the body targeted and its name set in the rh_target variable through an alias. Since this is an IRE game, I have to wait until I have balance before I can pick it up. I have a balance boolean variable that is set based off my status prompt and by the trigger "You have regained balance". So I wait until I have gained balance then I want to send the command to palm (pick up) the body.

Ok so this is how I did it. I set up a trigger that let's me know when the fight is over ("You have slain (.*).") and then fire this:

Code: Select all

rh_palmBody(rh_target)
the rh_palmBody(target) is a function I put in a script. It looks like this:

Code: Select all

function rh_palmBody(target)
    if balance == true then
        send("palm " .. target)
    else
        tempTimer(1, rh_palmBody(target))
    end
end
As you can see, I first check to see if the balance variable has been set to true. If it has I go ahead and send the command to palm the target (whatever was passed in), otherwise, I set up a timer to wait 1 second and call the function again, in theory, checking again to see if balance has been set to true and so on until it finally is true and sends the command.

However, when I run this, some unexpected things happen. Most notably that the tempTimer seems to fire a LOT more than once every second (put an echo() in there to see what I mean).

I know I'm missing something here, but for the life of me I can't seem to see it. Any guidance would be greatly appreciated! Thanks! :D

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Recursive Function (bug or my own stupidity?)

Post by Vadi »

Yeah that's a bit silly to go about it.

Why not have:

Code: Select all

function rh_palmBody(target)
    if balance == true then
        send("palm " .. target)
    end
end
and call rh_palmBody on the 'you have got balance' message? (and appropriately not call it or unset target for when you don't want to pick up)

jonhutch01
Posts: 6
Joined: Tue May 04, 2010 3:29 pm

Re: Recursive Function (bug or my own stupidity?)

Post by jonhutch01 »

I think I see what my problem was in designing this. I'm still getting use to triggers. Basicly I'm going to need to check a flag every time my balance updates to see if I need to palm the target or not, like wise any other action I need to do after a battle will need to be flagged (and after it's completion unflagged).

I suppose I was trying to keep from having so many checks occurring when the trigger that updates my balance gets fired from the prompt (which pretty much happens every other line).

Since most of the actions I do after the battle put me off-balance, I was just trying to figure out a way to run those commands once, and then have the system not worry with checking for them the rest of the time.

I'm going back to take a look at this again (once I can get the mapper working again, worked yesterday, but not today, rather odd) and see if I can come up with a better way of solving this.

In the mean time. Recursive functions... bad? I thought they were valid inside of Lua, but to be fair I'm not overly familiar with Lua so I might be doing something completely wrong.

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Recursive Function (bug or my own stupidity?)

Post by Vadi »

Recursive functions are fine, just like in any language... provided you use them properly

naftali
Posts: 138
Joined: Wed Jan 20, 2010 8:42 pm

Re: Recursive Function (bug or my own stupidity?)

Post by naftali »

also tempTimer(1, echo("boing") ) will do the echo when tempTimer() is called, not after 1 second. To have it work after 1 second use this:

tempTimer(1, function () echo("boing") end )

except instead of echoing boing have the function be whatever you want it to be, obviously. Just FYI, because Vadi is right about there being a better way to do this.

jonhutch01
Posts: 6
Joined: Tue May 04, 2010 3:29 pm

Re: Recursive Function (bug or my own stupidity?)

Post by jonhutch01 »

Thank you naftali for explaining that. I was unclear at what point the tempTimer ran the code. That's good information. I have gone a different route with this and instead have opted to use flags to signal when certain things should and shouldn't be called. Thanks for your help guys! :)

Post Reply