How do I Create a Boolean Global (or Local) Variable?

Just started using Construct 2? Post your questions here

Post » Fri Jun 27, 2014 9:52 pm

My first (in THIS particular forum, anyway, but probably not my last!) "stupid question" ...
(if it IS really a "stupid" question, answered elsewhere, not appropriate for this forum, please advise)

As far as I've been able to determine, Global or Local Variables can ONLY be "Number" or "Text" but NOT "Boolean"; ONLY an "Instance Variable" may be "Boolean" ... Did I miss something ?

I'm a relatively "new" user to C2, but I've been programming more "conventional" paradigms almost as long as I can recall; I HAVE read a number of "tutorials" but have NOT read the ENTIRE manual; I HAVE searched repeatedly (scirra.com for Manual & Forums, also Google, etc.) for various combinations related to the above, to no avail ...

The only thing that I've seen in forums as well as tutorials, is the age-old "work-around" of using a Global or Local numeric variable and "testing" it for either 0 or 1 (or slightly "better," just "not zero") to get a "Boolean" result of "False" or "True," respectively. Why should this be necessary if the "environment" already HAS explicit Boolean variables available?

If I'm not missing something somewhere, and it's really NOT possible to create Global or Local Boolean variables, I'm curious as to why they would be limited in this way ? Was/Is there a specific reason for this?

Please and thank-you! (in advance)
B
7
Posts: 7
Reputation: 291

Post » Fri Jun 27, 2014 11:34 pm

You can use a text variable as a Boolean by either making it "No" or "Yes".
Or you can use the number variable "1" and switch it by multiplying by "-1".
B
6
S
1
Posts: 73
Reputation: 571

Post » Sat Jun 28, 2014 5:01 pm

rezagamertag wrote:You can use a text variable as a Boolean by either making it "No" or "Yes".
Or you can use the number variable "1" and switch it by multiplying by "-1".


Yep, the "text variable as a Boolean" workaround would seem to be an even worse choice than the "Integer as Boolean" one (I didn't want to say "kludge" but that's what it is) ... It might be more "readable" but theoretically, takes a bit longer to manipulate (although if you're looking at a "no programming" development tool, you're probably not concerned with this level of optimization) ...

The "*-1 toggle" trick is "clever" but "works" only if you "test" by comparing with ">0" rather than "≠0" ...

Still, my question is why are "real" Boolean types not allowed in "Global" or "Local" variables ?
This seems to be a rather obvious omission, and I'm just wondering if there's an explicit reason.

PS: Further reading shows me that C2 actually DOES have some "bit-wise" capabilities (get/set/togglebit functions) but still the single bit can't be implicitly interpreted as Boolean (must be explicitly compared) ...
B
7
Posts: 7
Reputation: 291

Post » Sun Jun 29, 2014 1:20 am

MikeDobson wrote:Still, my question is why are "real" Boolean types not allowed in "Global" or "Local" variables ?
This seems to be a rather obvious omission, and I'm just wondering if there's an explicit reason.


I personally don't see any benefit from having a Boolean variable compared to a variable that could be anything you'd want.
Conditions can be made like "if false" or "if true" where the variable has either been changed into "false" or "true".
I don't see Booleans adding any functionality that isn't already there.
B
6
S
1
Posts: 73
Reputation: 571

Post » Sun Jun 29, 2014 11:19 am

Booleans global and local aren t supported (I think they were forgoten, and since you can just replace it with 0 or 1, not a big deal, even instances booleans actually just take 0 or 1 values at runtime, you can verify).

To trigger the value of the variable bollean: set Bool to int(Bool%2) , this should work no problem (since you will want this variable to always be 0 or 1)
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
52
S
22
G
18
Posts: 2,122
Reputation: 17,093

Post » Mon Jun 30, 2014 9:00 pm

Thanks to @rezagamertag and @Aphrodite for your replies over the weekend!

I think I've at least inferred that the answer to my first question of "can it be done?" is probably "no" ...

As to my second question of "why not?", perhaps @Ashley or @Tom will reply with a "definitive" answer ! :)
Maybe I'll review more of the Forums here, and see if there's a better place for a "feature request" kind of thing (as opposed to the "Bugs" sub-forum).

From a "practical" POV, it is possible, perhaps even (not quite) "trivial" to "work around" the lack of Boolean type for Global/Local Variables, as mentioned already.

However, you always have more "work" to do to get "around": you have to always remember HOW exactly you've done it - is it "False=0, True=1" (or True≠0), "True>0, False≤0" or "True=Yes, False=No" or "yes, no" (and handle case if necessary); or maybe even build your own "pick-list" interface to enforce your workaround ... Having to "keep track" of it, is the real "issue" for such workarounds, especially when it's already implemented in the tool.

With the "real" Boolean, you simply pick "Is Boolean Variable Set" for an Event Condition, or in an Action, "Set Boolean" (thus ONLY allowing True or False is enforced) or (even more direct) "Toggle Boolean".

From the POV of a tool intended for "no programming" this is not only "correct," but somewhat annoying that it's available only in some cases. I can just imagine someone with no programming experience trying to figure out why they can use easy/convenient Boolean types in Instance Variables, but not in Global/Local Variables (much less keeping track of more complicated workarounds).
B
7
Posts: 7
Reputation: 291

Post » Tue Jul 01, 2014 12:12 am

Looking at the underlying technology of C2 (HTML5 / Javascript), I notice that all variables are ultimately without a type in their definition: http://www.w3schools.com/js/js_datatypes.asp

In fact, at the bottom of that page it says:

"
Do Not Declare String, Number, and Boolean as Objects!

When a JavaScript variable is declared with the keyword "new", the variable is created as an object:
var x = new String(); // Declares x as a String object
var y = new Number(); // Declares y as a Number object
var z = new Boolean(); // Declares z as a Boolean object

Avoid String, Number, and Boolean objects. They complicate your code and slow down execution speed.
"

With that said, I guess the Construct 2 editor tries to at least provide some structure by allowing variables to be specifically set to number or text. It seems possible to add the "type" of Boolean, but they probably also decided it was unnecessary due to the ability of Javascript to perform Boolean checks for most data types ( http://www.w3schools.com/js/js_booleans.asp )

So, they probably figured it's easier to just invert a condition that checks "if X = 0" than to bother with all the extra code to support a fake type, that would lower performance during execution if it were enforced via variables declared as objects.
"Construct 4 lets YOU make advanced games! (but not play them)" Construct Classic - Examples Kit
B
113
S
39
G
17
Posts: 2,184
Reputation: 19,217

Post » Tue Jul 01, 2014 3:37 am

Jayjay wrote:Looking at the underlying technology of C2 (HTML5 / Javascript), I notice that all variables are ultimately without a type in their definition


JavaScript uses dynamic typing most of the time, yes. That could lead to some interesting conversation, but that's not really my point. I didn't intend to get into any discussions of JS technology, language implementations, and/or script parsing/interpretation/compilation.

Jayjay wrote:With that said, I guess the Construct 2 editor tries to at least provide some structure by allowing variables to be specifically set to number or text.


This statement sort of leads towards my original point. This "structure" is for the "convenience" of the "no programming" game developer, not the eventual JS run-time code.

Jayjay wrote:It seems possible to add the "type" of Boolean, but they probably also decided it was unnecessary due to the ability of Javascript to perform Boolean checks for most data types


Not only "possible," but "they" already DID!

That in fact, IS my "original" point: C2 ALREADY supports a "Boolean" interface & dialogs for Instance Variables (only), so why not for Global & Local Variables as well ? THAT is my question ...

Jayjay wrote:So, they probably figured it's easier to just invert a condition that checks "if X = 0" than to bother with all the extra code to support a fake type, that would lower performance during execution if it were enforced via variables declared as objects.


Ok, I'm just guessing here, but it seems that the "enforcement" would be during the "design & development" using C2, not necessarily in the exported JS run-time code; if you "enforce" the "Boolean" within the design interface (C2 Boolean dialogs when "declared" as such Instance Variables), then you prevent the user from "accidentally" using it in any other manner, so as C2 generates the actual JS code on export, it should be able to guarantee that consistency. Exactly HOW the "Boolean" is "implemented" in the generated JS code ("if X=0" or whatever) should need be of NO concern to the game developer, as long as he does not (or is unable) to "mix" the types ...

(Now, I'm by no means any kind of "expert" in the use of C2, but I'm also guessing that there are other ways of bypassing those "interface" restrictions, but then, you're getting closer to "real code" as opposed to the "no programming" mantra of C2) ...

Once more, my only real issue here is:
When it fits my design, I'd like to use a "Boolean" interface & dialog(s) for Global & Local Variables, in the same manner as already supported for Instance Variables, so why NOT ? ...
B
7
Posts: 7
Reputation: 291

Post » Tue Jul 01, 2014 3:59 am

Well points 1 and 2 could matter, as there is no performance gain for Scirra to force a data type.

I'm not saying my reasoning is the definite reason for why they didn't copy and paste the feature either, I was just providing a possible explanation. I agree that it's strange they didn't do the same for global/local when private variables can be boolean.
"Construct 4 lets YOU make advanced games! (but not play them)" Construct Classic - Examples Kit
B
113
S
39
G
17
Posts: 2,184
Reputation: 19,217

Post » Tue Jul 01, 2014 12:04 pm

I believe it's on the TODO list.

booleans.png
You do not have the required permissions to view the files attached to this post.
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,630

Next

Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 2 guests