1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Why do strings start at 1 in GML?

Discussion in 'GameMaker Studio 2 Community Tech Support' started by andev, Sep 1, 2017.

  1. andev

    andev Member

    Joined:
    Jul 2, 2017
    Posts:
    444
    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).
     
  2. TrunX

    TrunX Member

    Joined:
    Jun 30, 2016
    Posts:
    146
    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?
     
    andev likes this.
  3. rwkay

    rwkay YoYo Games Staff YYG Staff

    Joined:
    Apr 12, 2016
    Posts:
    1,049
    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
     
    Cpaz, csanyk and andev like this.
  4. Guest User

    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 likes this.
  5. andev

    andev Member

    Joined:
    Jul 2, 2017
    Posts:
    444
    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.
     
  6. Guest User

    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
     
    andev likes this.
  7. Multimagyar

    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 and Edmanbosch like this.
  8. andev

    andev Member

    Joined:
    Jul 2, 2017
    Posts:
    444
    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: Sep 1, 2017
  9. dannyjenn

    dannyjenn Member

    Joined:
    Jul 29, 2017
    Posts:
    583
    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: Sep 2, 2017
    andev likes this.

Share This Page