digitalmars.D.learn - Storing a reference to the calling object
- Tim (27/27) May 23 2020 Hi all, I'm a little new to D and I'm wondering how I can store a
- drug (30/50) May 23 2020 You can make the Sprite class templated one:
- Mike Parker (14/42) May 23 2020 Since you're using classes, one way is to use a common base class
- Tim (4/17) May 23 2020 Thanks a lot for this. I was trying not to overcomplicate things
- Luis (42/70) May 23 2020 For example using a interface or a base class
Hi all, I'm a little new to D and I'm wondering how I can store a reference to the calling object. I want to create a reference to an object's parent so that each time I go to update the sprite, it is able to grab its position from the parent. So if I have: class Sprite{ /// Postional components of the sprite int* x, y; SDL_Surface* image_surface; auto parent; this(const char* path, auto parent){ writeln(*x); this.parent = parent; } void update(){ // Copy loaded image to window surface writeln("Sprites x: ", *x); SDL_Rect dstRect; dstRect.x = parent.x; dstRect.y = parent.y; SDL_BlitSurface(image_surface, null, g_surface, &dstRect); } } And call it like so: sprite = new Sprite(path, this); How can I make this sort of thing work? Thanks a lot in advance for the help!
May 23 2020
23.05.2020 12:27, Tim пишет:class Sprite{ /// Postional components of the sprite int* x, y; SDL_Surface* image_surface; auto parent; this(const char* path, auto parent){ writeln(*x); this.parent = parent; } void update(){ // Copy loaded image to window surface writeln("Sprites x: ", *x); SDL_Rect dstRect; dstRect.x = parent.x; dstRect.y = parent.y; SDL_BlitSurface(image_surface, null, g_surface, &dstRect); } }You can make the Sprite class templated one: ``` class Sprite(T){ /// Postional components of the sprite int* x, y; SDL_Surface* image_surface; T parent; this(const char* path, T parent){ writeln(*x); this.parent = parent; } void update(){ // Copy loaded image to window surface writeln("Sprites x: ", *x); SDL_Rect dstRect; dstRect.x = parent.x; dstRect.y = parent.y; SDL_BlitSurface(image_surface, null, g_surface, &dstRect); } } auto sprite(T)(const char* path, T parent) { return new Sprite!T(path, parent); } ``` and use it like: ``` auto sprite = sprite(path, this); ```
May 23 2020
On Saturday, 23 May 2020 at 09:27:46 UTC, Tim wrote:Hi all, I'm a little new to D and I'm wondering how I can store a reference to the calling object. I want to create a reference to an object's parent so that each time I go to update the sprite, it is able to grab its position from the parent. So if I have: class Sprite{ /// Postional components of the sprite int* x, y; SDL_Surface* image_surface; auto parent; this(const char* path, auto parent){ writeln(*x); this.parent = parent; } void update(){ // Copy loaded image to window surface writeln("Sprites x: ", *x); SDL_Rect dstRect; dstRect.x = parent.x; dstRect.y = parent.y; SDL_BlitSurface(image_surface, null, g_surface, &dstRect); } } And call it like so: sprite = new Sprite(path, this); How can I make this sort of thing work? Thanks a lot in advance for the help!Since you're using classes, one way is to use a common base class or an interface. But assuming "parent" is the owner of the Sprite instance, you might eliminate the dependency on the parent and have it update the Sprite's position when it's updated instead of maintaining a position separately. class Parent { private Sprite sprite; void updatePosition(int x, int y) { sprite.x = x; sprite.y = y; } }
May 23 2020
On Saturday, 23 May 2020 at 09:48:57 UTC, Mike Parker wrote:Since you're using classes, one way is to use a common base class or an interface. But assuming "parent" is the owner of the Sprite instance, you might eliminate the dependency on the parent and have it update the Sprite's position when it's updated instead of maintaining a position separately. class Parent { private Sprite sprite; void updatePosition(int x, int y) { sprite.x = x; sprite.y = y; } }Thanks a lot for this. I was trying not to overcomplicate things this early in development but I think in me holding back I made things more complex then they needed to be.
May 23 2020
On Saturday, 23 May 2020 at 09:27:46 UTC, Tim wrote:Hi all, I'm a little new to D and I'm wondering how I can store a reference to the calling object. I want to create a reference to an object's parent so that each time I go to update the sprite, it is able to grab its position from the parent. So if I have: class Sprite{ /// Postional components of the sprite int* x, y; SDL_Surface* image_surface; auto parent; this(const char* path, auto parent){ writeln(*x); this.parent = parent; } void update(){ // Copy loaded image to window surface writeln("Sprites x: ", *x); SDL_Rect dstRect; dstRect.x = parent.x; dstRect.y = parent.y; SDL_BlitSurface(image_surface, null, g_surface, &dstRect); } } And call it like so: sprite = new Sprite(path, this); How can I make this sort of thing work? Thanks a lot in advance for the help!For example using a interface or a base class ```D interface IBaseInterface { int x(); void x(int newX); int y() void y(int newY); ... } class Parent : IBaseInterface { private int x, y; int x() { return this.x; } void x(int newX) { this.x = newX; } int y() { return this.y; } void y(int newY) { this. y = newY; } ... void f() { auto sprite = new Sprite("foo/bar/sprite.png", this); ... } } class Sprite{ IBaseInterface parent; ... this(const char* path, IBaseInterface parent){ writeln(*x); this.parent = parent; } void update(){ // Copy loaded image to window surface writeln("Sprites x: ", *x); SDL_Rect dstRect; dstRect.x = parent.x; dstRect.y = parent.y; SDL_BlitSurface(image_surface, null, g_surface, &dstRect); } } ```
May 23 2020