Skip to main content
gamedev.net
🔒 Locked

Referencing objects in C# without using pointers

Started by CIJolly Mar 5, 2009 at 4:10 AM 2 replies 1.2k views
Original Post
CIJolly
CIJolly
Hey all. I'm returning to game coding after a long absence, and have switched to C# and XNA on the grounds that it makes everything a million times easier for a hobby coder such as myself. However, from what I am reading, pointers are something to be avoided in C# where possible. I've read that "unsafe" code isn't as ominous as it sounds, but all the same... So, there are a couple of things I would do with pointers in C++ that I am wondering how to replicate in C#. The first would be allowing objects to "target" other objects. This could be a heat seeking missile that would have previously given a pointer to it's target, or a player that is given a list of pointers to objects in his inventory. I thought about tracking the objects by their place/index in the linked list (eg, missile target = int targetindex where targetindex is the target enemies position in a list) but when objects get removed from the list these indexes change. Is the solution to just add code to keep up with these changes (eg, whenever something is deleted before the missiles target, have a function iterate through the list and change any out of date indexes). The other problems was with derived/child classes. I want to have an array of physical objects that behave the same way in many respects (eg, collisions and physics) and reside in the same list. However, special objects like players need to be handled differently. In C++ I would make an array of pointers to the players, and iterate through it to perform special player code like input. How can I replicated this in C#? Is it a case of going through the list and finding index entries to the player's objects?
DevFred
DevFred
Quote:
Original post by CIJolly
The first would be allowing objects to "target" other objects.

You get this for free in C#, as classes have reference semantics.
Ship s = new Ship();Missile s = new Missile(s);

Here, s is a C# reference (something like a C++ pointer) to a Ship. This C# reference is passed by value to the Missile constructor. There is still only one Ship object, only the reference gets copied.

If for some other objects you don't want reference semantics but value semantics, use structs for those.
cignox1
cignox1
Quote:

The other problems was with derived/child classes. I want to have an array of physical objects that behave the same way in many respects (eg, collisions and physics) and reside in the same list.
However, special objects like players need to be handled differently. In C++ I would make an array of pointers to the players, and iterate through it to perform special player code like input. How can I replicated this in C#? Is it a case of going through the list and finding index entries to the player's objects?


You should avoid to think in terms of c++ when working with c#, and in the ssame time things are sometimes very similar.

You should take into account the follwing fact: c# does NOT provide multiple inheritance (like c++ does) and c# DOES provide interfaces (just like java, kind class with only pure virual methods).

In the case you described, you can have a 'PhysicalEntity' interface, implemented by every class that describes a physical entity. Methods that work with those items (like collision detection procedures) will then accept as parameters those interfaces. They don't need to know what kind of object is, as long as the interface is implemented.
If you need to find only the players in the list you have many different options: one is to have a separate array (or list) that only holds players. Another option is to use Reflection (if(item is Player) { //execute player code }
This solution is easy, but not necessarily a good one.

lists and array traversal can be done using the foreach statement (wich does not exists in c++).

And remember that references are not really as pointers: there is no pointer arithmetic available, and memory is managed by the runtime, not by you.
CIJolly
CIJolly
At Devfred.
Wow. It's funny, that is exactly how I first tried to get things to target each other in C++. Of course, it all turned out to be much more complicated than that. But this...wow, that's really, really logical. Thanks for the heads up, who knows how long that would have taken me to figure out!

At Cignox.
Interfaces are another thing I hadn't encountered. I'll have to look into them for my program. Thanks for the answer.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.