• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community

using global Variable in IF-THEN (1 Viewer)

Bobbie

Newcomer
Joined
Nov 17, 2021
Messages
5
Reaction score
0
Standard is a variable a global variable. But when I call a variable in an other "IF-THEN" Statement the variable is NIL.

I give the variable x "1"when the switch is pressed Off ( in the first IF-THEN).
In the second IF-THEN, I call the variable x when the switch is pressed On. --> The error comes with NIL..

Of course I can start before the first IF-THEN with x = 0.
But When x become 1 in the first IF-THEN, after the loop at the end x = 0 again. There is no error.. but x = always 0 now..

I want to see de value of x in the second IF-THEN when I press the ON button..

The program is just a sample for testing.

How to use a variable in the whole program and not only in the "If then"statement?.

example to determine the variable x

Code:
Error code in log: Error: EventSystem: in test-lua: [string "commandArray = {}..."]:12: attempt to concatenate a nil value (global 'x')

Thx
 

Bobbie

Newcomer
Joined
Nov 17, 2021
Messages
5
Reaction score
0
"EditMode On"

Lua:
commandArray = {}
    if (devicechanged['RemoteWK2'] == "Off") then
        print(' Switch is Off')
        x = 1
        print(' X = : ' .. x .. ' ');
    end

    if (devicechanged['RemoteWK2'] == "On") then
    print('Switch is On')
    print(' X = : ' .. x .. ' ');
    end
print(x)
return commandArray
 

dinsdale247

Moderator
Staff member
Community Patron
Creator of WinLua
Joined
Nov 17, 2020
Messages
93
Reaction score
34
Location
Victoria BC
Website
winlua.net
Hi Bobbie,

The code fails because your variable x is only ever created when the first if statement is true. So if `devicechanged['RemoteWK2'] == "Off"` then the variable x is created when you assign the value 1. If, however `devicechanged['RemoteWK2'] == "On"` then x is never created. The simplest way to fix the problem is to create x outside of the if statements:

Lua:
devicechanged = {}
devicechanged['RemoteWK2'] = 'On'

x = 0 -- Declaring x outside of the if statements will ensure that it exists. 
commandArray = {}
if (devicechanged['RemoteWK2'] == "Off") then
    print(' Switch is Off')
    x = 1
    print(' X = : ' .. x .. ' ');
end

if (devicechanged['RemoteWK2'] == "On") then
    print('Switch is On')
    print(' X = : ' .. x .. ' ');
end
print(x)
return commandArray
 

Bobbie

Newcomer
Joined
Nov 17, 2021
Messages
5
Reaction score
0
Hi Dinsdale,

Thanks for your reply.
I have tried this. But when I give x the value 1 by pressing RemoteWK2 = Off, the loop makes x = 0 again. It seems the loop is not between "commandArray and return commandArray.

What I want is that I give the variable (x) a value and the variable (x) remains this value, also outside the IF-Then statement. Later on in the program I can the value give an other value by pressing an other button or action. And the value (x) hold this value till the next action.
 

Sorn

Administrator
Staff member
Joined
Jan 6, 2020
Messages
33
Reaction score
14
Hi Dinsdale,

Thanks for your reply.
I have tried this. But when I give x the value 1 by pressing RemoteWK2 = Off, the loop makes x = 0 again. It seems the loop is not between "commandArray and return commandArray.

What I want is that I give the variable (x) a value and the variable (x) remains this value, also outside the IF-Then statement. Later on in the program I can the value give an other value by pressing an other button or action. And the value (x) hold this value till the next action.
There is no loop in the code you posted. You need to declare x = 0 outside of the loop.
 

Bobbie

Newcomer
Joined
Nov 17, 2021
Messages
5
Reaction score
0
I mean the loop of the luaprogram ..
After "return commandArray" the program starts again and make x=0, while I gave x=1 in the If-Next statement..
So how keep x=1 in the whole program??
 

225559

Newcomer
Joined
Nov 17, 2020
Messages
23
Reaction score
7
I mean the loop of the luaprogram ..
After "return commandArray" the program starts again and make x=0, while I gave x=1 in the If-Next statement..
So how keep x=1 in the whole program??
Can you post the full code please?
 

Bobbie

Newcomer
Joined
Nov 17, 2021
Messages
5
Reaction score
0
I made a test-code to understand LUA in the first post. After Dinsdale's post I changed my test-code in:

Code:
x = 0 -- Declaring x outside of the if statements will ensure that it exists.
commandArray = {}
if (devicechanged['RemoteWK2'] == "Off") then      -- make x=1
    print(' Switch is Off')
    x = 1
    print(' X = : ' .. x .. ' ');
end

if (devicechanged['RemoteWK2'] == "On") then      -- show x
    print('Switch is On')
    print(' X = : ' .. x .. ' ');
end
print(x)
return commandArray

No errors now, but after I press RemoteWK2 to Off, x=1 for a moment and then x=0 again.
 

225559

Newcomer
Joined
Nov 17, 2020
Messages
23
Reaction score
7
This is not the entire code, as
Code:
return commandArray
has to be inside a function.
 
Top