Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem with constructors - game development  (Read 12387 times)

0 Members and 2 Guests are viewing this topic.

Hengad

  • Newbie
  • *
  • Posts: 19
    • View Profile
Problem with constructors - game development
« on: September 05, 2015, 09:25:38 pm »
I started making platformer game, and I ran into a problem. It is not a big deal, but if I continue developing this game, it will probably become big problem in future (I guess).

I have struggled with this for 3 hours, without success, so I don't really feel like I want to make this code much shorter, I cut out some parts to make it simpler. It is not too long, but I have stuff in separate files so I put source to pastebin.

http://pastebin.com/2QrfRJ1b

More details about the problem:
It compiles all fine, and it draws the tiles exactly I want them to be drawn. I also have messages coming to console about stuff. And when I run the program console looks like this:


It says twice that WindowManager constructor was called and also window options (that windowproperties class i cut out because i didn't see it's necessary, ignore it). Reason behind this is that I call it once in core class (because i need window for game loop) and once in world class (because i need window dimensions to make calculations and stuff for the world). But I don't want that constructor is called twice... I tried to do things things that prevent it from calling 2 times, it succeeded, but then it didn't draw anything to screen? If I continue developing and this twice calling will happen more, it uses a lot of compile time in the end.

So does anyone know what I should do? The main point is that I need screenDimensions from WindowManager, but if I create instance of it, WindowManager constructor will be called twice. If you read all this, you don't even know how much I appreciate it, thank you.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem with constructors - game development
« Reply #1 on: September 05, 2015, 10:25:50 pm »
Is it possible that you've passed the window manager by value instead of by reference or pointer?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

SpeCter

  • Full Member
  • ***
  • Posts: 151
    • View Profile
Re: Problem with constructors - game development
« Reply #2 on: September 05, 2015, 11:14:08 pm »
You have two instances of WindowManager in your program and wonder why you have two constructor calls?
Just pass the WindowManager from Core to your world by reference/pointer.

Hengad

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Problem with constructors - game development
« Reply #3 on: September 06, 2015, 11:45:18 am »
Is it possible that you've passed the window manager by value instead of by reference or pointer?

Well yes, if you read the code, you see I passed it by value. But I don't know how to pass it by reference. I only know basics of pointers and passing function parameters by reference. I tried to research for it, but I just got errors, could you tell me how do I pass object by reference? (If I understood right, that's what I have to do)

You have two instances of WindowManager in your program and wonder why you have two constructor calls?
Just pass the WindowManager from Core to your world by reference/pointer.

Well I don't "wonder" why. I know why, but I don't know how to prevent this. Check my answer to Hapax.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Problem with constructors - game development
« Reply #4 on: September 06, 2015, 11:56:53 am »
These are very basic C++ questions that are not related to SFML. If you use a library like SFML, a certain knowledge of the programming language is really required... Otherwise you'll constantly get stuck and waste much more time than learning things correctly in the first place (not just bla bla, I've done the same mistake initially).

I suggest you grab a good C++ book and read it, it will answer your current and future questions :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

GraphicsWhale

  • Full Member
  • ***
  • Posts: 131
    • View Profile
Re: Problem with constructors - game development
« Reply #5 on: September 06, 2015, 01:11:35 pm »
Well yes, if you read the code, you see I passed it by value. But I don't know how to pass it by reference. I only know basics of pointers and passing function parameters by reference. I tried to research for it, but I just got errors, could you tell me how do I pass object by reference? (If I understood right, that's what I have to do)

I'm confused. Do you know how to pass-by-reference or not? Even if you only know how to use pointers, I'm not sure why you didn't use them.

http://www.cprogramming.com/tutorial/references.html

Also what Nexus said.

Well I don't "wonder" why. I know why, but I don't know how to prevent this. Check my answer to Hapax.

You seem to know what pass-by-reference is (even if you don't know how to use it), so why are you asking how to prevent it?

It's kinda like wanting to hammer a nail into wood and not knowing how to use the hammer, and rather than learning how to use it you simply ask How do you get the nail into the wood? only to get a Use the hammer! response. All you're doing is delaying how long it will be until you stick a piece of pointy refined metal into the beautifully stained chunk of a tree's corpse.

Bad metaphors aside:

void setToFive(int& x)
{
    x = 5;
}
 

int x = 2;
setToFive(x);
assert(x == 5);
 

Using references inside of a class requires the use of an initializer list because references cannot be uninitialized:

class ClassWithReference
{
    int& x;
public:
    ClassWithReference(int& x_param) : x(x_param) { }
    int& getX() { return x; }
}
 

Just make sure the class that holds the reference dies before the object it's referring to does.

But please do get a decent C++ book.

PS: Your use of "cutted" (from your source code) makes me uncomfortable

Hengad

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Problem with constructors - game development
« Reply #6 on: September 06, 2015, 01:49:10 pm »
These are very basic C++ questions that are not related to SFML. If you use a library like SFML, a certain knowledge of the programming language is really required... Otherwise you'll constantly get stuck and waste much more time than learning things correctly in the first place (not just bla bla, I've done the same mistake initially).

I suggest you grab a good C++ book and read it, it will answer your current and future questions :)

I wouldn't say "very basic", I've heard that even experienced programmers sometimes have problems with pointers. But anyways, I have ordered book named "C++ Primer, I guess that is good enough.

I'm confused. Do you know how to pass-by-reference or not? Even if you only know how to use pointers, I'm not sure why you didn't use them.

As I said I have learned only basics; creating a pointer and making it point to some variable. I am not comfortable using them yet.

Using references inside of a class requires the use of an initializer list because references cannot be uninitialized:

Quote
class ClassWithReference
{
    int& x;
public:
    ClassWithReference(int& x_param) : x(x_param) { }
    int& getX() { return x; }
}
 


I don't understand how this helps me in this situation :( I am not passing anything to function, I just want an instance of WindowManager without calling its constructor.

PS: Your use of "cutted" (from your source code) makes me uncomfortable

Sorry, english isn't my native language, and I didn't remember that cut is irregular verb. :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Problem with constructors - game development
« Reply #7 on: September 06, 2015, 01:51:15 pm »
I wouldn't say "very basic", I've heard that even experienced programmers sometimes have problems with pointers.
Pointers are still language basics. The reason why "experienced" programmers don't know how to use them correctly is that they've skipped some chapters, thinking they wouldn't be important ;)

I just want an instance of WindowManager without calling its constructor.
That's not possible. The whole point of constructors is to enforce initialization.
« Last Edit: September 06, 2015, 01:55:40 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Satus

  • Guest
Re: Problem with constructors - game development
« Reply #8 on: September 06, 2015, 02:17:49 pm »
I wouldn't say "very basic", I've heard that even experienced programmers sometimes have problems with pointers.

But they don't have problems with syntax, only with complicated memory management.

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Problem with constructors - game development
« Reply #9 on: September 06, 2015, 02:27:07 pm »
That's why they use smart pointers rather than raw pointers. Also, as far as I can tell, you want the same WindowManager to be in Core and World, is that right? If it is then both Core and World should have a
Code: [Select]
WindowManager& (which works almost exactly like a normal WindowManager, none of the whole dereferencing stuff).

Hengad

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Problem with constructors - game development
« Reply #10 on: September 06, 2015, 02:30:45 pm »
Pointers are still language basics. The reason why "experienced" programmers don't know how to use them correctly is that they've skipped some chapters, thinking they wouldn't be important ;)

Probably, + what Satus said.
Quote
But they don't have problems with syntax, only with complicated memory management.

I just want an instance of WindowManager without calling its constructor.
That's not possible. The whole point of constructors is to enforce initialization.

I know that, but there must be something that allows me to use screenDimensions from WindowManager class in World class without calling constructor? I just don't know what.

PS. Many experienced programmers have said that it's better to write bad but working code, instead of writing nothing, so I thought I should even try to make something. :)

That's why they use smart pointers rather than raw pointers.

Some people say that smart pointers shouldn't be used at all, so I haven't even learned about them yet.

Also, as far as I can tell, you want the same WindowManager to be in Core and World, is that right? If it is then both Core and World should have a
Code: [Select]
WindowManager& (which works almost exactly like a normal WindowManager, none of the whole dereferencing stuff).

Thank you, I will try that.

EDIT: Nevermind, I'll just wait for the book, got error:
Quote
In constructor 'Core::Core()':
error: uninitialized reference member 'Core::windowManager' [-fpermissive]
« Last Edit: September 06, 2015, 02:40:25 pm by Hengad »

Satus

  • Guest
Re: Problem with constructors - game development
« Reply #11 on: September 06, 2015, 02:48:30 pm »
Some people say that smart pointers shouldn't be used at all, so I haven't even learned about them yet.

I wonder who are this "some people" you are talking about.

Quote
In constructor 'Core::Core()':
error: uninitialized reference member 'Core::windowManager' [-fpermissive]

Reference should be initialized in constructor:
class MyClass {
    OtherClass& m_oc;
    MyClass(OtherClass& oc) : m_oc(oc) {}
};

GraphicsWhale

  • Full Member
  • ***
  • Posts: 131
    • View Profile
Re: Problem with constructors - game development
« Reply #12 on: September 06, 2015, 03:08:14 pm »
I wouldn't say "very basic", I've heard that even experienced programmers sometimes have problems with pointers. But anyways, I have ordered book named "C++ Primer, I guess that is good enough.

The problem is less due to pointers themselves and more due to problems relating to manual memory management (which utilizes pointers).

One you learn how to properly use pointers (and really, it's not that hard, maybe 30 minutes tops for some basic usage), it wouldn't hurt to learn about a couple common data structures, like dynamic arrays, linked lists, ring buffers, etc.

In C, you'd have to implement everything yourself. No RAII, no templates, every single data structure has to be manually managed with every operation. And this isn't a simple "I know this pointer points to x", this dealing with pointers that point to other pointers, pointers to point to memory held my other pointers, pointers that point to memory held by other pointers held with pointers. And no memory leaks or segmentation faults are acceptable.

In C++, there's templates so the C++ standard library can abstract away from some common data structures for you (vector, list, and queue, for example, all use pointers internally to manage their data). There's also smart pointers, which can be helpful. So you're not going to have to deal with all that chaos, at least not if you don't want to (and you might, say, if you want a custom allocater, or something).

This chaos is why people use manged languages, by the way. :) Not that C++ is bad, it's that is can get a bit hairy sometimes.

I don't understand how this helps me in this situation :( I am not passing anything to function, I just want an instance of WindowManager without calling its constructor.

You can't. That's how object creation works. It gets constructed when it's created, and destructed when it's destroyed. It's basically a contract to the object that says "You get to be able to have your initialization code run once when it's created, allowing any set of arguments you want, and your deinitialization code run once when you get destroyed, but you cannot get any arguments". It's there so that you can be 100% sure every object is as valid as it needs to be; you can't have an uninitialized object. That and every object can free every resource it has implicitly.

If you don't want it to be called twice, you create one object. You can pass a reference to this object as long as the object you're referencing doesn't go out of scope before the object holding the reference does (otherwise the object gets deleted before it's finished being used and you'll probably crash your program).
« Last Edit: September 06, 2015, 03:10:02 pm by GraphicsWhale »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Problem with constructors - game development
« Reply #13 on: September 06, 2015, 03:10:38 pm »
Quote from: Hengad
I've heard that even experienced programmers sometimes have problems with pointers.
[...]
PS. Many experienced programmers have said that it's better to write bad but working code, instead of writing nothing
[...]
Some people say that smart pointers shouldn't be used at all
Do you have concrete sources for those claims? What makes you consider those people experienced? You shouldn't just believe what you hear somewhere, but question what you're told. And in order to meaningfully judge whether such statements make sense or not, you need some knowledge about the language itself, so I'd probably just read the book :)

That's why they use smart pointers rather than raw pointers.
Smart pointers only manage memory and ownership. Raw pointers are still used for passive indirections, pointer arithmetic, and so on.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

GraphicsWhale

  • Full Member
  • ***
  • Posts: 131
    • View Profile
Re: Problem with constructors - game development
« Reply #14 on: September 06, 2015, 03:16:49 pm »
I know that, but there must be something that allows me to use screenDimensions from WindowManager class in World class without calling constructor? I just don't know what.

Pass a reference or pointer.

Some people say that smart pointers shouldn't be used at all, so I haven't even learned about them yet.

Some of the most well-known people in the C++ community, even the guy who made it, all seem to love smart pointers.

std::unique_ptr uses RAII to ensure dynamically-allocated objects gets deleted implicitly as soon as they go out of scope. No performance cost.

std::shared_ptr lets you pass around the smart pointer as a sort of "handle", so the object stays alive until the last handle gets destroyed. Acts much like objects in Java/C# do. Pretty neat if you ask me. It comes at a slight cost, and I wouldn't use unless I absolutely need it, but the cost of doing the same thing (reference counting) manually isn't any better. It's not an issue with the smart pointer, it's an issue in the nature of what it does. It's kinda like calling C++ slow if it wont calculate the meaning of life fast enough for you (even though such question wouldn't be able to be answered any faster with raw pointers). :)
« Last Edit: September 06, 2015, 03:20:52 pm by GraphicsWhale »