View Single Post
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 10-16-2020 , 12:12   Re: Return arrays in ArrayLists by reference
Reply With Quote #9

Quote:
Originally Posted by Muhlex View Post
One more follow-up question about my example... I'm writing up a basic minigame that (hypothetically) may have multiple instances running simultaneously with different clients on a server belonging to different games. Now I could still use a static Player players[MAXCLIENTS + 1] array per game, however as it is (understandably) not allowed to set multidimensional fields on an enum struct, I could not add the players as a field for e. g. a Game struct.

Is there a workaround that doesn't defeat the purpose of the enum struct syntax, achieving something like this?
PHP Code:
enum struct Player {
  
int client;
  
int someOtherData;
}

enum struct Game {
  
Player players[MAXPLAYERS 1]; // not allowed
  
int winnerOrWhatever;

If the number of players is low like 4 you could just add "player1", "player2", "player3" manually, but that's ugly. You could define a maximum number of simultaneous games and reference another global array.

PHP Code:
#define MAX_GAMES 10

enum struct Player {
  
int client;
  
int someOtherData;
}
Player players[MAX_GAMES][MAXPLAYERS+1];

enum struct Game {
  
int index;
  
int winnerOrWhatever;
  
  
void DoStuff() {
    
this.winnerOrWhatever players[this.index][0].client;
  }
}
Game games[MAX_GAMES];

public 
void thing() {
  
// Find free game slot
  
int index = -1;
  for (
int iMAX_GAMESi++) {
    if (
games[i].index == -1) {
      
index i;
      break;
    }
  }

  
Game game;
  
game.index index;
  
games[index] = game;

I don't think there's a common way for things like this.

Quote:
Originally Posted by Muhlex View Post
Another thing... Is it possible to retrieve a method from an enum struct which is stored in an ArrayList and then execute it, without getting a full copy of the enum struct?
No, to access the fields you have to have a local copy.
__________________
Peace-Maker is offline