1

I used to code in Pawn years ago but now I am working with other languages. This is a piece of code that allowed me to create 3d arrays with enum.

enum healthHack
{
    Float:acHealth,
    bool:hImmune,
    bool:aImmune,
    Float:acArmour,
    hcTimer,
    pTick,
    bool:afkImmune,
    bool:hasSpawned
};

new hcInfo[MAX_PLAYERS][healthHack];

Assuming the player_id is 5, MAX_PLAYERS is 500 and while accessing it, I would be able to do it like,

hcInfo[player_id][hasSpawned] = false;

hcInfo[player_id][acHealth] = 100;

I was wondering if Java has a similar approach to 3d arrays like this?

marstran
  • 26,413
  • 5
  • 61
  • 67
  • You can look at [jagged arrays](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/jagged-arrays) which are the best for most scenarios, or [multidimensional arrays](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/multidimensional-arrays) which, for the sake of efficiency, should only be used if you know for a fact you will fill all dimensions to the max – Narish Sep 19 '22 at 14:56
  • @Narish Those are C# links, not Java. – marstran Sep 19 '22 at 14:56
  • @marstran this post was originally tagged with C#, hence why I thought these links would be relevant to their answer. Java also has jagged arrays which are used in much the same way, though I don't think they have the equivalent of C#'s multidim arrays – Narish Sep 19 '22 at 14:59
  • 1
    @Narish I did take a look at them but it can only be of a single data type, right? The enum here allows u to have multiple data types in the array. I was wondering if there's something similar to that instead of having multiple jagged arrays with different data types? And yes, the reason I tagged C# is cuz I am working with both, C# and Java and looking to implement this code in both languages. – Sunehildeep Singh Sep 19 '22 at 14:59
  • You can create a class that has the same fields as your enum, and create your array so it can hold objects of that class, I guess? Don't think you can do the second bit indexing the array by property name, you'd probably need to extract the object from the array, change it, and then put it in the array again overwriting the old one. – JustAnotherDeveloper Sep 19 '22 at 15:03

2 Answers2

4

It's better practice in Java to create a class representing a Player and then updating it's values. You could use a Map<PlayerId, Player> to get the player with a specific playerId.

public class Player {

    private String playerId;
    private double acHealth;
    private boolean hImmune;
    private boolean aImmune;
    private double acArmour;
    private int hcTimer; // What type? I'm just guessing int.
    private int pTick;
    private boolean afkImmune;
    private boolean hasSpawned;

    // Constructor
    // Getters and setters
    // toString, hashCode, equals
}

Map<String, Player> playerMap = new HashMap<>();
playerMap.add("1", player1);

Player player = playerMap.get("1");
if (player != null) {
    player.setHasSpawned(false);
    player.setAcHealth(100.0);
}
marstran
  • 26,413
  • 5
  • 61
  • 67
1

C# implementation using classes and Dictionary to allow for lookups by the player id

public class Player 
{
    public int Id {get; set;} //you seem to use int here
    public double AcHealth {get; set;}
    public boolean HImmune {get; set;}
    public boolean AImmune {get; set;}
    public double AcArmour {get; set;}
    public Timer HcTimer {get; set;} //Maybe should look into System.Timers.Timer
    public int PTick {get; set;}
    public boolean AfkImmune {get; set;}
    public boolean HasSpawned {get; set;}
}

Dictionary<int, Player> players = new Dictionary<int, Player>
{
    [5] = new Player 
    {
        //init fields here
    },
    //... more players
};

Player? p1 = players[5];
if (p1 is not null)
{
    p1.HasSpawned = false;
    p1.AcHealth = 100.0;
}
Narish
  • 607
  • 4
  • 18