• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

This topic is now archived and is closed to further replies.

Smoo

[java] Java and pointers

7 posts in this topic

Greets folks, I''ve got a problem. Now I know that java is pointers but I''m at a loss here. I have a class called mapfile that contains all the info I need for a map. I tried using a hashtable so I would have htMapFile.put(key, mapfile); then I would change the contents of mapfile and repeat the above process... I was hoping that it wouldn''t do what it did. htMapFile key 1 --> points to mapfile change mapfile contents htMapFile key 2 --> changed mapfile but htMapFile key 1 --> also points to changed mapfile hence the joy of pointers is there a way to perhaps clone mapfile so when it dumps the info into the hashtable i can have in key 1 different data than key 2? There has to be a way to work around it... somehow Help/Suggestions would be appreciated Smoo
0

Share this post


Link to post
Share on other sites
you want to clone an object. I think there is a method in Object called clone(). Since all classes are subclasses of object every class has the clone() method. I assume it works like this:

Map m1 = new Map();
Map m2 = m1.clone();

So now m1 and m2 are two different but identical objects.
0

Share this post


Link to post
Share on other sites
The clone method is protected.
To use it your class needs to implement Cloneable.
Also clone makes a shallow copy, i.e. it doesn''t clone any internal objects.
So if you have Map m2=m1.clone(); and say there is an object in Map called picture, then m1.picture==m2.picture. Thats is, they point to the same thing, modifying one will mdify the other also.

A better solution to your problem is to just make two maps using the same constructor.
0

Share this post


Link to post
Share on other sites
Another solution to get a deep copy would be to serialze the object, then unserialize it into another variable.
0

Share this post


Link to post
Share on other sites
Hmm... Yes, in your case, the clone() method, even if it is supported, isn't what you are looking for. I, too, thought that using serialization would be a clever solution, but it would copy too deeply... It would copy every object stored in the map, not just the mapping information.

I'd say that the anonymous poster's approach is on the right track, although more complicated than necessary. You should be able to copy a Hashtable object by just using something like this:

Hashtable htMapFileCopy = new Hashtable(htMapFile);

Of course, since I went to the work of figuring out how to do deep copying using serialization, I'm going to include that code anyway... Just remember that it IS NOT what you want, because it'll copy everything you store in it, key and value pairs, and the entire graph of objects each of those elements references. If your design is interconnected enough, you could end up duplicating your entire world with this...


                

import java.io.*;

public final class DeepCopy {

//don't allow this class to be instantiated

private DeepCopy() {}

public static Object copy(Serializable s)
throws IOException, ClassNotFoundException
{

//serialize the object into a byte array

ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(s);
oo.flush();
byte [] b = bo.toByteArray();
oo.close();

//construct a copy of the object from the byte array

ByteArrayInputStream bi = new ByteArrayInputStream(b);
ObjectInputStream oi = new ObjectInputStream(bi);
Object copied = oi.readObject();
oi.close();

//return the copy

return copied;

}//copy


}// class DeepCopy



(Edited because the source tag ate my white space...)

Edited by - c_wraith on October 16, 2000 2:31:02 AM
0

Share this post


Link to post
Share on other sites
You could still use object serialization, just create custom readObject and writeObject methods. You can then control exactly what gets persisted and what doesn''t.

,Jerry
0

Share this post


Link to post
Share on other sites
c_wraith, what you wrote was interesting and I''ll try out a few things with that.

You bring up a couple of questions,
1. The mapfile class contains only a few variables and a TriggerObject[] which holds info for events. This is all for 1 map only. The hashtable is there for caching this stuff basically.
1. Load the map in mapfile
2. Dump it to hashtable
3. If goes to a different map repeat 1 and 2
4. If goes to a map that has been loaded into hashtable dump from hashtable to mapfile

and mapfile = null; between loadings so it would lose all pointer info to other vars, etc.. so serialization should work.. shouldn''t it?

However when you bring up the point of
Hashtable htMapFileCopy = new Hashtable(htMapFile);

I don''t see how for instance if I have
1. Map1 is dumped to htMapFile key 1
2. Create a new instance (Yes this would work for it here)
3. Map2 is dumped into htMapFile key 2
4. But since it''s pointers, key 1 and key 2 are now alike
so I don''t follow how creating a new instance of hitMapFile into htMapFileCopy would do the trick here because it will have removed all info from htMapFileCopy and replace it with htMapFile.

You have given me something to try out and I''m gonna try it out right now.

I just thought of something else I could try since the
Hashtable htMapFileCopy = new Hashtable(htMapFile);
gave me an idea.

if I have this:
Hashtable htMapFileCopy = new Hashtable(htMapFile);
Hashtable htMapFile = new Hashtable(htMapFileCopy);

nothing in htMapFile should be linked to mapfile and I could re-input new data into mapfile and dump it to htMapFile.

Thanks folks, I''ll keep you posted
Smoo
0

Share this post


Link to post
Share on other sites
Well I think I''m stupid.

all it took was a simple
mapfile = null;
mapfile = new MapFile();
reset all stats in mapfile then dump it into the hashtable.

before I i had just put all stats in mapfile to null and thought that would work. well, creating a new instance of the mapfile loses all pointer info, nullifying the vars doesn''t.

c_wraith you did help out quite a bit actually by giving me the idea of a new instance. It wasn''t something that I didn''t know about, just I didn''t think to use. Well it shows I''m still a newbie

Anyways, now that that''s working hopefully my attempts at caching will work.

Smoo

Btw the Hashtable htCopy = new Hashtable(oldHt);
doesn''t work. it''s not one of the constructors.
0

Share this post


Link to post
Share on other sites