Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  Setting a variable to another  (Read 686 times)
0 Members and 1 Guest are viewing this topic.
Offline Zerolife

Senior Newbie





« Posted 2010-07-07 04:48:37 »

I read somewhere that when you set a variable to another like say x = y, all that is done is the path of y is copied to x, so whenever y changes from thatpoint on, they always stay the same. Idk how true that is, but i seem to be getting an issue involving something like that in the game I am making. To check to see if a variable has changed, i just set a variable to the old value of it, or rather what the screen is drawing, and check it with the current. in the init, and in the fix, i just set the current variables to the old storeage ones, so the path is getting copied, so whenever i change the current one, they change with them. sorry if thats confusing, anyone have any idea of how to fix that?

EDIT: So I've verified myself that java copies thel ocation of the variable to the other, linking the too, atleast with a string. I need a fix for this though. I tried adding "" to the end, but then it says that the 2 strings are different, when they are exactly the same. I havnt any idea how to fix this, so any help would be great.
Offline zoto

Senior Member


Medals: 4



« Reply #1 - Posted 2010-07-07 09:52:26 »

This only happens with objects, not primitives. You will need to create a new object that has the values of the old object.
One way to do this for strings is:
1  
String newString = new String( oldString );
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #2 - Posted 2010-07-07 11:06:59 »

This only happens with objects, not primitives. You will need to create a new object that has the values of the old object.
One way to do this for strings is:
1  
String newString = new String( oldString );


Bad advice is often worse than no advice at all. Since strings are immutable, there's absolutely no point in your code, other than wasting cpu time and memory.

Zerolife: What you've described should be impossible with strings. Post actual code which illustrates the problem. I suspect you're confused between '==' (tests if two references are the same object) and '.equals()' (tests if two different objects represent the same value/state).

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Zerolife

Senior Newbie





« Reply #3 - Posted 2010-07-07 13:07:48 »

well my issue wasnt the == vs .equals, i had that correct, i even tried the string = new String thing, and that didnt work, so heres my code

I call this to set that variables to make them the same

wep2 = wep; wepid2 = wepid;
body2 = body; bodyid2 = bodyid;
bodyc2= bodyc;

i call this to check the variables to see if they are the same, which they never are

if(!wep2.equals(wep)||!wepid2.equals(wepid)||!body2.equals(body)||!bodyid2.equals(bodyid)||!bodyc2.equals(bodyc))
     return true;
return false;

that is the only place they issue shows up. Maybe I missed something small, idk.
Offline Zerolife

Senior Newbie





« Reply #4 - Posted 2010-07-07 13:19:12 »

Well I found my issue, and now I feel pretty stupid. I was checking the wrong variables for changes. The values I  check change, but not quite yet in my game. So I tried updating values that are incapable of being different in my game yet. I don't know how i managed to skip over that. I fixed it though, and its all working. Thanks for the help though. My issues never seem to be what I think they are -.-
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.

CogWheelz (13 views)
2014-07-30 21:08:39

Riven (21 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (29 views)
2014-07-23 21:07:15

Riven (30 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!