• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Scene Graph

 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm trying to create something called a scene graph, where I have multiple nodes in a tree like structure. Every node is a different class (for the most part) but they all inherit from the scene graph node class. Now, putting the objects
into the graph is easy - since they are all inheriting from the same root type. The problem, my problem, is that if I want to get data OUT of the scene graph, or look at one node for instance, I can only look at the base class,
not the class that inherits from the base class. Does anybody have any suggestions for me?

I also tried writing a function that registered the nodes with a string in a hashmap, but I ran into the same problem. When I tried to pull nodes out of the hashmap, they all had to be the same type or inherit from the same type
so I lost the subclass type info.


Thanks

Ted
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can down cast if you know which node type you are dealing with.
You should also review your design. Perhaps the RootNode class should be abstract and should call for a wider range of methods that every subclass should support. This is the kind of mess that Java, having everything inherited from Object, caused. Hence, the need for generics for stronger typing. It's hard to say more without seeing some code examples of your problem.
 
Ted Gress
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would this work? Assuming CreateFirstPlayerCharacterService and the other services inherit from the same base class?


 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It probably would work, but I didn't read it. All those if statements look like a problem with your design to me. For a start, you can use a switch statement with those String literals. For another, you are breaching the principle of single responsibility. The service class should deal with such divisions into different objects. That block will otherwise be a maintenance nightmare, and that nightware will be even worse if you repeat that sort of selection anywhere else.
There is also something suspicious about all the effort you are going to to pass null as a constructor argument. If you had a no‑arguments constructor, you could use this very clunky method using reflection:-
Class.forName("CampbellsServiceType").newInstance()
but you would have to make the new node have null via the constructor.
If you follow enough of those links, you will find I have misled you into using deprecated API, but the documentation does tell you a reasonably simple alternative. Beware: those methods declare a list of checked exceptions as long as your arm.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The newInstance trick does require a no‑arguments constructor; if you are passing a real element, you would have to add that via a setElement method.
Never write == false or == true, which are both poor style and error‑prone if you write = by mistake.
Never
if (b == false) ...
always
if (!b) ...
Never
if (b == true) ...
always
if (b) ...
 
reply
    Bookmark Topic Watch Topic
  • New Topic