Welcome,
Guest
. Please
login
or
register
. Did you miss your
activation email?
French forum
Home
Help
Search
Login
Register
SFML community forums
»
Bindings - other languages
»
DotNet
»
Inheritance question [SOLVED]
Print
Pages: [
1
]
Author
Topic: Inheritance question [SOLVED] (Read 6909 times)
0 Members and 1 Guest are viewing this topic.
Mario1159
Newbie
Posts: 7
Inheritance question [SOLVED]
«
on:
July 14, 2018, 10:50:11 pm »
I want to access to the rectangle/convex/circle methods in my sGameObject, dGameObjects or Player class but i dont know how, because my current class sGameObject just have a Shape {get;set;} so i can create for example a sGameObject doing this:
RectangleShape rectangleShape
=
new
RectangleShape
(
new
Vector2f
(
50f,50f
)
)
;
SGameObject box
=
new
SGameObject
(
)
;
box
.
Shape
=
rectangleShape
;
//but i cant access to the RectangleShape methods
box
.
RectangleShape
.
getSize
(
)
;
// error
«
Last Edit: July 15, 2018, 04:54:46 am by Mario1159
»
Logged
dabbertorres
Hero Member
Posts: 505
Re: Inheritance question
«
Reply #1 on:
July 15, 2018, 12:12:21 am »
You've kind of half-answered your own question I think...
As you said, your
SGameObject
class has a
Shape
field. So you should be accessing the field's
GetSize()
method.
As in,
box.Shape.GetSize()
. Right?
Logged
Github
Mario1159
Newbie
Posts: 7
Re: Inheritance question
«
Reply #2 on:
July 15, 2018, 01:22:16 am »
Shape doesnt have a definition for GetSize (
https://www.sfmldev.org/documentation/2.5.0/classsf_1_1Shape.php
)
however the RectangleShape has the getSize method but i cant access to that in my sGameObject class
or maybe i didnt understood your answer correctly.
Logged
Hapax
Hero Member
Posts: 3379
My number of posts is shown in hexadecimal.
Re: Inheritance question
«
Reply #3 on:
July 15, 2018, 04:08:55 am »
I don't think your inheritance flow makes sense.
Is it possible that a "dynamic game object" that "can move"
is a
"static game object" that "can't move"?
I honestly think it needs some more careful thought.
Anyway, from the shapes to Shape and back, you can cast. Just be really careful to make sure that they are the thing you are casting them to/from.
For example:
sf
::
RectangleShape
*
rectangle
=
new
sf
::
Rectangle
;
sf
::
Shape
*
shape
=
dynamic_cast
<
sf
::
Shape
*
>
(
rectangle
)
;
// ...
sf
::
RectangleShape
*
thatRectangle
=
dynamic_cast
<
sf
::
RectangleShape
*
>
(
shape
)
;
That's untested. It's also a pretty volatile system
Logged
Selba Ward
-SFML drawables
Cheese Map
-Drawable Layered Tile Map
Kairos
-Timing Library
Grambol
*
Hapaxia Links
*
Mario1159
Newbie
Posts: 7
Re: Inheritance question
«
Reply #4 on:
July 15, 2018, 04:54:34 am »
It works! thanks and also thanks for the quote about my inheritance flow because im a beginner in concepts of oop and my code always tend to look like a spaghetti
Logged
eXpl0it3r
SFML Team
Hero Member
Posts: 11033
Re: Inheritance question [SOLVED]
«
Reply #5 on:
July 16, 2018, 10:13:19 am »
It's also recommended to use composition over inheritance. While might argue that your game object
is a
shape of some sort, it's better to just have it as a member of the game object (
has a
relationship).
This makes for a better design as you don't force the interface of shape classes onto your game object classes and you can better encapsulate the object logic.
Are you working with C# then?
Logged
Official FAQ:
https://www.sfml-dev.org/faq.php
Official Discord Server:
https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog:
https://duerrenberger.dev/blog/
Mario1159
Newbie
Posts: 7
Re: Inheritance question [SOLVED]
«
Reply #6 on:
July 16, 2018, 06:52:24 pm »
Yes, im working in c# and thanks for the recommendation
Logged
Print
Pages: [
1
]
SFML community forums
»
Bindings - other languages
»
DotNet
»
Inheritance question [SOLVED]
anything