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

GML String functions that start at 0 instead of 1

Discussion in 'Tutorials' started by andev, Sep 1, 2017.

  1. andev

    andev Member

    Jul 2, 2017
    GM Version: Studio
    Target Platform: All
    Download: Download
    Links: <N/A>

    It bothers me that the string functions start at id 1 and not 0, because it means you have to add offsets sometimes. So I wrote some pass-through scripts that fix it. Further reading.

    All scripts are ready to drag straight into your project at once quickly for maximum convenience. They look like this:
    ///str_char_at(str, index)
    return string_char_at(argument0,argument1+1);
    So now to get the first character in a string, you look up ID 0 (not ID 1).

    Another example is:
    ///str_pos(substr, str)
    return string_pos(argument0,argument1)-1;
    Which will return -1 when it can't find the string (and not 0). Same logic as above applies too.


    If I've missed something or something doesn't work, let me know, I'll fix it asap. Thanks
    Last edited by a moderator: Sep 2, 2017
  2. chance

    chance predictably random Forum Staff Moderator

    Apr 22, 2016
    Nothing wrong with your approach. But adding scripts to simply increment the position index doesn't seem worth the effort -- to me, at least. And barely worthy of a tutorial submission.

    But I suppose the GML indexing convention could be really bothersome to some people. So perhaps they'll use your approach.
    andev likes this.
  3. andev

    andev Member

    Jul 2, 2017
    Thank you for approving it even though it's barely worthy! I sympathise that it's very minimal and not a tutorial but there doesn't seem to be a "resources" subsection anymore which would probably suit this submission a little better. I know this doesn't save that much time, which is why I've put the scripts in a folder rather than putting them in a .gmz or pasting them as code here. But if you wanted to do this, downloading someone elses would be a lot more appealing than writing your own, if you just want to get on with your project.

    Currently I'm working on something that's very heavily reliant on the string functions, and large number of the bugs I've crushed so far have been to do with me getting this offset wrong, because there are so many other offsets being added as well. I feel like if it's useful to me I should share it, and if it's any consolation, if someone else had made this before me and I'd seen it, I'd have definitely clicked download.

    Thanks again!

Share This Page