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
JavaBot5mo 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 User5mo ago
Message Not Public
Sign In & Join Server To View
asdru
asdru5mo ago
yeah i know, but i didn't use that because i will different characters
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
asdru
asdru5mo 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 User5mo ago
Message Not Public
Sign In & Join Server To View
asdru
asdru5mo 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 User5mo ago
Message Not Public
Sign In & Join Server To View
asdru
asdru5mo 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 User5mo ago
Message Not Public
Sign In & Join Server To View
asdru
asdru5mo ago
ah ok thanks
JavaBot
JavaBot5mo 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
asdru5mo ago
i'll see if that fixes the issue
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
asdru
asdru5mo 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
Want results from more Discord servers?
Add your server