Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (636)
Games in Android Showcase (178)
games submitted by our members
Games in WIP (686)
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 907 times)
0 Members and 1 Guest are viewing this topic.
Offline Zerolife

Senior Newbie

« Posted 2010-07-07 02: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 Devvie

Medals: 4

« Reply #1 - Posted 2010-07-07 07: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:
String newString = new String( oldString );
Offline Orangy Tang

JGO Kernel

Medals: 57
Projects: 11

Monkey for a head

« Reply #2 - Posted 2010-07-07 09: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:
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).

[ - 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 11: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

     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 11: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.

Dwinin (61 views)
2015-11-07 13:29:08

Rems19 (75 views)
2015-10-31 01:36:56

Rems19 (68 views)
2015-10-31 01:32:37

williamwoles (103 views)
2015-10-23 10:42:59

williamwoles (91 views)
2015-10-23 10:42:45

Jervac_ (103 views)
2015-10-18 23:29:12

DarkCart (131 views)
2015-10-16 00:58:11

KaiHH (113 views)
2015-10-11 14:10:14

KaiHH (153 views)
2015-10-11 13:26:18

BurntPizza (166 views)
2015-10-08 03:11:46
Rendering resources
by Roquen
2015-11-13 14:37:59

Rendering resources
by Roquen
2015-11-13 14:36:58

Math: Resources
by Roquen
2015-10-22 07:46:10

Networking Resources
by Roquen
2015-10-16 07:12:30

Rendering resources
by Roquen
2015-10-15 07:40:48

Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11 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‑
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!