Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Search inside a string?

Suppose the player acquires a few items in the course of the game I later want to remove or transform. E.g. silk pants, silk shoes and silk hat. I would normally record the fact that he has them with <<set $silkpants to 1>> or similar, but I could also write it down to a string - <<set $silkpants to "silkpants">>. I'm thinking this might be useful if I decide to remove all items beginning, or containing, "silk". I could do it with a separate variable change for each, but if there are 20 different silk items, and if I haven't finished giving them all to the player, might add some later, then it's a real drag. So, is it possible to reference string content?

Comments

  • temnix wrote:
    So, is it possible to reference string content?


    Yes. You can use the Javascript string function indexOf() to determine whether or not a substring exists. An example would be:

    <<if $myValue.indexOf("silk") > -1>>
    <<print "The value " + $myValue + " contains the word 'silk'">>
    <<endif>>
    The -1 is the result of the indexOf function if it cannot find the value in the string.
  • Thanks.
  • And is it possible to use indexOf() with "either"? If I want to search for multiple substrings.
Sign In or Register to comment.