Array out of bounds should not return 0

Discussion and feedback on Construct 2

Post » Mon Aug 26, 2013 10:52 am

Hello there,

right now, if you check an array at the position, which is out of bounds, you'll get 0, which is totally inconvinient and error prone. E.g. array.width=7, you check at array.x=10 and it returns 0. It would be much better if in this case it will retur a -1 or NaN.

What do you think?xoros2013-08-26 13:06:46
B
45
S
11
G
4
Posts: 515
Reputation: 7,389

Post » Mon Aug 26, 2013 12:36 pm

Why is -1 better than 0? Why is 0 error prone?
Scirra Founder
B
387
S
230
G
87
Posts: 24,248
Reputation: 192,238

Post » Mon Aug 26, 2013 1:00 pm

Because 0 is a value, which could be present in the game in some way e.g. UID. Logically, if I check the value at the position which is out of bounds, the value doesn't exist. So may be it would be even beter to return NaN.

It's not a big deal to limit a loop to the bounds of the array, but anyways it would save time.
B
45
S
11
G
4
Posts: 515
Reputation: 7,389

Post » Mon Aug 26, 2013 1:13 pm

some people designed their games with te fact that outside boundaries, the value is 0, so it is not like it was easy to change.

Also,-1 is a value, so the problem is still here.

I think it is a good choice to have a default value outside boundaries in some cases, maybe if we can set this value to whatever we want (0 if not specified), it could be better.

But if you make your array bigger with NaN outside boundaries, not sure the value that the array will take.

Also NaN is a value... and an array can have text so It'll be complicated...Aphrodite2013-08-26 13:14:46
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 Aug 26, 2013 1:21 pm

Yes, that's true. Custom out of boundaries value is a good solution.
B
45
S
11
G
4
Posts: 515
Reputation: 7,389

Post » Mon Aug 26, 2013 7:05 pm

NaN has lots of weird side-effects. Like if you set an object's X, Y, width, height or angle to NaN, it disappears (because it fails the is-in-viewport check). This can be confusing and difficult to debug, so I'd rather not create another way to introduce NaN numbers in to the engine. I think 0 makes more sense than -1 as well, since 0 means "nothing" whereas -1 could arguably mean "something", and there ought to be nothing if you access a value outside the array.Ashley2013-08-26 19:05:20
Scirra Founder
B
387
S
230
G
87
Posts: 24,248
Reputation: 192,238

Post » Mon Aug 26, 2013 9:07 pm

Maybe something like the "Option Explicit" did in VB, with that statement in your startup, you were required to declare your variables. Without it you could make them as needed.

Also if you have the Action in your startup, it would only "stop" on an out of bounds in the debugger, ignoring them again on export.
B
226
S
60
G
33
Posts: 902
Reputation: 39,989

Post » Mon Aug 26, 2013 10:40 pm

Hi @Ashley,

I think it's not just "out of bounds" issue. Also after "clear" - the empty array contents are zeros, I think?

[QUOTE=Ashley] Why is -1 better than 0? Why is 0 error prone?[/QUOTE]
For me, because all indexing starts at 0: for arrays, string tokens, etc. Therefore, when I include "0" into the array, I don't know whether "0" means element @index 0, or "this slot is not yet assigned". I recently used arrays heavily for randomizing e.g. animation frames, and so I start my random lists at "1", and then I'm forced to deduct 1 later in code. Maybe I'm doing this wrong, but for me it's a bit more messy this way.

"-1" would be much better, imho :-)

Anyway thanks for investigating, maybe this will result in some changes in future versions?

GregCJK2013-08-26 22:42:31
B
18
S
3
G
3
Posts: 83
Reputation: 3,736

Post » Tue Aug 27, 2013 11:29 am

-1 would break so much though, I don't want to go rewrite all my games.

If it's out of bounds, I want it to add 0 to my equation, not subtract 1.
B
226
S
60
G
33
Posts: 902
Reputation: 39,989

Post » Tue Aug 27, 2013 11:43 am

But anyways, something has to be done with this, because if your game is heavily based on arrays calculations, it's inevitable, that you stumble upon this issues.

Custom out of boundaries value is the best solution.xoros2013-08-27 11:47:26
B
45
S
11
G
4
Posts: 515
Reputation: 7,389

Next

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 2 guests