PDA

View Full Version : Button States



Steve_M
February 28th, 2010, 07:49 AM
my lua is letting me down again!

I have a frustratingly simple question, I have a button with 4 states and two variables controlling it this is what I am trying to achieve

State 0 default
State 1 VariableA is true (state rule = 'variableA')
State 2 VariableB is true (state rule = 'variableB')
State 3 VariableA and VariableB is true (state rule = ?)

States 0, 1, and 2 work fine. I can't get 3 to work, what should the rule be?

Rob H
February 28th, 2010, 04:51 PM
You can't do that with simple state rules.

You have a couple of choices - I think the first of these will work (I'm fairly sure that I implemented the necessary change to allow it a while ago)

I assume that by true you mean non-zero?


State 1 - lua Rule - return (NetRemote.GetVariable('A') == '1') and (NetRemote.GetVariable('B' == '0'))

State 2 - lua Rule - return (NetRemote.GetVariable('A') == '0') and (NetRemote.GetVariable('B' == '1'))

State 3 - lua Rule - return (NetRemote.GetVariable('A') == '1') and (NetRemote.GetVariable('B' == '1'))


Alternatively, you can add a bit of Lua code to your .lua file that uses variable watches for variables A and B.

Something like


local function SetValue(var, value)
NetRemote.SetVariable('C', tonumber(NetRemote.GetVariable('A')) + tonumber(NetRemote.GetVariable('B'))
end

NetRemote.RegisterVariableWatch('A', SetValue)
NetRemote.RegisterVariableWatch('B', SetValue)

And have the states dependent upon variable C instead which will take the values 0, 1, 2 or 3 assuming that A and B can only be 0 or 1.

Steve_M
March 1st, 2010, 02:50 AM
Thanks Rob

I had attempted to try something like this in my .lua code with watched variables using an 'if' statement but with no success.

I'll give your recommendation a go tonight.

Steve_M
March 1st, 2010, 04:45 AM
Rob

Just had a look at your '.lua' solution, surely A + B would give me 0, 1, 1 or 2 ie.
A B

0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 2

should this work?...




local function SetValue(var, value)
NetRemote.SetVariable('C', (tonumber(NetRemote.GetVariable('A'))*2) + tonumber(NetRemote.GetVariable('B'))
end

NetRemote.RegisterVariableWatch('A', SetValue)
NetRemote.RegisterVariableWatch('B', SetValue)



i.e

(0*2) + 0 = 0
(0*2) + 1 = 1
(1*2) + 0 = 2
(1*2) + 1 = 3

Rob H
March 1st, 2010, 05:55 AM
Oops, you're right, sorry about that.

But your solution looks fine to me.