Quick simple syntax stacking issue?

Posted by Simimi on Sat 01 Sep 2007 12:47 PM — 15 posts, 49,913 views.

#0
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.
USA #1
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.
#2
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?
#3
 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...
USA #4
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
#5
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.
USA #6
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
#7
so using.

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


Is a no?
#8
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.
USA #9
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.
#10
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...
#11
Well the double sip bug was "fixed" but having the sending of "drink health" to set bals.potion to "curing" pre-emtively, just incase!
#12
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
#13
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.
Australia Forum Administrator #14
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".