Filling array with same object reference

playerParty[0] = new PlayerParty(new Vector2D(screenCenter.x-100,screenCenter.y),
new PlayableEntity[]{
PlayableEntity.getCharacter(PlayableEntity.Characters.PLAYER,this),
PlayableEntity.getCharacter(PlayableEntity.Characters.PLAYER,this),
PlayableEntity.getCharacter(PlayableEntity.Characters.PLAYER,this),
PlayableEntity.getCharacter(PlayableEntity.Characters.PLAYER,this)
});
playerParty[0] = new PlayerParty(new Vector2D(screenCenter.x-100,screenCenter.y),
new PlayableEntity[]{
PlayableEntity.getCharacter(PlayableEntity.Characters.PLAYER,this),
PlayableEntity.getCharacter(PlayableEntity.Characters.PLAYER,this),
PlayableEntity.getCharacter(PlayableEntity.Characters.PLAYER,this),
PlayableEntity.getCharacter(PlayableEntity.Characters.PLAYER,this)
});
get characters returns this return new PlayableEntity(gamePanel, "player",new Stats(100,20)); i thought this would make the playable entities inside of the array different intances of the same object, but for some reason all the changes i make the one playable entities in the array are applied to all other playable entities
17 Replies
JavaBot
JavaBot7mo ago
This post has been reserved for your question.
Hey @asdru! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
asdru
asdruOP7mo ago
yeah i know, but i didn't use that because i will different characters
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
asdru
asdruOP7mo ago
oh, i should have mentioned in the title that that's the unwanted behaviour my bad rn if i add the players to the array and for example change the position of one, the position of all of them changes. i dont want that
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
asdru
asdruOP7mo ago
but how would apply this to my case? the array has 4 elements, which in my game means that the party can have a max of 4 characters. the characters inherit the playableEntity traits and override some functions i have a bit of an odd setup cause im fairly new at this, but all i need is to find a way to make the getCharacter function return a new instance each time. so if i understand this correctly all the PlayableEntity objects inside the array should have different addresses in memory
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
asdru
asdruOP7mo ago
i know oop, but its my first time making a big java project but i think the root of my issue is the fact that this
public static PlayableEntity getCharacter(Characters c,GamePanel gamePanel){
if(c==Characters.PLAYER) return new PlayableEntity(gamePanel, "player",new Stats(100,20));
else return null;
}
public static PlayableEntity getCharacter(Characters c,GamePanel gamePanel){
if(c==Characters.PLAYER) return new PlayableEntity(gamePanel, "player",new Stats(100,20));
else return null;
}
returns the same address each time, which i dont really understand why
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
asdru
asdruOP7mo ago
ah ok thanks
JavaBot
JavaBot7mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
asdru
asdruOP7mo ago
i'll see if that fixes the issue
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
asdru
asdruOP7mo ago
i still have the same issue ignore the fact that im repeating the same line of code 8 times to create the players https://github.com/asdru22/JavaGame/blob/main/src/main/java/main/Game.java the getCharacter function is behaving weird https://github.com/asdru22/JavaGame/blob/main/src/main/java/entity/PlayableEntity.java i have the two parties, both with 4 characters, however if i run the program i see only one character per party, which is odd considering i have a function that offsets the character position
No description
asdru
asdruOP7mo ago
i did some debugging and found that all the PlayableEntities in the arraylist are same object instance, otherwise i cant really explain why the position of all characters is the one of the last offset
public void initializeCharacters(){
initializeCharacter(party.get(0), new Vector2D(-20,-75));
initializeCharacter(party.get(1), new Vector2D(20,-25));
initializeCharacter(party.get(2), new Vector2D(-20,25));
initializeCharacter(party.get(3), new Vector2D(20,75));

System.out.println("current pos: "+party.get(0).pos);
public void initializeCharacters(){
initializeCharacter(party.get(0), new Vector2D(-20,-75));
initializeCharacter(party.get(1), new Vector2D(20,-25));
initializeCharacter(party.get(2), new Vector2D(-20,25));
initializeCharacter(party.get(3), new Vector2D(20,75));

System.out.println("current pos: "+party.get(0).pos);
oh god i found the issue... it had nothing to do with what i just said
JavaBot
JavaBot7mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server