1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

string_count help

Discussion in 'Programming' started by SmashAdams, Mar 7, 2017.

  1. SmashAdams

    SmashAdams Guest

    i have a string:
    str = "FIST KNIFE SHOTGUN";

    I have a substring:
    playerText = whatever the player is typing into the input field.
    ex: playerText = "FIST";

    I want to be able to know if the player has typed either FIST, KNIFE OR SHOTGUN into the input field
    without having to do this:

    if(playerText == "SHOTGUN){
    //do this
    }else if(playerText == FIST"){
    //do this
    }else if //etc...

    so I figured I could just count the # of times a substring (playerText) appears in my weapons list (str).
    if string_count returns a value >0, then I know the player has typed one of the three weapons.

    so I tried that, but it would only return 0.
    then I tried simplifying my 'str' string to just str = "FIST";
    then when I typed FIST into the input field, string_count returned 1.
    why doesn't string_count like me using str = "FIST KNIFE SHOTGUN"?
    I know I must be misunderstanding how string_count really works, but i've reread the docs many times.
    any help would be appreciated.

    an alternative method I think would be to store the 3 weapons as string values in an array, and then loop through each element in that array and check it against string_count to see what it returns. but that could be slow if I have 50+ weapons...
     
  2. YellowAfterlife

    YellowAfterlife ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    2,445
    Be careful - string_count takes the string to count number of (in your case, "FIST") as the first argument, while the string to count in is the second argument.
    Therefore you'd have it like
    Code:
    if (string_count("FIST", playerText) > 0) {
    // ...
    }
     
  3. SmashAdams

    SmashAdams Guest

    I understand that, but

    if (string_count("FIST", playerText) > 0) {
    // ...
    }else if(string_count("SHOTGUN", playerText) > 0){
    //...
    }

    is no different from this more direct version:

    if(playerText == "SHOTGUN){
    //do this
    }else if(playerText == FIST"){
    //do this
    }else if //etc...

    and is what i'm trying to avoid having to do.

    I want to take my input, and compare it to a list of predefined weapon names, and if it's ANY of those weapon names, do something.

    like this:
    if(playerText = SHOTGUN or playerText = KNIFE or playerText = FIST or....){
    //do this...
    }
    but more elegant. i don't want to have to keep adding on "or playerText = weapon_Name" every time I introduce a weapon...

    }
     
  4. SmashAdams

    SmashAdams Guest

    this does what I want but like I said, performance could take a hit if I have a large # of weapon names to loop through.

    where...
    Code:
    weaponsList = ["FIST","KNIFE","HANDGUN","SHOTGUN"]
    and i'm looping through the array to count how many times my substring (playerText) appears in each array element...
    Code:
    for(var i=0;i<array_length_1d(weaponsList);i++){
                if((string_count(playerText,weaponsList[i]))==1){
                show_debug_message("PLAYER INPUT = ACCEPTED WEAPON");
                }
                }
    
     
  5. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,149
    That's how it's done, but I wouldn't check if the string_count equals 1, because what if the player typed fist twice? Then it'd fail.

    Can the player type just FIST and get just a FIST?
    Can the player type just SHOTGUN and get just a SHOTGUN?
    If yes, then you need to loop through an array of entries.

    Can the player type FIST SHOTGUN and get both a FIST and SHOTGUN?
    If yes, then you need to loop through an array.

    Can the player type SHOTGUN FIST and get both a FIST and SHOTGUN?
    If yes, then you need to loop through an array.

    You're trying to micromanage CPU performance on something that logically can't be micromanaged much further.

    You know how people do secret name codes in games in the distant past?
    Code:
    for(n=0; n<8; n++) {
    for(i=0; i<15; i++) {
    for(k=0; k<15; k++) {
    if cheat_names[i,k] != PLAYER_NAME[n] k = 15; }
    if k == 15 i = 15; }
    if i != 15 enable_cheats(i);
    }
    
    You know how they do it nowadays, at least in Game Maker?
    [code]for(i=0; i<15; i++) if cheat_names[i] == PLAYER_NAME {enable_cheats(i);}
    They still have to use a loop and call the same code over and over in its optimized form.


    Performance shouldn't take a noticeable hit at all if you only call that code ONCE -- when the player submits his commands.
     
  6. SmashAdams

    SmashAdams Guest

    Thanks for the reply! and yeah I realized using string_count was uneccessary when I could just do:


    Code:
    weaponsList = ["FIST","KNIFE","HANDGUN","SHOTGUN"]
    
    for(var i=0;i<array_length_1d(weaponsList);i++){
        if(playerText == weaponsList[i]){
            //do something...
            break; //exit for-loop
            }  
    
    that solves the issue of the player typing in "FISTT" or "FIST FIST" or whatever.
    I know It DOESN'T account for things like "FIST SHOTGUN" (unless an element in my array was "FIST SHOTGUN), but I only want the playing typing in one weapon at a time anyway. (this is for weapon equipping).
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice