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.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Quick simple syntax stacking issue?

Quick simple syntax stacking issue?

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


Posted by Simimi   (40 posts)  Bio
Date Sat 01 Sep 2007 12:47 PM (UTC)
Message
This is a quick question I promise. I seem to have some sort of nested error in my script here. It seems I have just fudged the synatx.

I have never tried to stack so many ifs before...


if var.priority == "health" then
      if tonumber (var.health) <= (.90 * tonumber (var.maxhealth)) then
	    if bals["potion"] == true then 
          Send ("drink health")
         end
      end 
    end


The script compiles just fine, but it does not send the command if the health is indeed below 90%. I know it must be something simple, as I keep trying with different attempts using various forms of 'then' and 'do' and 'and if' etc with different errors in the compile.
Top

Posted by Shaun Biggs   USA  (644 posts)  Bio
Date Reply #1 on Sat 01 Sep 2007 05:20 PM (UTC)
Message
Have the script print out the appropriate values before the if statements to check what values are being compared.

print( var.priority, var.health, var.maxhealth, bals.potion )
if var.priority == "health" then
  if tonumber (var.health) <= (.90 * tonumber (var.maxhealth)) then
    if bals["potion"] == true then 
      Send ("drink health")
    end
  end 
end

This way you can make sure that the values are correct before comparison. Sometimes you wind up surprised at what is being sent in when you test the values.

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

Posted by Simimi   (40 posts)  Bio
Date Reply #2 on Sat 01 Sep 2007 11:57 PM (UTC)
Message
Thank you so much, that does help quite a bit. But now the same problem arises from the old sipper (before I wrote this one to try and fix it.)

I have the following actions trigger bals.potion to do various thing.

1)Sipping a vial sets potion balance to nil
2)Suceeding in a sip (you get a healing message) sets it to "curing"
3)Failing a sip, sets balance back to nil

This is the if check that precedes the previous block of code.


  if bals["potion"] == "curing" or nil then
    return
  end


The problem is, it will keep drinking potions if my health takes a huge hit, say, 80% of the total, eventhough balance is indeed set to nil or "curing". Does it need to return nil? I thought return and return nil were the same, contextually?
Top

Posted by Simimi   (40 posts)  Bio
Date Reply #3 on Sun 02 Sep 2007 12:07 AM (UTC)
Message
 if bals["potion"] == "curing" or
     bals["potion"] == nil then
    return
  end
  


Changed the above to that and it seems to be working nicely, so far...
Top

Posted by Onoitsu2   USA  (248 posts)  Bio
Date Reply #4 on Sun 02 Sep 2007 12:38 AM (UTC)
Message
You cannot compare a value to two values using the first method, that is why the second method worked. The only way is to use parenthesis to group the check, and even that is iffy.

-Onoitsu2
Top

Posted by Simimi   (40 posts)  Bio
Date Reply #5 on Sun 02 Sep 2007 12:48 AM (UTC)
Message
I do have one question though... right now I have;

function autosipper() --start function
--balance checks/ chug failsafes
  if var.autosipper == nil then
	return
  end

  if bals["potion"] == "curing" or
     bals["potion"] == nil then
    return
  end
    
--the sipping priority loops
print( var.priority, var.health, var.maxhealth, bals.potion )
if var.priority == "health" then
  if tonumber (var.health) <= (.90 * tonumber (var.maxhealth)) then
    if bals["potion"] == true then
      Send ("drink health")
    end
  end
end


Now by var.autosipper being nil I mean the word nil is in the variable value box inside of Mush. I have tried it with "nil" and false and "false" also. I also tried if not true...

No matter how I set var.autosipper, the print still happens, which leads me to believe (I hope falsely) that it is continuing down the script. It is supposed to not continue on if var.autosipper isn't true or if bals["potion"] are absent.

Sometimes it does not print at all even if var.autosipper are true AND potion balance is true... and the other variables are correct.
Top

Posted by Shaun Biggs   USA  (644 posts)  Bio
Date Reply #6 on Sun 02 Sep 2007 12:54 AM (UTC)
Message
Quote:
Does it need to return nil? I thought return and return nil were the same, contextually?

It is different... sort of. I would recommend always returning nil if you want nil, just to be on the safe side.

Lua 5.1.2  Copyright (C) 1994-2007 Lua.org, PUC-Rio
> function foo() return end
> function bar() return nil end
> print( foo() )

> print( bar() )
nil
> a = foo()
> print( a )
nil
> a = bar()
> print( a )
nil
>

However, this part is the problem...

if bals["potion"] == "curing" or nil then
  return
end

Lua 5.1.2  Copyright (C) 1994-2007 Lua.org, PUC-Rio
> print( "curing" == "curing" or nil )
true
> print( "foo" == "curing" or nil )
nil
> print( nil == "curing" or nil )
nil
> print( ( nil or "curing" ) == "curing" )
true
> print( ( "foo" or "curing" ) == "curing" )
false
> print( ( "curing" or "curing" ) == "curing" )
true
>

The or in this case will check to see if "curing" is nil, and if it is nil, it will use nil instead. The only possible answers are true and nil. This should do the trick:

if ( bals["potion"] or "curing" ) == "curing" then
  return nil
end

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

Posted by Simimi   (40 posts)  Bio
Date Reply #7 on Sun 02 Sep 2007 01:04 AM (UTC)
Message
so using.

if bals["potion"] == "curing" or
     bals["potion"] == nil then
    return nil
  end


Is a no?
Top

Posted by Simimi   (40 posts)  Bio
Date Reply #8 on Sun 02 Sep 2007 01:07 AM (UTC)
Message
Here is a log of it in action.

372h, 3152m, 2780e, 10p, 15590en, 14660w esSilrx-<09:05:50.689>
You exclaim, "Stop!"
health 372 3338 true
drink health
372h, 3152m, 2780e, 10p, 15590en, 14660w esSilrx-<09:05:51.113>
You take a drink from an amethyst vial.
The potion heals and soothes you.
866h, 3152m, 2780e, 10p, 15590en, 14660w esSilrx-<09:05:51.420> <+494H>
You take a drink from an amethyst vial.
The potion flows down your throat without effect.
866h, 3152m, 2780e, 10p, 15590en, 14660w esSilrx-<09:05:51.817>

Doman utters a deep, rumbling laugh.
866h, 3152m, 2780e, 10p, 15590en, 14660w esSilrx-<09:05:54.545>

You may drink another health, mana, or bromide potion.
health 866 3338 true
drink health
866h, 3152m, 2780e, 10p, 15590en, 14660w esSilrx-<09:05:55.369>
You take a drink from an amethyst vial.
The potion heals and soothes you.
1331h, 3152m, 2780e, 10p, 15590en, 14660w esSilrx-<09:05:55.631> <+465H>

The bug is sometimes it tries to drink twice, I can not figure out why... It is a rare fluke, but a fluke none the less.
Top

Posted by Shaun Biggs   USA  (644 posts)  Bio
Date Reply #9 on Sun 02 Sep 2007 01:11 AM (UTC)
Message
I would have the autosipper test be backwards of how you currently have it.

function autosipper() --start function
--balance checks/ chug failsafes
  if var.autosipper then
    -- code that should run goes here
  end
end

I'm a bit confused with what bals.potion is supposed to store though. You are testing it for "curing", true, and nil in one function. Keeping variables only one out of strings, tables, numbers, and booleans will help a lot with code readability.

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

Posted by Simimi   (40 posts)  Bio
Date Reply #10 on Sun 02 Sep 2007 01:23 AM (UTC)
Message
Ahh ok, well this is how it works. We have a table, bals

bals = {}


And the triggers set those table values to "curing" (an intermediary value), nil (0) and true (1) respectively. Nil removes it from the table, obviously, so with nothing in the table, nothing can be done. It is also not supposed to be done while "curing" is up.

-Drinking from a vial sets it to nil
-Getting the healing message from a vial sets it to curing
-Failing to drink a vial (it slides down your throat because you do not have balance) sets it to nil
-Regaining balance sets it to true

currently using

 if bals["potion"] == "curing" or
     bals["potion"] == nil then
    return nil
  end

if var.autosipper == "true" then
--the sipping priority loops
print( var.priority, var.health, var.maxhealth, bals.potion )
if var.priority == "health" then
  if tonumber (var.health) <= (.90 * tonumber (var.maxhealth)) then
    if bals["potion"] == true then
      Send ("drink health")
    end
  end
end
end


Seems to be working, though it still wants to occasionally sip twice...
Top

Posted by Simimi   (40 posts)  Bio
Date Reply #11 on Sun 02 Sep 2007 01:37 AM (UTC)
Message
Well the double sip bug was "fixed" but having the sending of "drink health" to set bals.potion to "curing" pre-emtively, just incase!
Top

Posted by Simimi   (40 posts)  Bio
Date Reply #12 on Sun 02 Sep 2007 02:01 AM (UTC)
Message
and what is;

[string "Script file"]:81: '<eof>' expected near 'end'


End of Function espected near end... no matter how many 'ends' I put down there past line 81 it still gives this error.


function autosipper() --start function
--balance checks/ chug failsafes

  if bals["potion"] == "curing" or
     bals["potion"] == nil then
    return nil
  end

if var.autosipper == "true" then
--the sipping priority loops
print( var.priority, var.health, var.maxhealth, bals.potion )

if var.priority == "health" then
  if tonumber (var.health) <= (.90 * tonumber (var.maxhealth)) then
    if bals["potion"] == true then
      Send ("drink health")
      bals.potion = "curing"
    end
  elseif tonumber (var.mana) <= (.75 * tonumber (var.maxmana)) then
    if bals["potion"] == true then
	  Send ("drink mana")
	  bals.potion = "curing"
	end
  elseif tonumber (var.ego) <= (.65 * tonumber (var.maxmana)) then
    if bals["potion"] == true then
	  Send ("drink ego")
	  bals.potion = "curing"
	end
  end
end
end

if var.priority == "mana" then
 if tonumber (var.health) <= (.80 * tonumber (var.maxhealth)) then
    if bals["potion"] == true then
      Send ("drink health")
      bals.potion = "curing"
    end
  elseif tonumber (var.mana) <= (.85 * tonumber (var.maxmana)) then
    if bals["potion"] == true then
	  Send ("drink mana")
	  bals.potion = "curing"
	end
  elseif tonumber (var.ego) <= (.65 * tonumber (var.maxmana)) then
    if bals["potion"] == true then
	  Send ("drink ego")
	  bals.potion = "curing"
	end
  end
end
end

if var.priority == "ego" then
 if tonumber (var.health) <= (.80 * tonumber (var.maxhealth)) then
    if bals["potion"] == true then
      Send ("drink health")
      bals.potion = "curing"
    end
  elseif tonumber (var.mana) <= (.65 * tonumber (var.maxmana)) then
    if bals["potion"] == true then
	  Send ("drink mana")
	  bals.potion = "curing"
	end
  elseif tonumber (var.ego) <= (.85 * tonumber (var.maxmana)) then
    if bals["potion"] == true then
	  Send ("drink ego")
	  bals.potion = "curing"
	end
  end
end
end
end
end
Top

Posted by Simimi   (40 posts)  Bio
Date Reply #13 on Sun 02 Sep 2007 02:10 AM (UTC)
Message
I can't seem to find the "edit post" function, but, thanks for all of your help, I solved the eof issue by spacing the functions better.
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #14 on Sun 02 Sep 2007 07:04 AM (UTC)
Message
Quote:

... I mean the word nil is in the variable value box inside of Mush ...


The word "nil" is a completely different thing than testing for nil. In Lua, nil means "no value" (as in, this thing doesn't exist).

If you want to test for the word "nil" then you should explicity do so. eg.


if blah == "nil" then 
 -- do something
end -- if


I think it is confusing (to you, as well as us) to use the word "nil" (which is a string). I would choose a different word, like "empty".

- Nick Gammon

www.gammon.com.au, www.mushclient.com
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.


34,864 views.

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.