Combobox and boolean variables, help

GPC2 script programming for Titan Two. Code examples, questions, requests.

Combobox and boolean variables, help

Postby pablosscripts » Wed Oct 19, 2016 5:27 am

I can't quite wrap my head around how this works.

I would like to have a combobox with two options - "Hold" and "Toggle", that map to a boolean variable.

How would I go about doing this? I started writing the code as pasted below, but it doesn't seem right.

Code: Select all
/* <cfgdesc>
[Sniper Mode]
shortdesc    = Test
byteoffset    = 18
bitsize        = 8
control        = combobox
default        = 0
item        = Hold
item        = Toggle
</cfgdesc> */

 
bool sniper_mode_toggle_switch = TRUE;
 
init {
    pmem_load();
    sniper_mode_toggle_switch = pmem_read(18);
}


How would the system know to map one value to True and another one to False? Is the memory location returned? Or do I need some logic to map "Hold" to True? I'm not sure how this is supposed to work. Help!
Setup: XIM Apex, T2, K780 keyboard, G Pro Wireless mouse, SteelSeries 4HD pad, DXRacer armrest, LucidSound LS30 headset, Netduma router, Ubiquiti UniFi AP LR

My R6 script: https://youtu.be/x-9NtxyySVM
User avatar
pablosscripts
Brigadier General
Brigadier General
 
Posts: 1976
Joined: Tue Nov 24, 2015 6:27 am

Re: Combobox and boolean variables, help

Postby Scachi » Wed Oct 19, 2016 8:18 am

1 is mapped to TRUE automatically , 0 is mapped to FALSE automatically
If you want to have Hold to be 1(TRUE) you have to make that the second item. The first item is 0(FALSE).
You can use some logic to map that around, too (if/else/switch).
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: Combobox and boolean variables, help

Postby pablosscripts » Wed Oct 19, 2016 8:43 am

Thanks for that! Yeah I suspected this is how it worked so thanks for confirming.
Setup: XIM Apex, T2, K780 keyboard, G Pro Wireless mouse, SteelSeries 4HD pad, DXRacer armrest, LucidSound LS30 headset, Netduma router, Ubiquiti UniFi AP LR

My R6 script: https://youtu.be/x-9NtxyySVM
User avatar
pablosscripts
Brigadier General
Brigadier General
 
Posts: 1976
Joined: Tue Nov 24, 2015 6:27 am

Re: Combobox and boolean variables, help

Postby J2Kbr » Thu Oct 20, 2016 5:39 pm

Just a little comment about FALSE and TRUE and why the GPC only defines FALSE.

As in C, the GPC language doesn't have a concept of a Boolean variable (a type class that can be either true or false with bit size 1); bool is an alias to uint8. As FALSE is a very well defined as 0, TRUE is everything different from 0. Defining TRUE as !FALSE makes TRUE == 1, which works most of time but can lead to an unintended behavior in some circumstances.

The best practice is always use FALSE in conditionals, even when testing variables for true, example:

Code: Select all
if(x != FALSE) { // Check for true
    //
    // Do something
    //
}


For assignments, define TRUE as !FALSE is fine.
ConsoleTuner Support Team
User avatar
J2Kbr
General of the Army
General of the Army
 
Posts: 20323
Joined: Tue Mar 18, 2014 1:39 pm

Re: Combobox and boolean variables, help

Postby The_Rabid_Taco » Thu Oct 20, 2016 5:41 pm

Thanks for the explanation. It does explain some of the bugs I've been tracking in a script I'm currently working on. I had an idea that this may be the case, but the confirmation makes it much easier to track down.
User avatar
The_Rabid_Taco
Major
Major
 
Posts: 1066
Joined: Wed Mar 16, 2016 6:04 pm
Location: Pensacola, FL

Re: Combobox and boolean variables, help

Postby pablosscripts » Fri Oct 21, 2016 8:31 am

I've always been curious about what on earth that "#define TRUE !FALSE" was all about. Good to know!

Since a majority of scripts probably use boolean variables, is there not a way to build this into GPC in some way so that it doesn't have to be explicitly declared? I probably have no idea what I'm talking about, just thinking of how to make things as simple and as fool proof as possible:)
Setup: XIM Apex, T2, K780 keyboard, G Pro Wireless mouse, SteelSeries 4HD pad, DXRacer armrest, LucidSound LS30 headset, Netduma router, Ubiquiti UniFi AP LR

My R6 script: https://youtu.be/x-9NtxyySVM
User avatar
pablosscripts
Brigadier General
Brigadier General
 
Posts: 1976
Joined: Tue Nov 24, 2015 6:27 am

Re: Combobox and boolean variables, help

Postby J2Kbr » Sat Oct 22, 2016 1:04 pm

lol ... yes, I am not sure what you are trying to propose. But, a real Boolean type, using just 1 storage bit, is something that requires lots extra computational steps to acomplish, as the minimum addressable storage cell is an byte, or 8 bits.
ConsoleTuner Support Team
User avatar
J2Kbr
General of the Army
General of the Army
 
Posts: 20323
Joined: Tue Mar 18, 2014 1:39 pm


Return to GPC2 Script Programming

Who is online

Users browsing this forum: No registered users and 168 guests