8 Replies - 1478 Views - Last Post: 01 September 2012 - 10:23 AM Rate Topic: -----

#1 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,258
  • Joined: 08-January 12

Something like pointers in C#

Posted 01 September 2012 - 08:23 AM

Hello everyone :) I am currently working on a game, and would like some advice. I have a class for attacks, in which I have 2 variables - targetX and targetY. Let us say the constructor for that class looks something like
public Attack(int targetX, int targetY)

And I am calling it by writing
Attack attack = new Attack(X,Y);

Now, the problem I am facing is, when I create the attack, the values of targetX and targetY are saved as X and Y respectively. But, in other part of my code, the value of X and Y are changing continuously, and I need to get those updated values in targetX and targetY. What I mean is, whenever the value of X and Y are changing, I want the value of targetX and targetY to change automatically. I tried passing it by ref, but it didn't work. I know in C/++ I could have easily used pointers, but I don't think there are pointers in C#. Is there any other way?

Is This A Good Question/Topic? 0
  • +

Replies To: Something like pointers in C#

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6508
  • View blog
  • Posts: 14,375
  • Joined: 02-June 10

Re: Something like pointers in C#

Posted 01 September 2012 - 08:30 AM

C# has pointers

But I think the bigger issue here is that since you have C++ experience you are trying to code like C# is just C++ with different syntax used in for loops and so on. That's just not the case.

C# is far more event driven than C++. Based on your description what I would do is raise an event when the values are changed. Then any other class instance can subscribe to that event and react however it is meant to.

Don't try to micro-manage C#. Don't worry about having to expressly push values from one class to another or one method to another. Embrace the events and let the different classes talk to each other. Let each class be a self-contained black-box that is easily re-used and isolated as much as possible.

Take 30 minutes out of your day and work these tutorials completely. Don't read them - work them - build the examples and study them. Armed with your new understanding I think you'll see how to make good use of the events in your project.

Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Was This Post Helpful? 3
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2512
  • View blog
  • Posts: 3,990
  • Joined: 21-June 11

Re: Something like pointers in C#

Posted 01 September 2012 - 08:34 AM

There are pointers in C#, but you probably don't want to use them since they only work in unsafe code and require you to pin objects, so the GC doesn't move them around.

In your case, why don't you just stop using X and Y after you create the attack object and instead use attack.TagetX and attack.TagetY?

This post has been edited by sepp2k: 01 September 2012 - 08:34 AM

Was This Post Helpful? 2
  • +
  • -

#4 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,258
  • Joined: 08-January 12

Re: Something like pointers in C#

Posted 01 September 2012 - 08:36 AM

Thanks for the tuts, will surely look into them :)

BTW I was half fearing you would reply, shouting at me for my lack of experience and posting the entire FAQ which you always post :P Still, I appreciate your help. And yeah, you are correct.

@sepp2k : The reason is, actually X and Y are the coordinates of mouse in case of user's attack, and X and Y in case of AI's attack. That's why I can't do it.

EDIT : Although I am looking into events (once again), can anybody explain why pointers are bad?

This post has been edited by aresh: 01 September 2012 - 08:39 AM

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6508
  • View blog
  • Posts: 14,375
  • Joined: 02-June 10

Re: Something like pointers in C#

Posted 01 September 2012 - 08:46 AM

View Postaresh, on 01 September 2012 - 09:36 AM, said:

BTW I was half fearing you would reply, shouting at me for my lack of experience and posting the entire FAQ which you always post :P Still, I appreciate your help. And yeah, you are correct.


Happy to disappoint in this case. Though I'm not so sure I've ever yelled at someone for lack of experience. Lack of effort: Sure. Absolutely. And the FAQ post sure saves a lot of time for the top 50 questions that get asked 10 times a week. Generic repetitive questions get generic repetitive answers. But that's how more time can be spent on original questions (like this one) where someone has made an effort and isn't asking for a hand-out.

We all lack experience at LOTS of things. I have very little when it comes to databases and consuming website feeds. Yet that is a major part of a lot of coding these days depending on what your company does. One can't get upset about a lack of experience. But I sure can't abide laziness. So much is learned through trial and error. I feel everyone should at least do their best to research and experiment with 5 different ways of doing something before they give up and ask for help. Even if those 5 different ways don't resolve the problem they will result in learning new things and trying new techniques that might be applicable to some other issue, now or in the future.
Was This Post Helpful? 1
  • +
  • -

#6 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,258
  • Joined: 08-January 12

Re: Something like pointers in C#

Posted 01 September 2012 - 09:03 AM

After going through the first link you posted, I am thinking is it really worth that much effort? I mean, I can do it, that is no problem, but why not simply use pointers? 'Coz raising an event, subscribing to it and then taking action... Simply using pointers would save me a lot of trouble. Last time I tried event (2 months ago), I hated them.

This post has been edited by aresh: 01 September 2012 - 09:04 AM

Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6508
  • View blog
  • Posts: 14,375
  • Joined: 02-June 10

Re: Something like pointers in C#

Posted 01 September 2012 - 09:37 AM

View Postaresh, on 01 September 2012 - 09:36 AM, said:

Thanks for the tuts, will surely look into them :)

[...] and posting the entire FAQ which you always post :P

PSST: Those tutorials you were happy to see... They're in the FAQ post you were afraid to see.

View Postaresh, on 01 September 2012 - 09:36 AM, said:

EDIT : Although I am looking into events (once again), can anybody explain why pointers are bad?

We try to avoid unmanaged code in managed languages. As sepp2k pointed out they have to be handled carefully (micro-managed), allowing of unsafe code and are not manageable by the GC (Garbage Collection). For years even experienced professionals have had issues with this resulting in pointers that never get disposed of, memory never freed up etc. resulting in memory leaks. That's a big part of the entire desire to create managed languages and a GC that will automatically dispose of such things when they go out of scope.

They really don't need to be used casual as you have here. They have their place in very specific situations where they have a limited need and lifespan. Image processing is a good example of that. It is faster to rip through a bitmap pixel-by-pixel with pointers to direct memory addresses. When you have 18 million pixels in a photo that time savings adds up quick. But it all takes place in a single method that does something to the bitmap then is deliberately disposed of at the end of the method.

Using them in your described manner like an easy global variable is dangerous. It also violates the basic underlying concept of Separation of Responsibilities we use in C#. If you use pointers. Using events your two classes would just react to an event. It doesn't know or care how or from where it gets raised. If some big underlying principal of the program is changed your classes don't have to be scrubbed through and updated. If you use pointers then you are married to that. Later when you update this to be far more object oriented you aren't stuck.

Think about this: your pointers work for THIS simple design. What happens when there are 50 enemy players? Are you going to make 50 xy pointers? No: You're going to do the OOP thing of an enemy class raising a Moved event that provide eventargs containing the new xy position.

We use the same conepts in small programs as we do in big one so we don't go nuts. I don't want to have track how things were done by the size of the project:

Quote

This is a small project so I did it this way.
This is a medium project so I did it this other way.
Now the boss wants a change, so I have to retro-fit the medium project to this different technique because it is now a big project.



Quote

Simply using pointers would save ma a lot of trouble.

Then do it. Nobody is telling you how you have to make your program; just providing some insights based on experience. Sometimes we all have to actually experience for ourselves to learn and grow.
Was This Post Helpful? 1
  • +
  • -

#8 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,258
  • Joined: 08-January 12

Re: Something like pointers in C#

Posted 01 September 2012 - 09:49 AM

I appreciate your advice. Thank you.
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6979
  • View blog
  • Posts: 14,605
  • Joined: 16-October 07

Re: Something like pointers in C#

Posted 01 September 2012 - 10:23 AM

To be clear, an object variable is a reference to an instance. You may think of this as a C++ reference or a C pointer. You don't have the address of the object, but you do reference the same object.

It's really more fun to prove in code:
class Attack {
	public Attack(int x, int y) { this.X = x; this.Y = y; }
	public int X { get; set; }
	public int Y { get; set; }
	public override string ToString() { return "Attack(" + X + ", " + Y + ")"; }
}
//...
Attack a1 = new Attack(3, 4);
Attack a2 = a1; // reference to instance, think pointer
Debug.WriteLine("a1 = " + a1 + ", a2 = " + a2);

a1.Y = 22;
Debug.WriteLine("a1 = " + a1 + ", a2 = " + a2);

a2 = new Attack(5, 5); // new instance
Debug.WriteLine("a1 = " + a1 + ", a2 = " + a2);



Results:
a1 = Attack(3, 4), a2 = Attack(3, 4)
a1 = Attack(3, 22), a2 = Attack(3, 22)
a1 = Attack(3, 22), a2 = Attack(5, 5)



For a game, you often want an object to have the ability to announce that it has changed. Be aware of the event model and the ease of implementing custom events before you go nuts thinking along the lines of "pointers". If other parts of your code always need to know of changes to a value an event is recommended.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1