« first day (343 days earlier)      last day (2515 days later) » 

10:55 AM
Hi, I have an abstract base class Component and Collider class that is derived class (inherits from Component). Additionally, I have Texture class which also inherits from Component. So now, I have class Object. I want to something like this: int x(type){ if(type == Texture) return 0; if(type == Collider) return 1; } (in C++). So, is it possible to do this in C++?
Wow, I cannot edit no more. Ignore the question above, the "New question" is the new and correct question.
New question: Hi, I have an abstract base class Component and Collider class that is derived class (inherits from Component). Additionally, I have Texture class which also inherits from Component. I want to something like this: int x(Component component){ if(component == Texture) return 0; if(component == Collider) return 1; } (in C++). How can be this made in C++? Is it possible?
 
It is possible, but pointless. It's usually a way better idea for dispatch to happen naturally with virtual functions.
 
Yeah, got the same answer in the other group. I will look at what virtual function is. Thank you.
 
well, you're desperately want to see how your idea would look like, take a look at typeid: en.cppreference.com/w/cpp/language/typeid, but you should really use virtual functions
(cannot remember this damn syntax for links >_<)
 
@MuhamedCicak Usually we're moving questions from that room here
 
@milleniumbug Yeah, sorry for asking on both of them. When I was typing here, no one was online and I thought it will be like that for a few hours. So I asked in other group as well.
@login_not_failed I will look at virtual functions, as @milleniumbug mentioned as well.
@milleniumbug So as I understood what virtual function is, is this good solution to my answer: I would make virtual function in Component class called getType(). And then in both Texture and Collider I would override that virtual function. But the thing is, I dont know what getType function's actual code would look like? I can say just "return this" right? Because, then, what would be the return type?
Just please wait until i edit
I pressed enter mistakenly
Okay I edited it.
I cant* say just "return this;" right?
Sorry, I am really clumsy while typing
So I hope you could managed to read all this mess correctly...
 
11:20 AM
you have 2 minutes to edit your message, btw :P
 
Did not know about that...
 
have you tried searching examples of using virtual functions? like this one: stackoverflow.com/a/2392656/7561577
your getType() idea is still the same approach as before, just presented differently — it's not quite the intended usage of this concept
 
The point is, as I saw in the post you just gave me, he uses non abstract class. So, he can make instance of Animal. While I cannot make instance of Component. So I need to use pure virtual function I guess?
And again, even if I do use it. What would return type of getType() function be? That is the part where I cannot understand...
 
it shouldn't be getType(), you should redo your classes such that your virtual function would do anything meaningful
 
@MuhamedCicak what are you going to do with that value
 
11:31 AM
Okay, I want to have Components that I can attach to objects. But sometimes, I need to know what type of are the components. It can be Collider, Texture, etc. So that is my question.
 
say, you have a class Animal, from which you do two more classes: Cat, Dog; and so the useful function would be make_noise(); but you are trying to identify a type, and then do make_cat_noise() and make_dog_noise()
 
Think of like in Unity engine.
 
these are different approaches, and the latter one isn't the right one
@MuhamedCicak so you want to create different objects with different properties: like, a smoke particle shouldn't have a collision model and a bump mapping, right?
if so, how many different types of objects do you want to produce?
 
Im writing more detailed now, just a sec. I think this time i may explain it good.
Im giving now straight problem without examples. I am making game flappy bird in C++. And now I have class Bird. Bird will inherit from Object, and every other object in the game will. And object has methods setComponent and getComponent and has protected array of pointers to Component class. I have two classes out of Component (i.e. these classes inherit from Component): Collider and Texture. I want now to make method in Collider class that called checkAllCollisions.
And when collision happens, if one of the collided objects is Bird, that function should do something, lets say print: Bird colided. How could I make this?
 
I might be wrong here, but you should not check for collisions every single game tick, rather, when you move your bird to a new location check if it collided with anything on a new location
 
11:43 AM
wait a moment, writing an answer
class GameEntity
{
    virtual void collideWith(GameEntity& other) = 0;
    virtual ~GameEntity(){}
};

class Bird : public GameEntity
{
    void collideWith(GameEntity& other) override { /* bird collided */ }
};
 
well, you have a pretty clear question actually, you should look on how people do collision detection and get an idea on common approaches
 
@milleniumbug Sorry, I did not understood the code. How can Collider class benefit from this?
@login_not_failed I will try to google it with Collision detection, though my main point is not quite that but yeah.
 
My point is you're overengineering
don't design entire ECS from scratch if you just want to write flappy bird
 
Yeah, true xD
But I really am doing this learning purpose, so, I want to make a little engine as well.
I mean, I will make more little games
with that layout
 
well, for only a flappy bird, you don't really need lots of types of objects: an obstacle and a player
if you make flying enemies, they might be just obstacles internally
 
11:50 AM
Yeah, but, lets say I am not making flappy bird. As I said, I am planning to do some other games, like: SpaceShooter
Which you have enemies etc.
 
let your head feel the pain only when you finish with flappy bird, mkay? :)
you'll get better idea on what you would need for SpaceShooter later, when you'd have one finished game
 
But then, I would need to ask you again this same question later :D
May I try to write it again. Maybe I did not explain too well.
Because, I have feel, that I did not.
 
Given a value of type Component&, dynamic_cast<Bird*>(&component) will return a Bird* if that component is actually a bird, and return a null pointer otherwise
also see: visitor pattern for implementing double dispatch
 
@MuhamedCicak no it was clear what you want, what you did and you want to do in the future — I've been in the same boat, but with writing 3d engine from scratch, — a far bigger mess :D
 
@milleniumbug I just figured out it was the thing I was looking for, thanks! Googling it right now.
@login_not_failed I hope, in future, I will to that as well. I am actually going pretty far for my age :D
 
12:03 PM
look like good room than other C++ room
 
12:33 PM
Just wanted to let you know guys that I looked into example in 3D engine (which my dad made from scratch) and saw how the collisions are made, and now, I finally have idea how to make the logic of collision even if 3D collision is different from 2D collision (in mathematical way). And the particular thing about checking object type I was asking to you is made with visitor pattern (in that engine) as @milleniumbug mentioned. So I guess I will go with visitor pattern as a solution :)
 
1:01 PM
@Fanael Oh okay i'll try the Javascript lounge instead
I'm trying to write a function which accepts a 3-dimensional array of ints, so I declared the prototype as:

`void create(int***)`

But then I allocated the array on the stack as an `int[n][n][4]`, where `n` is only known at runtime. (This works under a language extension provided by g++.) However, trying to pass this variable into the function causes the error `Cannot convert int(*)[n][4] to int***`. I'm not sure I understand why this is even an error, let alone how to fix it
 
nwp

C++ Questions and Answers

Solve problems and approach solutions. Just ask and lurkers wi...
 
@nwp Thank you!
 
 
5 hours later…
6:35 PM
4 messages moved from Lounge<C++>
Reproducing a bug
test2
1 message moved to Sandbox
2
Q: Moving chat messages only takes into account messages selected before clicking "relocate"

milleniumbugHow to reproduce: Select room -> move messages Select a message Click "relocate" and type in the name of the room Select one more message Select the room by clicking "choose"

 

« first day (343 days earlier)      last day (2515 days later) »