#Using threads for dashing timers

17 messages · Page 1 of 1 (latest)

normal flame
#

Hello, I'm currently trying to create a basic 2d platformer in C++. Recently, I was trying to implement a dash using multithreading, to have its own timers.
However, I'm unable to get multi threading working.

I have the dash and movement options located in my "Player" class. Below is the code that is throwing the error, the dash code, and the errors.
Error:

void Player::events(SDL_Event& e)
{
    // If a key was pressed
    if (e.type == SDL_KEYDOWN && e.key.repeat == 0)
    {
        if (canSDash){
            switch (e.key.keysym.sym)
            {
                case SDLK_w: sDashDirection = 1; break;
                case SDLK_s: sDashDirection = 2; break;
                case SDLK_a: sDashDirection = 3; break;
                case SDLK_d: sDashDirection = 4; break;
            }
            //this is line 28
            std::thread t1(&Player::sDash, sDashDirection);
            t1.join;
        }
    }
}

sDash Code:

void Player::sDash(int direction) {
    canSDash = false;
    isSDashing = true;
    gravity = 0.0f;
    switch (direction){
        //up 
        case 1: break;
        //down
        case 2: break;
        //left
        case 3: break;
        //right
        case 4: break;
    }
    Sleep(sDashingTime*1000);
    gravity = 0.1f;
    Sleep(sDashingCooldown*1000);
    isSDashing = false;
    canSDash = true;
}

Errors:
.../Scripts/Player.cpp:28:58: required from here

.../bits/std_thread.h:157:72: error: static assertion failed:
std::thread arguments must be invocable after conversion to rvalues
157 | typename decay<_Args>::type...>::value,

twin lichenBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

opal vessel
#

You basically never want threads for this. Threads are relatively heavyweight, you definitely don't want to have a thread per entity

#

Maybe a coroutine would work ok here, but those have a problem of being hard-to-impossible to serialize, so adding a networking to this later would be a pain

normal flame
#

Networking is not in the plans

opal vessel
#

The reason why it doesn't compile, though, is because you need to give an instance of the Player class to std::thread. So probably like this:

std::thread t1(&Player::sDash, this, sDashDirection);

And you need to make sure the player is not moved around in memory, so it can't be e.g. a vector element if you add/remove elements

normal flame
#

what does "this" stand for?

#

It does compile correctly now

opal vessel
#

In a method of a class, this is a pointer to the instance of the class the method is being called on

normal flame
#

I see

#

And in regards to threads, the reason I chose them is because they are most similair to unity's "yield" function

opal vessel
#

I didn't work with unity, butyield sounds like something a coroutine would have

#

Though using them in C++ without third-party libraries is a pain (you basically aren't expected to), so if you want them, you'll need to find some third-party library

normal flame
#

I will look into that. Thank you for the help

#

!solved

twin lichenBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity