LastExceed Expert Cheater Reputation: 1
Joined: 05 Nov 2014 Posts: 130
|
Posted: Sun Feb 14, 2016 7:34 am Post subject: How to create a simple on/off switch [Level: Newcomer] |
|
|
I came across this problem quite often when I started with lua, so here's a super simple example for newcomers.
In this example I will create a button that does something when you first click it, and something else when you click it again.
Method 1: using a common button
If you want to create a switch with a common button you have to add some code that checks if the button has been pressed before or not. To do this we simply use a variable that we change everytime we click the button:
Code: | x=0 --define a variable and set it to 0 (call it whatever you like, 'x' is just an example)
function CEButton1Click(sender) --this is the function that gets called when you press the button 'CEButton1'
if(x==0)then --check if the variable is equal to 0
--something --place your code here (whatever you want to happen if the button is pressed for the first time)
x=1 --set the variable to 1 so that next time the button is pressed something else happens
else --if the variable wasn't equal to 0 in the first place
--something else --place your code here (whatever you want to happen when the button is pressed for the second time)
x=0 --set the variable back to 0 so that next time the button is pressed your first code is executed again
end --end of 'if'
end --end of the button |
Method 2: using a checkbox/togglebox
For most cases where you need an on/off switch however it makes more sense to use a checkbox/togglebox instead of a common button (unless you want a 3-way switch, but will cover that case later).
Checkboxes and Toggleboxes work exactly the same way, the only differences are the name and the look.
Actually you could simply use the same code we used in method 1 for these too, but that could lead to problems and bugs if you toggle them with code instead of a mouseclick. Therefore we will apply some minor changes:
First of all we don't call our function when the box is clicked, but when it's state (checked or unchecked) changes.
Second we can remove the variable stuff because we can now directly check whether the box is checked or not:
Code: | function CECheckbox1Change(sender) --this is the function that gets called when the state of 'checkbox1' changes
if (UDF1.CECheckbox1.State==cbChecked)then --check if 'checkbox1' is checked
--something --place your code here (whatever you want to happen when check the box)
else --if the box is unchecked
--something else --place your code here (whatever you want to happen when you uncheck the box)
end --end of 'if'
end --end of the checkbox |
Now some advanced stuff:
The 3 way switch [Level: Intermediate]
Actually I should call it 'the x way switch' because if understand this then you can do it as many times as you want.
In this example we will create a button that executes code1 when clicked for the first time, code2 when clicked for the second time, code3 when clicked for the third time, and then code1 again when clicked for the 4th time and so on.
Code: | x=0 --define the variable
y=3 --define another variable that containts the amount of options you have -1 (for a 3 way switch this number would be 2)
function CEButton1Click(sender)
if(x==0)then --check if the variable is equal to 0
--code1
end
if(x==1)then --check if the variable is equal to 1
--code2
end
if(x==2)then --check if the variable is equal to 2
--code3
end
if(x==y)then --after executing the code, you need to decide whether you want to increase the variable or set it back to 0
x=0
else
x=x+1
end
end |
Can you spot the pattern? I know I could have done this with way less code, but this way you can add as many options as you want.
Any questions? Need a tutorial for something else? Don't bother to ask
|
|