Solved Simplify a code to menu interaction

Question that is answered or resolved.

Toranks

Active member
I have a script to interact with various menus. Until now only player 1 could handle them, and I wanted to make it accessible to all 3 players. The only way I've gotten it to work is by tripling the code. Just for curiosity, and for learning. Can anyone think of a way to simplify this script or is it fine as it is? Thanks!

C:
void keyintall(void Ani, int Frame, void Key, int Hflag, int Limit)
{// Change current animation if proper key is pressed or released by any player
    void self = getlocalvar("self");
    void Health = getentityproperty(self,"health");
    int iDir = getentityproperty(self, "direction");
    void iRKey;
    void iRKey2;
    void iRKey3;

      if (Key=="U"){ //Up Required?
        iRKey = playerkeys(0, 0, "moveup"); // "Up"
      } else if (Key=="D"){ //Down Required?
        iRKey = playerkeys(0, 0, "movedown"); // "Down"
      } else if (Key=="F"){ //Forward Required?
         iRKey = playerkeys(0, 0, "moveright"); // "Right"
      } else if (Key=="B"){ //Forward Required?
         iRKey = playerkeys(0, 0, "moveleft"); // "Left"
      } else if (Key=="J"){ //Jump Required?
        iRKey = playerkeys(0, 0, "jump"); // "Jump"
      } else if (Key=="A"){ //Attack Required?
        iRKey = playerkeys(0, 0, "attack"); // "Attack"
      } else if (Key=="S"){ //Special Required?
        iRKey = playerkeys(0, 0, "special"); // "Special"
      } else if (Key=="A2"){ //Attack2 Required?
        iRKey = playerkeys(0, 0, "attack2"); // "Attack2"
      } else if (Key=="A3"){ //Attack3 Required?
        iRKey = playerkeys(0, 0, "attack3"); // "Attack3"
      } else if (Key=="A4"){ //Attack4 Required?
        iRKey = playerkeys(0, 0, "attack4"); // "Attack4"
      }

      if (Hflag==1){ //Not holding the button case?
        iRKey = !iRKey; //Take the opposite condition
    }

      if ((Health > Limit)&&iRKey){
        changeentityproperty(self, "animation", openborconstant(Ani), 2);
      changeentityproperty(self, "animpos", Frame);
//        updateframe(self, Frame);
      }
   
            if (Key=="U"){ //Up Required?
        iRKey2 = playerkeys(1, 0, "moveup"); // "Up"
      } else if (Key=="D"){ //Down Required?
        iRKey2 = playerkeys(1, 0, "movedown"); // "Down"
      } else if (Key=="F"){ //Forward Required?
         iRKey2 = playerkeys(1, 0, "moveright"); // "Right"
      } else if (Key=="B"){ //Forward Required?
         iRKey2 = playerkeys(1, 0, "moveleft"); // "Left"
      } else if (Key=="J"){ //Jump Required?
        iRKey2 = playerkeys(1, 0, "jump"); // "Jump"
      } else if (Key=="A"){ //Attack Required?
        iRKey2 = playerkeys(1, 0, "attack"); // "Attack"
      } else if (Key=="S"){ //Special Required?
        iRKey2 = playerkeys(1, 0, "special"); // "Special"
      } else if (Key=="A2"){ //Attack2 Required?
        iRKey2 = playerkeys(1, 0, "attack2"); // "Attack2"
      } else if (Key=="A3"){ //Attack3 Required?
        iRKey2 = playerkeys(1, 0, "attack3"); // "Attack3"
      } else if (Key=="A4"){ //Attack4 Required?
        iRKey2 = playerkeys(1, 0, "attack4"); // "Attack4"
      }

      if (Hflag==1){ //Not holding the button case?
        iRKey2 = !iRKey2; //Take the opposite condition
    }

      if ((Health > Limit)&&iRKey2){
        changeentityproperty(self, "animation", openborconstant(Ani), 2);
      changeentityproperty(self, "animpos", Frame);
//        updateframe(self, Frame);
      }
   
            if (Key=="U"){ //Up Required?
        iRKey3 = playerkeys(2, 0, "moveup"); // "Up"
      } else if (Key=="D"){ //Down Required?
        iRKey3 = playerkeys(2, 0, "movedown"); // "Down"
      } else if (Key=="F"){ //Forward Required?
         iRKey3 = playerkeys(2, 0, "moveright"); // "Right"
      } else if (Key=="B"){ //Forward Required?
         iRKey3 = playerkeys(2, 0, "moveleft"); // "Left"
      } else if (Key=="J"){ //Jump Required?
        iRKey3 = playerkeys(2, 0, "jump"); // "Jump"
      } else if (Key=="A"){ //Attack Required?
        iRKey3 = playerkeys(2, 0, "attack"); // "Attack"
      } else if (Key=="S"){ //Special Required?
        iRKey3 = playerkeys(2, 0, "special"); // "Special"
      } else if (Key=="A2"){ //Attack2 Required?
        iRKey3 = playerkeys(2, 0, "attack2"); // "Attack2"
      } else if (Key=="A3"){ //Attack3 Required?
        iRKey3 = playerkeys(2, 0, "attack3"); // "Attack3"
      } else if (Key=="A4"){ //Attack4 Required?
        iRKey3 = playerkeys(2, 0, "attack4"); // "Attack4"
      }

      if (Hflag==1){ //Not holding the button case?
        iRKey3 = !iRKey3; //Take the opposite condition
    }

      if ((Health > Limit)&&iRKey3){
        changeentityproperty(self, "animation", openborconstant(Ani), 2);
      changeentityproperty(self, "animpos", Frame);
//        updateframe(self, Frame);
      }
}
 
Last edited:
This should be in Help and Advice section.

Anyways, the reason your script only work for player 1 is the blue number:
iRKey = playerkeys(0, 0, "moveup"); // "Up"

That parameter defines from which player it takes input from.
To allow the function to work with any player, you'd need to modify them into this:
int iPIndex = getentityproperty(self,"playerindex"); //Get player index
void iRKey;

if (Key=="U"){ //Up Required?
iRKey = playerkeys(iPIndex, 0, "moveup"); // "Up"

BTW I only quote a portion of the script for simplicity. You have to modify all playerkeys function you've declared in the function nonetheless.
 
int iPIndex = getentityproperty(self,"playerindex"); //Get player index

This gets playerindex of the entity where the code is. The entity is a menu (down a example), nor a player, so this doesn't work, with this variable only player 1 can interact with them. I want ALL players can interact at any time.

Sin título.png
 
Last edited:
Wait a second, you want all 3 players have right to move one cursor? :rolleyes:

Hmmm... I think you'd need to describe how exactly menu works.
I'm going to describe how I imagine it works:
1. When menu is spawned, all players can't move but they can access the menu.
2. Menu has multiple options and it has a cursor to highlight the option.
3. There's only one cursor and it's not entity based.
4. When one player presses attack, highlighted option is selected.
5. No priority is given to any player, meaning all three players can move the cursor and do the selection.

Feel free to correct me if I'm wrong :)
 
Oh I see. So all players can move cursor if they want to :whistle:.

I just got an idea about this.
Since the script is identical for all players, you only need to alter one parameter of playerkeys function like this:
C:
void keyintall(void Ani, int Frame, void Key, int Hflag, int Index)
{// Change current animation if proper key is pressed or released by defined player
    void self = getlocalvar("self");
    void iRKey;

    if(Key=="U"){ //Up Required?
      iRKey = playerkeys(Index, 0, "moveup"); // "Up"
    } else if (Key=="D"){ //Down Required?
      iRKey = playerkeys(Index, 0, "movedown"); // "Down"
    }

    if(Hflag==1){ //Not holding the button case?
      iRKey = !iRKey; //Take the opposite condition
    }

    if(iRKey){
      changeentityproperty(self, "animation", openborconstant(Ani), 2);
      changeentityproperty(self, "animpos", Frame);
    }
}

I removed Limit parameter and health check cause I don't think you need to check health vs limit when controlling a cursor.
I also removed other accepted key inputs but you could add those yourself :)

With this, you'd need to declare this function 3 times, one for each player.
 
Oh I see. So all players can move cursor if they want to :whistle:.

I just got an idea about this.
Since the script is identical for all players, you only need to alter one parameter of playerkeys function like this:


I removed Limit parameter and health check cause I don't think you need to check health vs limit when controlling a cursor.
I also removed other accepted key inputs but you could add those yourself :)

With this, you'd need to declare this function 3 times, one for each player.
Check health is for another purposes, but that's fine for me, thanks!
 
Back
Top Bottom