Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Different stats on the same Array?  (Read 536 times)
0 Members and 1 Guest are viewing this topic.
Offline roseslayer

Junior Devvie


Medals: 2



« Posted 2014-01-03 15:55:41 »

Hi there JGO,

first of all: happy new year! I've an array[] with all the items saved in them. If I change 1 stat of the array all the items with the same id will be changed aswell. So is there a way on having different stats in an Array[]? Please only tell me how to, I want to programm it myself!

Code:
1  
2  
3  
   public static final Items[] tiles = new Items[400];
   
   public static final Items SWORD = new ToolsSword(10, 12); //for example these stats are AttackDamage and AttackSpeed


With this code all the items (with the same id) will have the same AttackDamage and ASpeed. So what is the best way to be able to change the attackDamage without changing them all?

if you use something like this:
1  
2  
3  
Void changeAttackDamage(int attackDamage){
this.attackDamage = attackDamage;
}

then it will change the damage of all the items (with the same id).

If you don't understood the question, just say it.
Thanks for helping already!
-RoseSlayer

Fundamentum W.I.P.
Offline Longor1996
« Reply #1 - Posted 2014-01-03 16:00:18 »

I think you will need to split the whole thing up into 'Item Definition'-classes (-> basically Singletons) and 'Item-Instance' classes.
Or just go and make 'attackDamage' static, but that will probably lead to some code-mess later on.

Have a nice day.

- Longor1996

Sorry for my bad English! That's because i am from Germany.
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #2 - Posted 2014-01-03 16:03:34 »

Are your fields for the attack damage etc. static? You'll want to make them instance variables, otherwise the stats will be shared between every instance of the item.

Why are all OpenGL tutorials written in Brainf**k?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline roseslayer

Junior Devvie


Medals: 2



« Reply #3 - Posted 2014-01-03 16:25:46 »

Are your fields for the attack damage etc. static? You'll want to make them instance variables, otherwise the stats will be shared between every instance of the item.

They aren't static, they are protected. All the variables are protected.

Fundamentum W.I.P.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2014-01-03 16:43:06 »

You need to realize that an array doesn't hold objects. It only holds references/pointers to objects.

You probably have multiple references in your array, pointing to the same object. Your observation is that changing one array element changes others, while in reality you are changing one object, which you encounter multiple times when iterating over the references in your array.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline CodeHead

JGO Knight


Medals: 41


From rags to riches...to rags.


« Reply #5 - Posted 2014-01-03 17:31:37 »

What Riven said. In this case, each time you add your SWORD item to the array, you're not creating a new instance, you're just pointing the array element at the single instance of SWORD that you've already created. You could approach this from a few different angles. For example you could just make a new ID for each sword type that you need:
1  
2  
public static final Items SWORD = new ToolsSword(10, 12);
public static final Items SWIFTSWORD = new ToolsSword(8, 24);


You could use the factory pattern to create instances of ToolsSword objects that have the desired characteristics, or similarly (though less desirably) you could just create new instances of ToolsSword and assign it directly to your array:
1  
2  
Items[x] = new ToolsSword(10,12);
Items[x+1] = new ToolsSword(20, 6);


There are other approaches as well. The choice of which one to implement will depend on how you plan to access/use the Items[] array within your game.

Arthur: Are all men from the future loud-mouthed braggarts?
Ash: Nope. Just me baby...Just me.
Offline roseslayer

Junior Devvie


Medals: 2



« Reply #6 - Posted 2014-01-04 19:30:08 »

How did minecraft solved this problem? If changing the name of a sword the don't add another pointer to the Items[]?

Fundamentum W.I.P.
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #7 - Posted 2014-01-04 19:52:27 »

How did minecraft solved this problem? If changing the name of a sword the don't add another pointer to the Items[]?

First off, don't call them pointers, call them objects. Two of the same instances cannot have different states. I think you're getting confused between pass by reference and pass by value.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public class Sword extends Item {

   private int damage;
   private int speed;

   public Sword(int damage, int speed) {
      this.damage = damage;
      this.speed = speed;
   }

   public void changeDamage(int damage) {
      this.damage = damage;
   }

}


You cannot do something like this:
1  
2  
3  
4  
Item[] items = new Item[2];
Sword sword = new Sword(10, 5);
items[0] = sword
items[1] = sword.changeDamage(20); // the damage for items[0] will change; they are the same instance. Java is pass by value, not pass by reference.


You have to do something like this:
1  
2  
3  
Item[] items = new Item[2];
items[0] = new Sword(10, 5)
items[1] = new Sword(12, 4);

Why are all OpenGL tutorials written in Brainf**k?
Offline roseslayer

Junior Devvie


Medals: 2



« Reply #8 - Posted 2014-01-05 13:28:45 »

So basically I need to make a new instance of them and increase the ID everytime I add another sword?

Fundamentum W.I.P.
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #9 - Posted 2014-01-05 14:54:36 »

Yes, that's exactly how you need to do it.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #10 - Posted 2014-01-05 15:53:58 »

why use IDs/arrays anyways, you don't need them.
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #11 - Posted 2014-01-05 16:00:59 »

Also true. You should keep your items contained in each entity's class. Don't just have a huge array of weapons that contains all of the player's gear.

Offline roseslayer

Junior Devvie


Medals: 2



« Reply #12 - Posted 2014-01-08 15:18:06 »

So just make for every item a new class? But for the normal items like for example a leaf, without any customizable id's should I also make a new class?

Fundamentum W.I.P.
Offline Sethir

Senior Newbie


Exp: 3 years



« Reply #13 - Posted 2014-01-08 17:04:28 »

You don't need to add a Class for every item in your game, just make categories.
A sword or a mace are weapons. Weapons are equipment. Armor is equipment too and both share the same stat modifiers (at least in my game).
A leaf on the other hand is maybe used for a recipe (like tea or bandages). So you could make a craftingItem class.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (57 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!