• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Why do strings start at 1 in GML?

andev

Member
Something I've been wondering for a while now, why does game maker treat string position ids differently? Everything else starts counting from 0.

Lists
Grids
Arrays
Sprites
Objects
Scripts
Sounds
Buffers
Buffer seek
Backgrounds
Rooms
Instances (+100000)
And I'm sure there's more...

But strings? Nope, the first character is 1.

Also while I'm at it, why do slots outside a grid return 0? And not undefined like all the ds functions?

Oh oh oh! And audio_play_sound? Shouldn't it be audio_sound_play?

And of course, not forgetting direction being the wrong way around (most annoying when working with physics).
 

TrunX

Member
I think that is something that got changed between GM8 and GameMaker: Studio.
I can remember porting a game and wondering the same. Why suddenly start to count at 1?
 

rwkay

YoYo Games Staff
YYG Staff
Well in GML string indexing always starts at 1 because Delphi strings start counting at 1 and that is what Mark Overmars (the original author is used to) - we are now stuck with that

Russell
 
G

Guest User

Guest
i've always kinda assumed that in languages that start at 1 for strings the logic behind it is that the '0' position is implied to be the very beginning of the string, before the first character. thus, after reading the first character you get 1.

no idea if that's really what drives it, or if there's a technical reason though. since some languages count strings from 0 it might just be personal preference.
 

andev

Member
the '0' position is implied to be the very beginning of the string, before the first character.
The only time I can think of needing that would be for inserting, and in that situation you can just use the character's ID as an input point, similar to how ds_list_insert works.
 
G

Guest User

Guest
no idea, i'm not a programmer so that's just how i rationalize as it working so i don't get all mixed up switching between 0 and 1 as the starter. :p
 
M

Multimagyar

Guest
relic of the past or not I would not say that it's mandatory to being stuck with it because people got used to it. Tho it is a fair point that a lot of project probably using the feature and would generate unwanted results without throwing an error. But also worth noting that inconsistencies can generate unwanted confusion. let it be BGR or RGB or a single number difference between string and array handling within the same language.
 

andev

Member
I guess the only win win solution here is to write some alternative scripts that subtract 1 from the position before passing everything through to the built in functions?

EDIT: Done
 
Last edited:

dannyjenn

Member
no idea if that's really what drives it, or if there's a technical reason though. since some languages count strings from 0 it might just be personal preference.
I believe that it's just a matter of abstraction. Since we naturally count things starting from 1, some of the higher-level languages (such as Delphi) use 1 as the first index.
In some other highly-abstracted languages the index signifies not the letter itself but the boundary or division between two letters (and in some languages, such as Python, you can do some crazy stuff with negative string indices. 0 is the division between the last character and the first character, so letter #0 is the first letter (because it's the letter between division #s 0 and 1), while letter #-1 is the last letter (the letter between division #s -1 and 0)...).

On a lower level, however, array indices (and string indices) start from 0 because what really matters is the offset, not the count. If you were programming in a very low-level language, and you wanted to store "The quick brown fox" in the RAM at memory address $D000, you'd be like, "I'll put a 'T' at $D000, and I'll put an 'h' at $D001, and I'll put an 'e' at $D002, and so on." Thus, if you later wanted to get the third character for whatever reason, you'd need to get the value at address $D002 (which is $D000+2, not $D000+3). So it makes more sense to count from 0.
 
Last edited:
Top