0 Favourites

Automatically convert to int()

  • In one of the games I'm making, I noticed a minor nuance that isn't implemented. When I convert the input from a text box to a variable, even though it's a "number" variable, if I inputted text, or had no input, it would come out as NaN.

    I think if it's a "number" variable, any inputs to it should automatically be converted to int(). I think the same is true for "text."

  • And how is it to know beforehand if the user will input a number rather than text?

  • Hi,

    I have been confused by this on several occasions, where I could not understand the source of surprising misbihavior of my apps.

    It's not just textboxes - I use heavily CSV files as input, and keep bumping into this ;-)

    If a variable is a number, I would say it should automatically attempt to cast an assigned value onto a numeric value.

    Greg

  • This might be a bit off-topic, but can you post an example of your work? I'm trying to learn as much about Construct 2 as possible.

  • When I convert the input from a text box to a variable, ...Assigning a value to a variable is not a conversion. Imagine it more like setting a new pointer to a spot in a table. Converting (known as type casting) will introduce a lot of issues, when trying to automate it.

    The input from a text box is -by definition- a string. How should C2 decide what it has to treat as a number?

    "+1" could be a number...or is it an approval? Or is it abbreviated for "me too"?

    "1a" What to do now? Taking "1" as a number and just omitting the rest of the text? Should C2 be allowed to just throw data out of the window automatically? Or isn't it a number but paragraph 1a?

    I think if it's a "number" variable, any inputs to it should automatically be converted to int().This would make it even more worse.

    "1.35" A number, right? But type casting to int would make it become 1 and again C2 would just throw data out of the window automatically.

    Even harder:

    "1,35" Not one but two numbers? Well, could be, but for example in germany this is the way to write the above number 1.35

    The only one who knows for sure, what type of value is needed, is the programmer. So it's good that it's up to you to do the correct conversions. <img src="smileys/smiley2.gif" border="0" align="middle" />

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Considering that I have to label global variables as "numbers" or "text," I don't see your point.

  • When I convert the input from a text box to a variable, even though it's a "number" variable, if I inputted text, or had no input, it would come out as NaN.That was my point, that I explained in detail. Your thoughts are wrong at this point. The input from a text box will always be text. Entering "1" is text, not a number. If you assign "1" to a number variable, C2 reacts absolutely correct with NaN (not a number). Why auto-type-casting isn't feasible is explained in my last post.

  • That's just false. Assigning 1 to a number variable or a text variable, and using that in an equation, will yield a value.

    Assigning "" to a variable will yield NaN.

    Which is what I meant in my posts.

  • I fully agree with tulamide.

    Sorry, ryackov, in many years of programming in different languages I've seen far more troubles caused by "auto casting" than the ones made by "manual casting": a programmer should always be conscious of what he/she is doing, and leaving such operations to the "intellect" of the language is not a good thing, in my opinion...

  • I think ryackov means he makes a global variable first. During this he chooses it to be a text or a number or a bool. He chooses number. So C2 knows it's a number.

    Then he sets the textbox's text to that global variable. Thus he feels c2 should remember the value was a number in first place, and when he retrieves it from text object, it should automatically be type-casted.

    But I do not think this is how textbox works. Due the explanations by the above, anythink you put into a textbox will have to be converted into text. Thats the reason of textbox. It enables you to do various things with text, and to be able to do so, the number is turned into text. What if you store 10 to textbox, then set text to self.text & " points"? now the value inside is "10 points" and when you retrieve it there is no way for c2 to decide what to keep and what to discard.

  • Actually, ryackov is right. There's an inconsistency, there's partly auto-casting, maybe by design Ashley?

    I never stumbled upon it, because I always use variables of suitable types. With this small 1-event-project you can test it:

    <img src="https://dl.dropboxusercontent.com/u/11182740/C2/images/event.png" border="0" />

    Preview it and enter the letter a. It will say NaN as expected. Enter the letter 1 and it will return the number 1. With all issues that I mentioned earlier in my posts. (1.34 gets 1; 1,35 gets 1; 1.a gets 1 etc.)

    I didn't see this behavior before and would like to know if it's by design, and if so, why? (I think, it doesn't help much and introduces other issues)

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