LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-13-2004, 11:42 AM   #1
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Rep: Reputation: 47
java, call function in calling object


I have a class called Game.class, inside it creates an instance of another class called GameCharacter.class, from GameCharacter is it possible to call a function inside Game.class?

example

class Game impliments CanBeUsedByCharacter
{
GameCharacter Bob = new GameCharacter();
//other stuff
public void DoSomething()
{

}
//other stuff
}

class GameCharacter
{
if (CallingObject instanceof CanBeUsedByCharacter)
{
(CanBeUsedByCharacter)(CallingObject).DoSomething();
}
}


that is essentially what I want to do, I know I can pass Game into GameCharacter with the "this" keyword, but this is a watered down version of what I am doing, and while the "this" keyword will work it is extreamely ugly for my purposes
 
Old 06-13-2004, 11:43 AM   #2
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
let me specify, I have a lot of object with sub-objects with yet more sub-objects, all from a main object, they all need to be able to through events that are intercepted by yet another object that only contains the Game object, and the functiosn to through the event are in the Game object.
 
Old 06-13-2004, 03:33 PM   #3
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
I'm not sure exactly what you are wanting to do, sounds even, a bit like inter-thread communication, but seeing as GameCharacter appears to have no relationship at all to Game I'm at a bit of a loss how GameCharacter can call anything in Game directly as it doesn't at first glance appear to know it exists, of course there are probably a ton more of enlightened folk out there. Now if it GameCharacter was an inner/member class of Game that would be different. It all seems a bit upside down
 
Old 06-13-2004, 03:37 PM   #4
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
ok let me explain what I am doing:

there is the main game object, any interaction done with game from a frontend is done here.
every time a character moves the game object throws an event to the frontend saying hey update the display a character moved, he moved from x, y to x, y. however each chracter object is it's own thread, it controls when and where it moves, so it needs to be able to call the updatedisplay event caster that is inside of the game object.
 
Old 06-13-2004, 04:35 PM   #5
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Okay, I'm assuming you are only going to have one "Game", yeah?


So how about making the relevant methods that you want to call static.

i.e

public static void doSomething(){

blah...blah
}


Then call it by

Game.doSomething();
 
Old 06-13-2004, 04:48 PM   #6
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
well, the game is an active game, and the dosomething function is actually pass an event to listeners, and the game object is keeping track of the listeners, so it can't be as you say..


let me rephrase:
game object contains characters, event catsers, and the list of things listening for events, th characters in the game object need to tell the game object to throw an event to the listener.
 
Old 06-13-2004, 06:25 PM   #7
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Struggling a bit, are the characters data structures or do they also extend gui components, just wondering becuase if they were you could use a component listener and when it move it would fire a component moved event which could call whatever routine you want
 
Old 06-13-2004, 06:34 PM   #8
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
I have figured out a different way to doit, thank you for your help.
 
Old 06-13-2004, 06:40 PM   #9
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
No problem, for all the good I was, anyway seeing as I'm interested, what was the general way you got round what you are trying to do?
 
Old 06-13-2004, 11:49 PM   #10
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

Rep: Reputation: 31
Quote:
Originally posted by exodist
let me specify, I have a lot of object with sub-objects with yet more sub-objects, all from a main object, they all need to be able to through events that are intercepted by yet another object that only contains the Game object, and the functiosn to through the event are in the Game object.
Java has a really slick mechanism for notifying one object that another object has changed. Look at java.util.Observer and java.util.Observable. Here's a toy example to demonstrate the idea.

The first class extends Observable. Whenever the state of the object changes, you call setChanged() to indicate that the object has changed, then call notifyObservers() or notifyObservers( Object arg ) to tell all of the Observers of the object that the state has changed. The Observers can either query the Observable object for its new state via accessor methods, or the Observable object can pass an Object argument to all of the Observers. For this little example, whenever the field x in MyObservable changes, it notifies all of the Observers passing the new value of x to the Observers.

Code:
// MyObservable.java

import java.util.Observable;

public class MyObservable extends Observable {

   private int x = 0;

   public void setX( int x ) {
      this.x = x;
      setChanged();
      notifyObservers( new Integer( x ) );
   }
}
The next class implements the Observer interface. This interface has only one method: update( Observable o , Object arg ). In this example, it just gets the new value of x and prints it out.

Code:
// MyObserver.java

import java.util.Observable;
import java.util.Observer;

public class MyObserver implements Observer {

   public void update( Observable o , Object arg ) {
      Integer i = (Integer)arg;
      System.out.println( i );
   }

}
The last class is just the main driver of the whole thing. It creates a MyObservable and MyObserver, and adds the MyObserver to the MyObservable. Then it updates the MyObservable's x value so that the MyObserver will print the value out.

Code:
// Observation.java

public class Observation {

   public static void main( String[] args ) {

      MyObservable observable = new MyObservable();
      MyObserver observer = new MyObserver();
      observable.addObserver( observer );

      for ( int x = 0 ; x  < 10 ; x++ ) {
         observable.setX( x );
      }
   }

}
One of the cool things about using Observable/Observer is that you can have different Observers that react differently to changes in the Observable without Observable having to know anything about the Observers.

For the problem you are working on, the Game could be an Observer of each of the GameCharacters. When a GameCharacter moves it can notify the Observer (the Game) of its new position by passing a Point with the new locations. Likewise the frontend can be an Observer of the Game. When the Game is notified by the GameCharacter of the character's new position, the Game can notify the frontend.

Last edited by eric.r.turner; 06-14-2004 at 01:36 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Function pointers, calling, and templates The_Nerd Programming 2 10-09-2005 07:08 PM
Calling another function from a function using GTK geminigal Programming 4 07-11-2005 03:15 PM
object loses data at funcyion call (c++) freeindy Programming 1 05-10-2005 05:15 PM
switch statements calling a function tekmorph Programming 2 10-19-2004 05:53 PM
Calling my function through MACROS in OpenOfficeWriter mcp_achindra Linux - Software 0 07-05-2004 06:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration