Read my last blog post if you want to know how to get this far. For the rest who have followed the last post, you will see that I added more entries in the list box. This is easy to do and I'm sure you know how to do it. Now it's time to show you how to add the magic to make your selection do a certain action.
1. Open up RangeGUI.hpp and go to the class Rex_Ok: RscButton. We will add this line of code.
- action = "[] spawn {execVM 'listboxAction.sqf';};";
The code basically executes a script every time the OK button is pressed. For the Cancel button add this line.
- action = "closeDialog 0;";
The code in this class will close the Dialog all together.
2. Create a file listboxAction.sqf
This file will be used to perform the action we want when the OK button is pressed. There isn't to much we have to add to this file. What is important in this file is the action lbCurSel.
https://community.bistudio.com/wiki/lbCurSel
Go to the link to learn more but for this tutorial purposes I'll keep it simple. What lbCurSel returns is a number or rather the index which the list box cursor was when the OK button was pressed.
As you can see depending on the where the cursor was located when the OK button was pressed it will have an index which we can retrieve to use to perform a certain action. Now looking at lbCurSel 447. The number 447 isn't an arbitrary number, it actually refers to the list box defined in the class Rex_Range or RangeGUI.hpp. Refer to my previous blog posts to see where it is defined. If you're new to programming there is one thing I need to make you aware of. Even though there is a list of items 1 to 4. Computers start the count at 0. As you can see in the code I made hints to reflect this. This very important you remember this as it can be a headache when the logic is right in your head but no the computers.
3. Click index 2 and press OK
As you can see index 2 is the third item in the list.
Conclusion
So this blog post is showing you how to retrieve user selection from the list box to perform an action. You can do a lot more than showing hints. You can actually make a person teleport or spawn a vehicle. There is plenty of things you can do if you have the right tools in your tool box. When I first started to learn Arma Scripting. Everything I did was attached to a pole. Having over 4 options on a pole is very hard to select the right choice. A list box is better for the user experience and easier to code. Using addAction command is a hassle. Well I hope my instructions were clear. Let me know if you get stuck.