Script failure

Post Reply
Salty
Posts: 5
Joined: Sat Oct 09, 2010 6:40 am
Location: Australia

Script failure

Post by Salty »

Hi guys,

First of all, Mudlet is awesome. It's amazing having a good Mac client (I used to use Cantrip when I played muds a few years back). And it's finally got me learning some programming, hooray.

I'm playing Lusternia at the moment, and I thought I'd capture some info from the prompt and make it a bit prettier in the process. I've got my health/mana/ego set up, but I'm having trouble displaying my balances. I'm using the following code for that at the moment :
Code: [show] | [select all] lua
local equilibrium = (balance.equilibrium and "<255,255,255>e" or "<80,80,80>e")
local balance = (balance.balance and "<255,255,255>x" or "<80,80,80>x")
local left = (balance.left and "<255,255,255>l" or "<80,80,80>l")
local right = (balance.right and "<255,255,255>r" or "<80,80,80>r")
prompts = ("<0,255,0>[" .. equilibrium .. balance .. left .. right .. "<0,255,0>]<255,255,255> - <0,255,0>[" .. "<0,255,0>]<255,255,255> - <255,0,0>[" .. "<255,0,0>]<255,255,255>")
This gets shoved into the full prompt string (probs not a good way to do it, I'll clean it up some other time). All that's then displayed is: - [exl] - [] - [] -
where the e and the x are in white, and the l in grey. (the brackets are in the right colours though)

I've checked the variables, which all show as true. My thinking was that the script should then display: - [exlr] - ; all in white. Can someone help?

Lucky24
Posts: 52
Joined: Sun Sep 12, 2010 1:50 am

Re: Script failure

Post by Lucky24 »

Check out http://www.lua.org/pil/3.3.html for how the AND/OR/NOT operators work.

BTW, your first line of code is always going to assign the first part to your local equilibrium variable, because balance.equilibrium is defined, and "<255,255,255>e" is a legitimate value. the AND statement just checks if whats' on the left and right is not undefined (= nil) or specifically marked as false (= false). AKA any value besides nil or false means the AND statement is true, so it returns the values. Your first line is double-ly going to be true because the OR statement evaluates to true if EITHER the statement on the left or right is not nil or false. Since the value to the left is"<255,255,255>e", it is true (because it's not nil or false). Same thing with "<80,80,80>e".

Instead of using AND/OR by themselves in a variable assignment, use IF/THEN/ELSE if you want to check whether a variable has a specific value, and then do something. The AND/OR logic is handy when you want to use it as a shortcut for assigning a default value in the case a variable is undefined (nil), but once it gets more complicated than that it's easier and more straight-forward to use IF/THEN/ELSE, especially if you're just beginning.

Check out the tutorial on IF/THEN/ELSE here:

http://www.lua.org/pil/4.3.1.html

Salty
Posts: 5
Joined: Sat Oct 09, 2010 6:40 am
Location: Australia

Re: Script failure

Post by Salty »

I have the balance.equilibrium (and others) flipping between true and false. So when I have no balance (balance.equilibrium = false), balance.equilibrium and "<255,255,255>e" should return false, shouldn't it? That would mean I'd end up with "<80,80,80>e". At least that was my thinking... I did have it in if/then/else form, but I thought this was a bit more concise.

Even if you're right, which I'm not doubting, then all four values should show the same display, right? But they're not, and I've already checked that all of the balances are true. That's the part that's confusing me.

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

Re: Script failure

Post by Vadi »

The use of and/or is proper there. "r" is a tag to reset the colour though at some point... what are you using to display this?

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Script failure

Post by Heiko »

You are overshadowing the global balance with your local balance definition.
This will do it:
Code: [show] | [select all] lua
balance = {}
balance.equilibrium = true
balance.balance = false
balance.left = false
balance.right = true
local _equilibrium = (balance.equilibrium and "<255,255,255>e" or "<80,80,80>e")
local _balance = (balance.balance and "<255,255,255>x" or "<80,80,80>x")
local _left = balance.left and "<255,255,255>l" or "<80,80,80>l"
local _right = balance.right and "<255,255,255>r" or "<80,80,80>r"
prompt = ("<0,255,0>[" .. _equilibrium .. _balance .. _left .. _right .. "<0,255,0>]<255,255,255> - <0,255,0>[" .. "<0,255,0>]<255,255,255> - <255,0,0>[" .. "<255,0,0>]<255,255,255>")
 
decho(prompt.."\n")

Salty
Posts: 5
Joined: Sat Oct 09, 2010 6:40 am
Location: Australia

Re: Script failure

Post by Salty »

Oh, that's what's wrong! Thanks so much guys! And yeah, I should have said that I was using decho to display earlier. oops.

Post Reply