Array out of bounds should not return 0

0 favourites
  • 11 posts
From the Asset Store
2D fighting template based in the game that defined the fighting games genre.
  • 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?

  • Why is -1 better than 0? Why is 0 error prone?

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

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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Yes, that's true. Custom out of boundaries value is a good solution.

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

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

  • Hi Ashley,

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

    Why is -1 better than 0? Why is 0 error prone?

    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?

    Greg

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

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

  • You can already do custom out-of-bounds values by accessing the array using a function from the Function object, and implement your own bounds checking in events.

    I really wouldn't want to change the existing value, mainly because it would break lots of existing projects, but also because any value could be a useful value in someone's project. -1 isn't somehow less of a valid number than 0.

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