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

0 favourites
  • 11 posts
From the Asset Store
Globals 2.0
$3.99 USD
Globals 2.0 stores and group variables. You can also load and save data (variables) from/to JSON files.
  • 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)

  • 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".

  • 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) ...

  • 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.

  • 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)

  • 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).

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

  • 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.

    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.

    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 ...

    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 ? ...

  • 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.

  • I believe it's on the TODO list.

    [attachment=0:8lffszft][/attachment:8lffszft]

  • I believe it's on the TODO list

    Wow! Thank you, thank you, thank you, ramones !!

    Someone finally just threw the egg at my face, instead of bypassing the original question !

    Now, I've searched just about every way I could come up with, without finding ANY those posts you linked ?!

    At the moment (re-searching just to check) it appears that maybe I just didn't sift (yet) far enough down in my search results !

    In many of these posts, Ashley mentions the "todo list" ...

    Is there an "official" list posted somewhere publicly ?

    (haven't managed to catch that on a search yet either!)

    I've seen the release page, and I assume there's a "master" list somewhere, such that certain items get prioritized for subsequent releases while others stay "lower" on "to-do" status ...

    Again, I officially apologize for not searching diligently enough, and wasting anyone else's time !

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)