Filling array with same object reference
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 entities17 Replies
⌛
This post has been reserved for your question.
Hey @asdru! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
yeah i know, but i didn't use that because i will different characters
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
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•8mo ago
Message Not Public
Sign In & Join Server To View
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•8mo ago
Message Not Public
Sign In & Join Server To View
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
returns the same address each time, which i dont really understand why
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
ah ok thanks
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.
i'll see if that fixes the issue
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
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
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
oh god i found the issue...
it had nothing to do with what i just said
💤
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.