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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - jonathansty

Pages: [1]
1
General / Question regarding the creation of objects.
« on: August 03, 2015, 03:16:05 pm »
Hello everyone,

Why do you use:
sf::Sprite sprite

and not
sf::Sprite* sprite

What's the different and why should you use one over the other?
I know the first variable is created on the stack and the second one is a pointer to an object on the heap?
Isn't the stack limited so won't it be a problem once you start using lots of variables?

2
What happens if you change the lambda to pass variables (here fn) by reference (changing [=] to [&])?

But besides that, anything stopping you from just defining another functor similar to the others?
Changing the lambda to pass by reference breaks it. It compiles and launches after a couple of actions it gives an error.

Nothing is stopping me from creating functors at the moment.
Thanks :)

3

So fixing your code might be as simple as moving the object creation into its own line and essentially caching the passed object for a bit:

Code: [Select]
{
    auto &shotbinding = std::bind(&Aircraft::fire, std::placeholders::_1);
    m_ActionBinding[Fire].action = derivedAction<Aircraft>(shotbinding);
}

I tried moving it right now to a different line seems like the actual binding it doesn't give a problem. Passing it to the template gives the problem. It's not fixed with changing it to a seperate line.

This is the code for the template
template <typename GameObject, typename Function>
Command::Action derivedAction(Function fn)
{
    return[=](SceneNode& node, sf::Time dt)
    {
        //Check if cast is safe
        assert(dynamic_cast<GameObject*>(&node) != nullptr);

        //Downcast node and invoke function on it;
        fn(static_cast<GameObject&>(node), dt);
    };
}

edit:
What I did for now was make a struct like this
struct FireMech
{
    FireMech()
    {

    }
    void operator() (Aircraft& aircraft, sf::Time dt) const
    {
        aircraft.fire();
    }
};
This works and the fire method executes and I can link it. Although I'm still hoping there is a solution for the std::bind problem.

4
General / SFML game dev book. Stuck at implementing the fire mechanic
« on: July 24, 2015, 11:38:13 am »
Hello everyone,

I'm currently working through the SFML game dev book but I stumbled upon a problem that I can't seem to fix.
This is the method that handles initializing the actions.
void Player::initializeActions()
{
    m_ActionBinding[MoveRight].action = derivedAction<Aircraft>(AircraftMover(1, 0.f));
    m_ActionBinding[MoveLeft].action = derivedAction<Aircraft>(AircraftMover(-1, 0.f));
    m_ActionBinding[MoveUp].action = derivedAction<Aircraft>(AircraftMover(0.f, -1));
    m_ActionBinding[MoveDown].action = derivedAction<Aircraft>(AircraftMover(0.f, 1));
    m_ActionBinding[Fire].action = derivedAction<Aircraft>(std::bind(&Aircraft::fire, std::placeholders::_1));
}
 
When I compile I get this error:
Error   1       error C3848: expression having type 'const std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Aircraft::* )(void),void,Aircraft,>,std::_Ph<1> &>' would lose some const-volatile qualifiers in order to call 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Aircraft::* )(void),void,Aircraft,>,std::_Ph<1> &>::operator ()<GameObject&,sf::Time&>(GameObject &,sf::Time &)'      e:\_gamedevelopment\sfml\sfml_flyinggame\command.h      24
 

this is the method I'm trying to bind:
void Aircraft::fire()
{
    if (Table[m_Type].fireInterval != sf::Time::Zero)
    {
        m_IsFiring = true;
    }
}

I'm using Visual studio 2013 ( and it's compiler).

Thanks in advance.

Pages: [1]