0-indexed vs 1-indexed

This forum is currently in read-only mode.
0 favourites
From the Asset Store
Full game Construct 2 and Construct 3 to post on Google Play
  • when i first worked with arrays, it took me some time to realize they are the only objects that are accessed with an index starting at 0 for the first element, while all the other plugins goe to some length to have any first element indexed by 1.

    i was guessing that you (the devs) did this to keep in line with the idea of 'simplified' programming, since 0-indexed stuff is admittedly a reoccuring source of (human) errors.

    but since most of the medium-and-above-complexity events / conditions & expressions that are often used in working with lists of objects are centered around 0 as a starting point (e.g. the LoopIndex, the random() expression), all the 1-indexed plugins actually generate more complexity by demanding extra attention to treat them different.

    so i was wondering if you are planning to strictly implement everything as 1-indexed, or if that is a notion you would open to negotiations :)

  • I've changed Array to 1 based. Not sure why it wasn't changed before.

    Developers using Array will need to review their code; it won't crash, but it'll return nothing if you call 0, as it'll be out of bounds.

  • I think everything in Construct should be 1-based index. 0-based makes sense in low-level programming, but for Construct, it's just more intuitive to have everything 1-based.

    I think it's worth seriously considering making Random() return 1-N instead of 0-(N-1) for the same reasons. However I can imagine that breaking a lot of stuff. What do you think?

    Also, I thought LoopIndex was 1-based already - if you find anything like that which is 0-based, report it as a bug and I'll correct it back to 1-based.

  • I'm not sure Random should be 1 based. There's genuine reason to generate between 0 and 1, and as you say, it'll break alot of things.

  • How about an option to have random not use 0?

    Clearly there are uses for both, but personally I'd prefer to have it not start with zero.

    Then again, I wish I could use decimals for a lot of stuff, but I don't see that happening.

  • Why not just do Random(N) +1?

    so instead of 0,1,2, you would get 1,2,3.

    I like having 0 in the random number generator, but I could always do Random(N) -1 I guess...

  • it is more intuitive for random to return values between 1 and random's argument in brackets. ie. for random(2) the "2" argument tells that it is the maximum number returned by the random function. but as you say it'll break a lot of things, i personally prefer the old fashioned 1+random(2)

  • Wasn't there a plan to change the random() expression to also be able to use two values for a range? How about random(3) returns 1, 2, or 3 and random(0, 3) would return 0, 1, 2, or 3? You could have it both ways.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Yeah, after discussing it on the chat 2 parameters for a range would be much nicer.

  • Oops, you mean there wasn't a plan before? I'm just making up my own plans for Construct now

  • I think I suggested it to Ashley at some point, but it makes more sense to clear it up now than after beta .

  • Then I propose

    random(N) returns 1 to N

    and random(A, B) would return any number from A to B inclusive (so random(1, N) is the same as random(N)).

    This means any cases of random(N) that currently exist must be changed to random(0, N-1). This will break a lot of things, but (hopefully) Construct will only grow more in future, so the least damaging time to do this is definitely now.

  • I think you should just keep the random function the same. I have quite a few things that would be half-broken.

  • I think you should just keep the random function the same. I have quite a few things that would be half-broken.

    Your noble sacrifice is for the greater good. Waver not, and face your fate though it may be grim... for yours is the glorious death of a warrior.

  • > I think you should just keep the random function the same. I have quite a few things that would be half-broken.

    >

    Your noble sacrifice is for the greater good. Waver not, and face your fate though it may be grim... for yours is the glorious death of a warrior.

    yeah, i agree. i`ll sacrifice my projects to... but besides it`s almost imposible to finish big game in construct in its current state, so it`s better to prepare matterials and ideas while waiting for 1.0.

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