Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (798)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (865)
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  
  Best way to debug?  (Read 6450 times)
0 Members and 1 Guest are viewing this topic.
Offline Therobodavo

Junior Devvie


Medals: 2
Exp: 2 years



« Posted 2015-06-18 19:56:22 »

So, I have around 26 files for my game, and I just went through updating them so that it's more object oriented and not hardcoded.

I thought everything was good, i made sure there were no infinite loops, and when i start the game up everything loads in, but after a couple of seconds it freezes and acts as if it's stuck in an infinite loop.

I also noticed when i start it up, I can move my player around, but I can't shoot.

Pretty much, I just shoot by pressing the M key.

When I click M, it runs currentWeapon.attack()

the currentWeapon is made in an inherited class, the Creature constructor.

Pretty much, I needed to save the "shooter" when I create the weapon, so I pass "this" in the arguments for the weapon, and within the weapon I use that for the x,y,width,height, and direction of the creature. (Where to spawn the bullet the weapon shoots). This worked when I hardcoded everything, but I'm wondering could this issue of the game freezing be cause because of how I create the weapon/Pass the creature? I also wonder if I pass it through the constructor, would that mean the data is never updated. Cause i set "this" to a local variable in the weapon. I don't really know what else to do unless I need to pass the creature every time I want to attack/draw.

MY question though is, what's the best way to find the issue for something like this? I could see when you have a large game with 100+ files... it may get kinda tricky to fix an issue like this.
Offline CopyableCougar4
« Reply #1 - Posted 2015-06-18 20:46:18 »

Most large games require testing each new class or feature as it's added, which eliminates most problems like this.

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

Github: http://github.com/CopyableCougar4
Offline Riven
Administrator

« JGO Overlord »


Medals: 1369
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2015-06-18 20:49:44 »

Launch the game in your IDE in Debug mode.
Once you suspect your game enters some loop, freeze the thread you suspect is 'stuck', and look at the stacktrace.
Then step though the code until you figure out what is happening.

Or... litter your code with logging, until you figure out what's the culprit.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline philfrei
« Reply #3 - Posted 2015-06-18 20:50:16 »

Some people prefer using a step-debugger process. Others prefer to embed print messages to the console at key spots revealing, key data.

I've seen books were the preference was asked of some of the greatest programmers alive, and some prefer one, or the other, or a mix of both. There was no clear winner.

I lean towards printing console messages, and use the following basic form:
1  
System.out.println("classname.methodname, variable=" + variable);

A lot of people would think this is overkill, but I really hate having old messages appear and not know where the heck they are coming from. Hence: the inclusion of the class and method names.

You don't necessarily have to put these in every one of your 100+ files. For example, you can pick a single entity that is unable to shoot, focus on that. Chances are that what you learn will be applicable to parallel situations. Also you can scatter these through various stages of you process, to try to narrow the possibilities of where the hang is occurring.

In situations where you want to look at a statement that repeats a huge number of times, and you don't want to flood the console, you can add a counter and only print every 1000th iteration or whatever is a useful count.

[EDIT] Riven's advice on using the debug to identify a hanging thread is really good. I will use this more in the future.

music and music apps: http://adonax.com
Offline Therobodavo

Junior Devvie


Medals: 2
Exp: 2 years



« Reply #4 - Posted 2015-06-18 22:44:41 »

I may have just realized something.

If I pass something, let's say an ArrayList in like the constructor, and set it to a local variable, will that local variable always have the most up to date version of the ArrayList? I thought it would be a copy.. xD, or do objects work differently? I guess my player is working fine shooting, and it's all based on "this" which is from the Creature constructor, and it's set to a local variable in my gun class.
Offline SauronWatchesYou

JGO Ninja


Medals: 33
Projects: 4
Exp: 2 years


Hi there! :)


« Reply #5 - Posted 2015-06-18 22:49:09 »

If you pass in an object somewhere and change it, it will change it everywhere Smiley the power of objects!

Hey, you! Back to work
Offline Therobodavo

Junior Devvie


Medals: 2
Exp: 2 years



« Reply #6 - Posted 2015-06-18 23:06:01 »

but if i set it to a golbal variable in another objects class... it's still referenced when i access that global variable? Even if the original one is changed a bunch?
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #7 - Posted 2015-06-18 23:09:42 »

You know you can write a simple program to test things like that, right?

Quote
If I pass something, let's say an ArrayList in like the constructor, and set it to a local variable, will that local variable always have the most up to date version of the ArrayList? I thought it would be a copy..

Rule of thumb for how Java operates: no instance of a reference type is ever copied unless you do it explicitly somehow (e.g. clone() or a custom deep copy function).

This is plainly seen when you realize that the constructor parameter and local variable you mention are not the arraylist itself, but pointers to it. You're assigning the local variable pointer to point at some list, not copying the list.
Offline SauronWatchesYou

JGO Ninja


Medals: 33
Projects: 4
Exp: 2 years


Hi there! :)


« Reply #8 - Posted 2015-06-18 23:13:00 »

but if i set it to a golbal variable in another objects class... it's still referenced when i access that global variable? Even if the original one is changed a bunch?

If I understand, you're asking if you have a bunch of variables inside a class and change those variables somewhere else then is it still referencing the same object? Or are you asking if you pass in the object to a variable of the same object type, does it make a copy or does it still reference the same object?

Both cases give the same result. You are passing the reference of an object and if you pass that reference to another variable, it is still the same reference

EDIT: This is known as a shallow copy. If you want to copy an object with it's own variables then you need to create a deep copy

Hey, you! Back to work
Offline Therobodavo

Junior Devvie


Medals: 2
Exp: 2 years



« Reply #9 - Posted 2015-06-18 23:39:09 »

I'm silly.

MoveableTile copy;

Public Object(MoveableTile shooter)
{

     copy = shooter;

}

this makes a shallow copy cause it's not making a new shooter?

MoveableTile copy;

Public Object(MoveableTile shooter)
{

     copy = new shooter; (or something like that, idk)

}

It would create a new instance of that object I assume?

I've never really worked with pointers or anything, but even after testing I always thought it made a copy. I swear, things they teach you in school are very misleading at times.

Thanks for the info.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #10 - Posted 2015-06-18 23:44:31 »

Quote
this makes a shallow copy cause it's not making a new shooter?

No. There is no copy at all.
Offline SauronWatchesYou

JGO Ninja


Medals: 33
Projects: 4
Exp: 2 years


Hi there! :)


« Reply #11 - Posted 2015-06-18 23:45:36 »

Quote
this makes a shallow copy cause it's not making a new shooter?

No. There is no copy at all.

This is a reason that it shouldn't be called a shallow copy Undecided it's just referencing the object

Edit:

Just been reading about something called a copy constructor. You create a constructor inside the class you want to copy that's parameters take an object of the class type like so: public Dog(Dog dogToCopy). You can then call something like this inside another constructor/method:
1  
2  
3  
4  
5  
// Maybe this class is a person that wants an exact copy of the original dog
private Dog dog
public void copyOfDog(Dog dogInstance){
    this.dog = new Dog(dogInstance)
}

You then set all the fields inside Dog equal to ToCopy like so:
1  
2  
3  
4  
public Dog(Dog dogToCopy){
    age = dogToCopy.getAge();
    weight = dogToCopy.getWeight(); // etc
}


This creates an exact copy of the original dog BUT it never updates the variables if the original changes as it creates a whole new object and is not referencing the same object. Clever stuff!

Hey, you! Back to work
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #12 - Posted 2015-06-19 00:06:09 »

It's not called a shallow copy...


a = new ...();
b = a;               // alias
c = a.shallowCopy(); // shallow
d = a.deepCopy();    // deep

+---+             +---+
| 1 |<--------+   | 3 |
+-^-+         |   +-^-+
  |           |     |
+---+       +---+ +---+
| 1 |<--+   | 2 | | 3 |
+-^-+   |   +-^-+ +-^-+
  |     |     |     |
  a     b     c     d  


EDIT: that copy constructor is an example of a shallow copy. A deep copy (e.g. a correctly implemented clone()) also recursively copies reference-type fields.
Offline SauronWatchesYou

JGO Ninja


Medals: 33
Projects: 4
Exp: 2 years


Hi there! :)


« Reply #13 - Posted 2015-06-19 00:08:42 »

It's not called a shallow copy...

Shocked It.. it's not? I've read on multiple sites now as it called that. Strange Huh

Edit: I've also read that clone is broken and full of holes and so a lot of people avoid it?

Hey, you! Back to work
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #14 - Posted 2015-06-19 00:09:48 »

In his code: copy = shooter; is not a shallow copy, it's an alias (Java's standard operating procedure).
Offline SauronWatchesYou

JGO Ninja


Medals: 33
Projects: 4
Exp: 2 years


Hi there! :)


« Reply #15 - Posted 2015-06-19 00:14:37 »

In his code: copy = shooter; is not a shallow copy, it's an alias (Java's standard operating procedure).

Ooooo. So a shallow copy is where we actually do copy the object and don't just reference it? A deep copy is where we change the fields as a unique copy and an alias just points to the object?

Hey, you! Back to work
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #16 - Posted 2015-06-19 00:20:29 »

Yes.

Alias: copy a pointer.
Shallow: copy contents of object into new object, return pointer to new object. (which may contain aliased fields)
Deep: copy contents of object, recursively shallow copy any references in the contents, return pointer to new object hierarchy containing no aliases to original hierarchy anywhere.
Offline SauronWatchesYou

JGO Ninja


Medals: 33
Projects: 4
Exp: 2 years


Hi there! :)


« Reply #17 - Posted 2015-06-19 00:27:50 »

Ah, thanks for clearing that up Smiley a shadow copy should fit most cases, right?

*slaps medal on pizza* Grin

Hey, you! Back to work
Offline Therobodavo

Junior Devvie


Medals: 2
Exp: 2 years



« Reply #18 - Posted 2015-06-19 02:23:18 »

Welp, I guess I misunderstood a lot of things in java regarding objects :/. Seriously though, they don't explain this correctly in school.
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 288
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #19 - Posted 2015-06-19 12:16:08 »

Ooooo. So a shallow copy is where we actually do copy the object and don't just reference it? A deep copy is where we change the fields as a unique copy and an alias just points to the object?

Say you have a class like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
class Thing{
   private String thing;

   public Thing(String thing){
      this.thing = thing;
   }

   public void setThing(String thing){
      this.thing = thing;
   }
 
   public String getThing(){
      return thing;
   }
}


And you then have a List of instances of that class:

1  
2  
3  
4  
List<Thing> things = new ArrayList<>();
things.add(new Thing("zero"));
things.add(new Thing("two"));
things.add(new Thing("three"));


You can make a shallow copy of that List just by copying over those instances into a new List:

1  
2  
3  
List<Thing> moreThings = new ArrayList<>();
moreThings.addAll(things);
moreThings.add(new Thing("four")); //won't affect the first things List


However, since all you've done is copy the references to the existing Things into a new List, your new List is still pointing to the Things in the first List. In other words, changes to Things in the first List will be reflected in the second List:

1  
2  
things.get(0).setThing("uh oh");
System.out.println(moreThings.get(0).getThing());//prints "uh oh"


This will cause weird behavior if your code uses that second List to modify the Thing instances in a way that the first List wasn't expecting. To avoid that, you can make a deep copy by actually making copies of the Thing instances:

1  
2  
3  
4  
5  
List<Thing> deepCopy = new ArrayList<>();
for(Thing thing : things){
   Thing deepCopiedThing = new Thing(thing.getThing());
   deepCopy.add(deepCopiedThing);
}


Now your deepCopy List contains copies of the original Things, so changes to the instances in the deepCopy List will not be reflected in the original List:

1  
2  
deepCopy.get(1).setThing("six");
System.out.println(things.get(1).getThing()); //prints "one"


This is a simplified example, but those are the basics. Sometimes shallow copies are fine, and perhaps even preferable: it can be very complicated, very expensive memory-wise, or even downright impossible to create deep copies all the time. Which approach you take really depends on what you're trying to do.

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (45 views)
2019-09-04 15:33:17

hadezbladez (4025 views)
2018-11-16 13:46:03

hadezbladez (1468 views)
2018-11-16 13:41:33

hadezbladez (4048 views)
2018-11-16 13:35:35

hadezbladez (780 views)
2018-11-16 13:32:03

EgonOlsen (4098 views)
2018-06-10 19:43:48

EgonOlsen (4685 views)
2018-06-10 19:43:44

EgonOlsen (2762 views)
2018-06-10 19:43:20

DesertCoockie (3657 views)
2018-05-13 18:23:11

nelsongames (3867 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
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!