Automatically convert to int()

Discussion and feedback on Construct 2

Post » Wed Sep 18, 2013 2:46 am

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."
B
9
S
3
G
1
Posts: 191
Reputation: 1,725

Post » Wed Sep 18, 2013 8:02 am

And how is it to know beforehand if the user will input a number rather than text?
Image ImageImage
B
168
S
50
G
163
Posts: 8,221
Reputation: 105,061

Post » Wed Sep 18, 2013 8:31 am

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.

GregCJK2013-09-18 08:32:28
B
18
S
3
G
3
Posts: 83
Reputation: 3,736

Post » Wed Sep 18, 2013 8:36 am

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.
B
9
S
3
G
1
Posts: 191
Reputation: 1,725

Post » Wed Sep 18, 2013 10:05 am

[QUOTE=ryackov]When I convert the input from a text box to a variable, ...[/QUOTE] 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?
[QUOTE=ryackov]I think if it's a "number" variable, any inputs to it should automatically be converted to int().[/QUOTE] 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.
Image
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,279

Post » Wed Sep 18, 2013 7:15 pm

Considering that I have to label global variables as "numbers" or "text," I don't see your point.
B
9
S
3
G
1
Posts: 191
Reputation: 1,725

Post » Wed Sep 18, 2013 7:29 pm

[QUOTE=ryackov]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.[/QUOTE] 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.
Image
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,279

Post » Thu Sep 19, 2013 8:26 am

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.
B
9
S
3
G
1
Posts: 191
Reputation: 1,725

Post » Thu Sep 19, 2013 9:52 am

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'm not a insane, my mother had me tested

I'm an engineer. To save time, just assume I'm never wrong
B
30
S
12
G
4
Posts: 141
Reputation: 4,782

Post » Thu Sep 19, 2013 10:11 am

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.
B
17
S
4
G
1
Posts: 332
Reputation: 3,099

Next

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 4 guests