Alias using function not working

AubreyAubrey in hot place
This was my pentagram alias:

if pentagram == false and dwield == true then

send("carve pentagram")

elseif pentagram == false and dwield == false and dgrasp == true then

send("wield daegger")

omni.doAdd("carve pentagram")

elseif pentagram == false and dwield == false and dgrasp == false then

send("summon daegger")

omni.doAdd("wield daegger")

omni.doAdd("carve pentagram")

elseif pentagram == true then

send("dispel pentagram")

end


It's too long so i put it in function pentagram().

But when I put pentagram() in the scripting box for pentagram alias, it does not work. What can be the problem?

Comments

  • Did you initialize pentagram to be either false or true?

  • edited May 2014
    Hard to tell without debugger (but either of the variables being undefined is most likely), but code in a function vs. code in an alias should run exactly the same.

    Making sure your function definition and function call don't have a typo might be a good place to look too.

    Also, if the name of your function is "pentagram()" then that is your problem. Change it, and you'll be fine.

    Lastly, is this -all- the code in your alias/function?

    If none of this works for you, catch me in game in a little while, and we'll go to the debugger.

    Also, as a tip, you should change your script to use: if pentagram them, not if pentagram == false then. It's better coding practice, runs faster, and handles undefined (nil) variables automatically (returns false for nil).
  • AubreyAubrey in hot place

    I have script for initial stats, @Crixos. Hi.

    changing to pentie() made it work.

    @Ernam, how will that work? If pentagram then --> does it mean if pentagram variable exists? I do not know how to code using if pentagram and other conditions then. :(

  • edited May 2014
    If pentagram then
    -- code
    end

    Will execute the code if pentagram is equal to anything other than false or nil.

    The way you wrote it is perfectly fine, but would "crash" out if pentagram is nil.

    For a "false only" if statement, you could use:

    If pentagram then else --// or if not pentagram then
    --code
    end

    This, however, would execute the code if pentagram is not defined, however, which in your example would be better.
  • if pentagram then  <-- fires if pentagram is not the boolean false or the undefined nil


    if not pentagram then  <-- fires if it IS the boolean false or the undefined nil


    So, for example:

    if not pentagram and dwield then

    send("carve pentagram")

    elseif not pentagram and not dwield and dgrasp then

    send("wield daegger")

    omni.doAdd("carve pentagram")

    elseif not pentagram and not dwield and not dgrasp then

    send("summon daegger")

    omni.doAdd("wield daegger")

    omni.doAdd("carve pentagram")

    elseif pentagram then

    send("dispel pentagram")

    end


    image
    Cascades of quicksilver light streak across the firmament as the celestial voice of Ourania intones, "Oh Jarrod..."

  • AubreyAubrey in hot place
    edited May 2014
    Thank you for the examples, I will use them. :)

    Oh, if I use if and not, can I remove my initial scripts? It is just variables set to false.
  • edited May 2014
    By the way, in case you were curious about the cause of your problem, when you define a function in lua, it essentially saves the function as a variable.

    So: function pentagam() .... end -- basically saves your function in a variable named "pentagram". However, you were already using that variable, so you were basically overwriting it each time you set pentagram to true or false. (Thus, when you call pentagram(), nothing was happening, as it was a Boolean value, and no longer a function.)
  • I'd keep your initialize scripts, while it might not be necessary for this script to fire properly (if defaults would be false, for example), it's a good coding habit to prevent things not working for unknown reasons down the line.

    image
    Cascades of quicksilver light streak across the firmament as the celestial voice of Ourania intones, "Oh Jarrod..."

Sign In or Register to comment.