Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (746)
Games in Android Showcase (226)
games submitted by our members
Games in WIP (827)
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  
  [solved]Ran into an odd bug when I tried to generate random numbers for items  (Read 2481 times)
0 Members and 1 Guest are viewing this topic.
Offline jay4842
« Posted 2015-08-06 04:27:32 »

So I am creating a game that will allow the player to craft weapons with random stats. But I am running into a huge issue. I made it to where the numbers generated would print out to the console, but it sets the item stats to the same number as identical items. for example



all of these items have the exact same stats.

but in the console it said other wise.



I dont know where the problem is, I only set the item stats once, but they all are set to the same number. The numbers only generate a new random number every time the program is compleatly restarted. If you have any idea please help. Thanks!

edit:
This is where the item is set and modified then added to the players inventory



This is the getRand(int x) method



sorry forgot the code
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #1 - Posted 2015-08-06 04:31:16 »

Well this is entirely a code issue and you posted no code, so we're not really going to be able to help.
Offline CopyableCougar4
« Reply #2 - Posted 2015-08-06 04:46:13 »

It seems you are modifying/adding the same object each time when you do:

1  
knife = Templates.IronThorn

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jay4842
« Reply #3 - Posted 2015-08-06 04:54:46 »

It seems you are modifying/adding the same object each time when you do:

1  
knife = Templates.IronThorn


That was done to see if the same item could have different stats so that each item is unique in its own way. but it only seems to add one single item instead of two similar items maybe?
Offline Jesse

JGO Coder


Medals: 23



« Reply #4 - Posted 2015-08-06 04:57:46 »

That was done to see if the same item could have different stats so that each item is unique in its own way. but it only seems to add one single item instead of two similar items maybe?

Make sure you're clear on the difference between reference and value semantics and which applies here. You might check out this recent thread - it contains links to some articles that cover this.

In short, it looks like all your 'instances' are actually just references to the same object, which is probably why they all appear to have the same characteristics.
Offline jay4842
« Reply #5 - Posted 2015-08-06 05:07:23 »

That was actually it! thank you, I will remember that in the future!
Pages: [1]
  ignore  |  Print  
 
 

 
xxMrPHDxx (78 views)
2017-12-31 17:17:51

xxMrPHDxx (39 views)
2017-12-31 17:15:51

xxMrPHDxx (45 views)
2017-12-28 18:11:33

Ecumene (215 views)
2017-09-30 02:57:34

theagentd (313 views)
2017-09-26 18:23:31

cybrmynd (370 views)
2017-08-02 12:28:51

cybrmynd (401 views)
2017-08-02 12:19:43

cybrmynd (371 views)
2017-08-02 12:18:09

Sralse (336 views)
2017-07-25 17:13:48

Archive (1089 views)
2017-04-27 17:45:51
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!