0-indexed vs 1-indexed

New releases and general discussions.

Post » Sat Feb 21, 2009 1:20 am

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 :)
B
2
S
2
G
4
Posts: 171
Reputation: 1,792

Post » Sat Feb 21, 2009 2:35 am

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.
B
3
S
2
G
5
Posts: 1,777
Reputation: 5,529

Post » Sat Feb 21, 2009 12:56 pm

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.
Scirra Founder
B
359
S
214
G
72
Posts: 22,951
Reputation: 178,578

Post » Sat Feb 21, 2009 12:59 pm

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.
B
3
S
2
G
5
Posts: 1,777
Reputation: 5,529

Post » Sat Feb 21, 2009 1:07 pm

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.
Image Image
B
161
S
48
G
90
Posts: 7,354
Reputation: 66,763

Post » Sat Feb 21, 2009 2:00 pm

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... :)
B
2
S
1
G
4
Posts: 156
Reputation: 1,612

Post » Sat Feb 21, 2009 2:02 pm

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) ;)
B
81
S
50
G
10
Posts: 555
Reputation: 13,009

Post » Sat Feb 21, 2009 4:38 pm

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.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Sat Feb 21, 2009 4:44 pm

Yeah, after discussing it on the chat 2 parameters for a range would be much nicer.
B
3
S
2
G
5
Posts: 1,777
Reputation: 5,529

Post » Sat Feb 21, 2009 4:50 pm

Oops, you mean there wasn't a plan before? I'm just making up my own plans for Construct now :P
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Next

Return to Construct Classic Discussion

Who is online

Users browsing this forum: No registered users and 4 guests