Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ function return and a simple if statement

function return and a simple if statement

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1 2  

Posted by Gore   (207 posts)  Bio
Date Thu 01 Mar 2007 02:46 PM (UTC)
Message
What anyone see what I'm doing incorrectly? This if statement will fire regardless if I'm at max health or not. I've tested the max_hp () max_mp () functions to return 1 or 0 when I'm at max health/mana so I think the issue is with the arguments of the if statement?

if def.vitality == 0 and def.vitality_timer == 1 and max_hp () and max_mp () then
  Send ('vitality')
end

function max_hp ()
  if hp.cur == hp.max then return 1; else return 0; end
end

function max_mp ()
  if mp.cur == mp.max then return 1; else return 0; end
end


Do I need to do if max_hp () == 1 then ?
Top

Posted by Gore   (207 posts)  Bio
Date Reply #1 on Thu 01 Mar 2007 03:12 PM (UTC)

Amended on Thu 01 Mar 2007 03:13 PM (UTC) by Gore

Message
and also shouldn't I be able to do

if not def.vitality and def.vitality_timer then

if def.vitality and def.vitality_timers only have the values of 0 and 1?
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #2 on Thu 01 Mar 2007 05:38 PM (UTC)
Message
0 is not false in Lua. If you want to return false or true, you should return false or true, not zero or one.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #3 on Thu 01 Mar 2007 07:55 PM (UTC)
Message
David is quite right, the Lua manual warns you about that. Also your functions could be written more simply:


function max_hp ()
  return hp.cur >= hp.max
end


You want to return a boolean, so simply do a test and return the result, in one operation.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #4 on Thu 01 Mar 2007 07:57 PM (UTC)
Message
Quote:

if not def.vitality and def.vitality_timer then


Sure that will work, providing you store true or false into those fields, not 0 or 1.

Get into the habit of using the boolean values true and false, if that is what you intend. Trying to use numbers is a C way of doing things.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Shaun Biggs   USA  (644 posts)  Bio
Date Reply #5 on Thu 01 Mar 2007 09:36 PM (UTC)

Amended on Thu 01 Mar 2007 09:38 PM (UTC) by Shaun Biggs

Message
You can also remove the extra functions there. Having a function for the simple comparison of hp or mp just adds an extra layer of obscurement in there.

if def.vitality and def.vitality_timer and ( hp.cur >= hp.max ) and ( mp.cur >= mp.max ) then
  Send ('vitality')
end

Is just a bit easier to read. That is assuming that you change def.vitality and def.vitality_timer to booleans instead of integers.

It is much easier to fight for one's ideals than to live up to them.
Top

Posted by Gore   (207 posts)  Bio
Date Reply #6 on Fri 02 Mar 2007 06:18 AM (UTC)
Message
wow, thanks for all the advice I will definately change them to true and false!
Top

Posted by Gore   (207 posts)  Bio
Date Reply #7 on Sat 03 Mar 2007 10:43 PM (UTC)
Message
(lua newb incoming)

For some reason I'm having some issues with using if statements in the if variable then form.

I have no clue why, can anyone help me?

1: function pronequeue ()
2:   if auto.queue and auto.stand and not aff.sleep then
3:     if aff.prone then Send ("stand") end
4:   end
5: end


line 3 fires whether aff.prone is true, or aff.prone is false? How come?
Top

Posted by Gore   (207 posts)  Bio
Date Reply #8 on Sat 03 Mar 2007 11:07 PM (UTC)
Message
Compile error
Immediate execution
[string "Timer: "]:1: ')' expected near '<eof>'

Also what might that mean?
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #9 on Sat 03 Mar 2007 11:09 PM (UTC)
Message
What is the exact content of aff.prone?

As for the compile error, that means that you left out a closing parenthesis somewhere. Chances are you just need to add it to the end but I'd look more carefully at your code and see if you made another mistake somewhere.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Gore   (207 posts)  Bio
Date Reply #10 on Sun 04 Mar 2007 12:05 AM (UTC)
Message
aff= { }
aff=
  {
  prone=true,
  sleep=false,
  }

function sit (n,o,wc)
  auto.stand = false
  Send ("sit")
  ColourNote(color.fg, color.bg, "[ auto.stand: off [stand] to resume ]")
end

function stand (n,o,wc)
  auto.stand = true
  pronequeue ()
  ColourNote(color.fg, color.bg, "[ auto.stand: on ]")
end

function afflict.prone (n,o,wc)
  aff.prone = true
  pronequeue()
end

function cure.prone (n,o,wc)
  aff.prone = false
end

function pronequeue ()
  if auto.queue and auto.stand and not aff.sleep then
    if aff.prone == true then Send ("stand") end
  end
end
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #11 on Sun 04 Mar 2007 12:55 AM (UTC)
Message
Quote:

if aff.prone == true then Send ("stand") end


This line is OK, although personally I would write it as:


if aff.prone then 
  Send ("stand") 
end -- if prone


How is all this structured? Are you sure aff.prone is really false? Are you sure the "stand" comes from this function? You could add debugging like this:


print ("aff.prone type =", type (aff.prone)) -- make sure boolean
print ("aff.prone =",aff.prone) -- show value
if aff.prone then 
  Send ("stand") 
end -- if prone


If you re-execute the code that does this every time:


aff=
  {
  prone=true,
  sleep=false,
  }


Then naturally aff.prone will always be true. I am hoping this is in a script file. Also if you reload the script file during testing, that will reset it to true.

Quote:

Compile error
Immediate execution
[string "Timer: "]:1: ')' expected near '<eof>'


You need to show your code if you want help on that one.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Gore   (207 posts)  Bio
Date Reply #12 on Sun 04 Mar 2007 01:27 AM (UTC)

Amended on Sun 04 Mar 2007 01:30 AM (UTC) by Gore

Message
Ah, thanks I changed aff.prone to initialize as false.

And thanks for the debug, I tried it and it seems that somehow aff.prone is a number :/

You have recovered equilibrium.
aff.prone type = number
aff.prone = 0
1374(81)h, 1477(93)m, 1580(100)e, 10p elrx-
You stand straight up.
1374(81)h, 1477(93)m, 1580(100)e, 10p elrx-


I have no clue where it would get the idea that it was a number.

Ahhh, I feel silly. When I updated all of my variables to boolean, I missed one important thing, in my prompt function I have this:

if string.find (defs, "p") ~= nil then aff.prone = 1 else aff.prone = 0 end
if string.find (defs, "x") ~= nil then bal.bal = 1 else bal.bal = 0 end
if string.find (defs, "e") ~= nil then bal.eq = 1 else bal.eq = 0 end
if string.find (defs, "l") ~= nil then bal.la = 1 else bal.la = 0 end
if string.find (defs, "r") ~= nil then bal.ra = 1 else bal.ra = 0 end


I'm assuming all will be fine after I fix that.

Top

Posted by Gore   (207 posts)  Bio
Date Reply #13 on Sun 04 Mar 2007 01:35 AM (UTC)
Message
1374(81)h, 1477(93)m, 1580(100)e, 10p elrx-
You sit yourself down.
aff.prone type = boolean
aff.prone = true
1374(81)h, 1477(93)m, 1580(100)e, 10p elrxp-
You stand up and stretch your arms out wide.
1374(81)h, 1477(93)m, 1580(100)e, 10p elrx-


Looks like everything is in check, sorry about that, but thank you for your help.

Just to confirm

if not variable then

will execute if the variable is false

if variable then

will execute if the variable is true, will either of those execute if the value of variable is ANY other value? Like .5 or "hi" etc? Or will if variable then consider variable true because it's not false or nil?
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #14 on Sun 04 Mar 2007 02:11 AM (UTC)
Message
variable is true whenever variable is not false, and not nil. not variable is true whenever variable is false, i.e. not variable is true when variable is false or nil.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


58,544 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.