String Editing

SinequanonSinequanon Member Posts: 35
edited November -1 in Working with GS (Mac)
I understand when you're using a rule on a string, you can use a few different comparative measures that are unique to Text: Contains, Begins With, Ends With and Is

Say I have a string: ABCD

And I want to look at the string and see if it begins with A. No problem, this one does.

Say I want to look at the substring BCD (or alternatively, I want to create a substring based on the string ABCD), and see if it begins with B. Is that possible?

Comments

  • SinequanonSinequanon Member Posts: 35
    No one knows?
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    Hi Sinequanon, I guess you could try something like:

    Rule set to All
    When
    game.string01 contains BCD
    game.string01 Ends with D

    That's the closest you can get, I think.

    ----------------------------------------------
    http://www.davidgriffinapps.co.uk/

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

  • SinequanonSinequanon Member Posts: 35
    I think what I'd really like is essentially to have a general rule that says "If string begins with A, do blah"; "If string begins with B, do blah" and so on where I am able to truncate it after every run-through. It's for a music game so I know that I'm going A-G.
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    Gotcha, I think; OK, what about your string as Text, initially set to ABCD, lets call it MusicList. So

    Rule
    When attribute game.MusicList = ABCD
    ---do your stuff---
    Change attribute game.Musiclist to BCD

    possibly a boolean switch, set to false, (let's call it ShortenGo), so can have control over the next Rule.

    So when ready, change attribute game.ShortenGo to true

    When ShortenGo is true
    When game.MusicList = BCD
    -- do your stuff---
    Change attribute game.Musiclist to CD
    Change attribute game.ShortenGo to false.

    ---etcetera----

    Would that work out for what you're after?

    ----------------------------------------------
    http://www.davidgriffinapps.co.uk/

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

  • xarmianxarmian Member Posts: 124
    It would be nice to have some decent string manipulation in GameSalad.. you may need to wait until we have arrays (or tables) to do what you're doing but an alternative would be to use numbers. You may run into limitations though.. But if you equate each letter to a number (A = 1, B = 2, C = 3,...) then arrange it backwards, you can grab the "first" number and truncate easily, so if you want to have the string ADDBGG, it would be:

    ADDBGG ---> GGBDDA
    144277 ---> 772441

    Then you can easily check the "first" letter and truncate:

    mod(772441,10) = 1 = A
    floor(772441/10) = 77244
    mod(77244,10) = 4 = D
    floor(77244/10) = 7724

    Does that make sense?
  • SinequanonSinequanon Member Posts: 35
    That's brilliant, xarmian, thanks. That would definitely work. Thank you, too, gyroscope - I need a bit more dynamic solution but I appreciate the help.

    Also to help anyone out who might be coming along after this, if a string needs to be built similarly to this, one could pretty easily start the number at 0 and then add the next note to the sequence using a couple of variables:

    Say A= 1, B = 2 and so on, and game.modifier = 0 and game.songString = 0 (songString is our song):

    the sequence to be recorded will be ABCD, which we want as a result 4321, you'd get the general form:

    game.songString = game.songString + (game.letter * 10^(game.modifier)) and game.modifier = game.modifier + 1.

    So A would give game.songString = 0 + (1 * 10^0) = 0 + 1*1) = 1

    AB would give: game.songString = 1 + (2 * 10^1) = 1 + 20 = 21

    ABC would give: game.songString = 21 + (3 * 10^2) = 321

    and ABCD would give: game.songString = 321 + (4 * 10^3) = 4321.

    And so on. I like it very much. Thanks a lot, again.
  • xarmianxarmian Member Posts: 124
    You're welcome. Just glad I could open a door. Good luck with your app.
Sign In or Register to comment.