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

Author Topic: SOLVED - [Thor] - Strange Error works then doesn't work no code change?  (Read 4666 times)

0 Members and 1 Guest are viewing this topic.

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
I am getting a crash in my game and the console is getting this message printed in it..


Code: [Select]
Assertion failed: progress >= 0.f && progress <= 1.f, file h:\nom!\_xternlibs\sfml2\include\thor\animation\frameanimation.hpp, line 115
The strange thing is the crash only happens sometimes. If I compile, it will run, and then I run it a few times and suddenly it will crash with this error, with no code change at all. If I compile a release version, same thing happens, I can just sit there running the exe, and 1/2 the time it works.. the other 1/2 it crashes.

Anyideas?

Here is my code if it helps.

#include "goPac.h"

goPac::goPac()
        : _nextDirection(STOPPED)
        , _velocity(10.0f)
        , _maxVelocity(100.0f)
{
        _PACsprite.setTexture(*TextureManager::getInstance().rSpriteSheet());


        thor::FrameAnimation defaultAnim;
        defaultAnim.addFrame(1.f, sf::IntRect(40, 80, 40, 40));

        // Create animation: CHOMP
        thor::FrameAnimation chomp;
        chomp.addFrame(1.f, sf::IntRect(120, 80, 40, 40));
        chomp.addFrame(1.5f, sf::IntRect(160, 80, 40, 40));
        chomp.addFrame(1.f, sf::IntRect(120, 80, 40, 40));
        chomp.addFrame(2.f, sf::IntRect(40, 80, 40, 40));

        animPac.setDefaultAnimation(defaultAnim, sf::seconds(1.f));
        animPac.addAnimation("chomp", chomp, sf::seconds(0.4f));
}

goPac::~goPac()
{
}

void goPac::Update(sf::RenderWindow& rw, sf::Event& _event, float TimeElapsed)
{
        rw.pollEvent(_event);
        rw.setKeyRepeatEnabled(false);


        if (_event.type == sf::Event::KeyPressed)
        {
                if (_event.key.code == sf::Keyboard::Up || _event.key.code == sf::Keyboard::W || _event.key.code == sf::Keyboard::Num8)
                {
                        debug("UP");
                        playAnimation();

                }

                if (_event.key.code == sf::Keyboard::Left || _event.key.code == sf::Keyboard::A || _event.key.code == sf::Keyboard::Num4)
                {
                        debug("LEFT");
                }

                if (_event.key.code == sf::Keyboard::Right || _event.key.code == sf::Keyboard::D || _event.key.code == sf::Keyboard::Num6)
                {
                        debug("RIGHT");
                }

                if (_event.key.code == sf::Keyboard::Down || _event.key.code == sf::Keyboard::S || _event.key.code == sf::Keyboard::Num2)
                {
                        debug("DOWN");
                }

        }

        animPac.update(animClock.restart());
        animPac.animate(_PACsprite);
}

void goPac::playAnimation()
{
        debug("playAnimation();");
        animPac.update(animClock.restart());
        animPac.playAnimation("chomp", true);
}

void goPac::Draw(sf::RenderWindow& rw)
{
        rw.draw(_PACsprite);
}

« Last Edit: July 18, 2012, 09:39:26 pm by aNewHobby »
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Re: [THOR] - Strange Error works then doesn't work no code change?
« Reply #1 on: July 16, 2012, 08:59:27 am »
It seams to stop erroring (though the animation will not play) if I remove

animPac.update(animClock.restart());

I have tried setting addFrame so everything is 1.f and add Animation is 1.f
any ideas?

I think it has somthing to do with "pollEvent"?
« Last Edit: July 16, 2012, 09:38:28 am by aNewHobby »
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10829
    • View Profile
    • development blog
    • Email
Re: [THOR] - Strange Error works then doesn't work no code change?
« Reply #2 on: July 16, 2012, 10:05:40 am »
It's interesting that it runs so often. :-D
You're declaring the animations in the constructor, this means as soon as you leave the constructor the animation pointer will get invalid. Funny enough it seems like the data in memory is still kept there sometimes, thus the pointer can access it.
Make your animation ptr a member variable and you should be good. ;-)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Re: [THOR] - Strange Error works then doesn't work no code change?
« Reply #3 on: July 16, 2012, 10:34:09 am »
It's interesting that it runs so often. :-D
You're declaring the animations in the constructor, this means as soon as you leave the constructor the animation pointer will get invalid. Funny enough it seems like the data in memory is still kept there sometimes, thus the pointer can access it.
Make your animation ptr a member variable and you should be good. ;-)

Sorry I am very new to programming... Are you saying that I should make a member function something like InitilizeAnimation(); and place that in the constructor.. then put all the animation initialisation code for the animation in hat function and then call the function from the constructor?

This is the header as it is for the header...

#ifndef GOPAC_H
#define GOPAC_H

#include "Debug.h"
#include "SFML/Graphics.hpp"
#include "Thor/Animation.hpp"
#include "GameObject.h"

class goPac : public GameObject
{
public:
        enum {STOPPED, LEFT, RIGHT, UP, DOWN};

        goPac();
   ~goPac();

   void Update(sf::RenderWindow&, sf::Event&, float);
   void Draw (sf::RenderWindow&);
private:
        float _velocity;
        float _maxVelocity;
        float _startofTimer;

        int _nextDirection;

        // Sprite Animation
        sf::Sprite _PACsprite;
        sf::Clock animClock;
        thor::FrameAnimation defaultAnim;
        thor::FrameAnimation chomp;
        thor::Animator<sf::Sprite, std::string> animPac;
        void playAnimation();
};

#endif // - GOPAC_H


Quote
Make your animation ptr a member variable and you should be good. ;-)
This is the question I have... I am not declaring any pointers in the code I posted...

edit

oh you mean
thor::FrameAnimation defaultAnim;
?? I'll try this

edit2

I think you mean this? All I did was comment out the marked lines,

goPac::goPac()
        : _nextDirection(STOPPED)
        , _velocity(10.0f)
        , _maxVelocity(100.0f)
{
        _PACsprite.setTexture(*TextureManager::getInstance().rSpriteSheet());


        //thor::FrameAnimation defaultAnim;  // This is in the .h file WTF am I doing?
        defaultAnim.addFrame(1.f, sf::IntRect(40, 80, 40, 40));

        // Create animation: CHOMP
        //thor::FrameAnimation chomp; // This is in the .h file WTF am I doing?
        chomp.addFrame(1.f, sf::IntRect(120, 80, 40, 40));
        chomp.addFrame(1.5f, sf::IntRect(160, 80, 40, 40));
        chomp.addFrame(1.f, sf::IntRect(120, 80, 40, 40));
        chomp.addFrame(2.f, sf::IntRect(40, 80, 40, 40));

        animPac.setDefaultAnimation(defaultAnim, sf::seconds(1.f));
        animPac.addAnimation("chomp", chomp, sf::seconds(0.4f));
}
« Last Edit: July 16, 2012, 10:45:12 am by aNewHobby »
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Re: [THOR] - Strange Error works then doesn't work no code change?
« Reply #4 on: July 16, 2012, 10:54:30 am »
Hmm, tried those things.. unless you meant something else and still having problems... it could be my imaginations but it seams to be loading more often but I'm still getting that assertion error.

CPP
#include "goPac.h"

goPac::goPac()
        : _nextDirection(STOPPED)
        , _velocity(10.0f)
        , _maxVelocity(100.0f)
{
        InitiliseAnimation();
}

goPac::~goPac()
{
}

void goPac::Update(sf::RenderWindow& rw, sf::Event& _event, float TimeElapsed)
{
        rw.pollEvent(_event);
        rw.setKeyRepeatEnabled(false);


        if (_event.type == sf::Event::KeyPressed)
        {
                if (_event.key.code == sf::Keyboard::Up || _event.key.code == sf::Keyboard::W || _event.key.code == sf::Keyboard::Num8)
                {
                        debug("UP");
                        playAnimation();

                }

                if (_event.key.code == sf::Keyboard::Left || _event.key.code == sf::Keyboard::A || _event.key.code == sf::Keyboard::Num4)
                {
                        debug("LEFT");
                }

                if (_event.key.code == sf::Keyboard::Right || _event.key.code == sf::Keyboard::D || _event.key.code == sf::Keyboard::Num6)
                {
                        debug("RIGHT");
                }

                if (_event.key.code == sf::Keyboard::Down || _event.key.code == sf::Keyboard::S || _event.key.code == sf::Keyboard::Num2)
                {
                        debug("DOWN");
                }

        }

        animPac.update(animClock.restart());
        animPac.animate(_PACsprite);
}

void goPac::playAnimation()
{
        debug("playAnimation();");
        //animPac.update(animClock.restart());
        animPac.playAnimation("chomp", true);
}

void goPac::Draw(sf::RenderWindow& rw)
{
        rw.draw(_PACsprite);
}

void goPac::InitiliseAnimation()
{

        _PACsprite.setTexture(*TextureManager::getInstance().rSpriteSheet());

        defaultAnim.addFrame(1.f, sf::IntRect(40, 80, 40, 40));
        // Create animation: CHOMP
        chomp.addFrame(1.f, sf::IntRect(120, 80, 40, 40));
        chomp.addFrame(1.5f, sf::IntRect(160, 80, 40, 40));
        chomp.addFrame(1.f, sf::IntRect(120, 80, 40, 40));
        chomp.addFrame(2.f, sf::IntRect(40, 80, 40, 40));

        animPac.setDefaultAnimation(defaultAnim, sf::seconds(1.f));
        animPac.addAnimation("chomp", chomp, sf::seconds(0.4f));

}

.H
#ifndef GOPAC_H
#define GOPAC_H

#include "Debug.h"
#include "SFML/Graphics.hpp"
#include "Thor/Animation.hpp"
#include "GameObject.h"

class goPac : public GameObject
{
public:
        enum {STOPPED, LEFT, RIGHT, UP, DOWN};

        goPac();
   ~goPac();

   void Update(sf::RenderWindow&, sf::Event&, float);
   void Draw (sf::RenderWindow&);
private:
        float _velocity;
        float _maxVelocity;
        float _startofTimer;

        int _nextDirection;

        // Sprite Animation
        sf::Sprite _PACsprite;
        sf::Clock animClock;
        thor::FrameAnimation defaultAnim;
        thor::FrameAnimation chomp;
        thor::Animator<sf::Sprite, std::string> animPac;
        void InitiliseAnimation();
        void playAnimation();
};

#endif // - GOPAC_H
« Last Edit: July 16, 2012, 11:02:33 am by aNewHobby »
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Re: [THOR] - Strange Error works then doesn't work no code change?
« Reply #5 on: July 16, 2012, 11:40:53 am »
hmm... seams I just complete remove the default animation and then no problems... ..  .
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: [THOR] - Strange Error works then doesn't work no code change?
« Reply #6 on: July 16, 2012, 01:39:03 pm »
You're declaring the animations in the constructor, this means as soon as you leave the constructor the animation pointer will get invalid.
This is okay, since animations are functors which are copied to the animator. There are no pointers.

Anyideas?
You know that playAnimation() restarts the animation all the time? You shouldn't call it together with update().

Otherwise, please provide a minimal example reproducing the problem...

hmm... seams I just complete remove the default animation and then no problems... ..  .
Although your problem is probably not related to it, I have already planned to remove the special case of a "default animation"... I mean, the same functionality can achieved by an own animation, and thus stopAnimation() really stops the animator.

By the way, it's Thor and not THOR ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Re: [THOR] - Strange Error works then doesn't work no code change?
« Reply #7 on: July 17, 2012, 01:54:20 pm »
hmm... seams I just complete remove the default animation and then no problems... ..  .
Although your problem is probably not related to it, I have already planned to remove the special case of a "default animation"... I mean, the same functionality can achieved by an own animation, and thus stopAnimation() really stops the animator.

What dose relate to it then.. what was the problem?
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: [THOR] - Strange Error works then doesn't work no code change?
« Reply #8 on: July 17, 2012, 11:05:42 pm »
I don't know. If you want to find it out, please provide a minimal and complete example that I can directly test to reconstruct the problem.

But if it doesn't appear without using default animations, I guess it's not worth to invest time on it, as I'm going to remove those.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: