Log in to reply
 

Wait() alternatives?



  • I have a script that detects speed difference based on subtraction of vehicle's speed with it's speed after 10ms:

    collisionSpeed = currentVehicle.Speed;
       Wait(10);
    afterSpeed = currentVehicle.Speed;
                            speedDif = collisionSpeed - afterSpeed;
    

    Is there a better way of doing this, without pausing the whole script?



  • you can use "DateTime" or the "GameTime" itself.



  • @RusLanParty something like this:

    using GTA;
    using GTA.UI;
    
    namespace Accurate_throttle_for_aircraft
    {
        internal class Class1 : Script
        {
            private float _currentGameTime;
    
            private float _oldGameTime;
    
            public Class1()
            {
                _currentGameTime
                    = Game
                        .GameTime;
    
                _oldGameTime
                    = Game
                        .GameTime;
    
                Tick += (o, e) =>
                {
                    if (ReturnsTrueForEach(2000))
                    {
                        Notification
                            .Show($"{_currentGameTime}");
                    }
                };
            }
    
            private bool ReturnsTrueForEach(int milliseconds)
            {
                _currentGameTime
                    = Game
                        .GameTime;
    
                if (_currentGameTime > _oldGameTime + milliseconds)
                {
                    _oldGameTime
                        = _currentGameTime;
    
                    return true;
                }
    
                return false;
            }
        }
    }
    
    


  • @RusLanParty said in Wait() alternatives?:

    without pausing the whole script?

    Create multiple "threads" or coroutines; if that section/chunk/block of code is meant to run independently of whatever else you may have, separate it into it's own.

    Lua example:

    local GetEntitySpeed = GetEntitySpeed; local Vehicle = Info.Player.Vehicle; local Wait = JM36.Wait
    local SpeedDifference = 0.0
    
    JM36.CreateThread(function()
        while true do
            local CollisionSpeed = GetEntitySpeed(Vehicle.Handle)
            Wait(10)
            local AfterSpeed = GetEntitySpeed(Vehicle.Handle)
            SpeedDifference = CollisionSpeed - AfterSpeed
            Wait() -- Wait and start over/again next frame (or X ms)
        end
    end)
    
    JM36.CreateThread(function()
        while true do
            -- Some independently executing code and stuff here, probably utilizing the value in the SpeedDifference var
            Wait() -- Wait and start over/again next frame (or X ms)
        end
    end)
    

    Edit: Trying to shorten/compress/compact the above to avoid the scrollbar while trying to maintain readability at the same time, which is difficult.

    @Niziul said in Wait() alternatives?:

    you can use "DateTime" or the "GameTime" itself.

    Of course you may also do this as well using if statements and comparisons, but meh it's not as nice as far as code tbh (but is valid and does work perfectly fine)



  • @JayMontana36 said in Wait() alternatives?:

    Lua example:

    local GetEntitySpeed = GetEntitySpeed; local Vehicle = Info.Player.Vehicle; local Wait = JM36.Wait
    local SpeedDifference = 0.0
    
    JM36.CreateThread(function()
        while true do
            local CollisionSpeed = GetEntitySpeed(Vehicle.Handle)
            Wait(10)
            local AfterSpeed = GetEntitySpeed(Vehicle.Handle)
            SpeedDifference = CollisionSpeed - AfterSpeed
            Wait() -- Wait and start over/again next frame (or X ms)
        end
    end)
    
    JM36.CreateThread(function()
        while true do
            -- Some independently executing code and stuff here, probably utilizing the value in the SpeedDifference var
            Wait() -- Wait and start over/again next frame (or X ms)
        end
    end)
    

    I don't know anything about the lua language, I'm confused.

    using GTA;
    
    namespace Accurate_throttle_for_aircraft
    {
        internal class Class1 : Script
        {
            public Class1()
            {
                Tick += (o, e) =>
                {
    
                };
            }
        }
        internal class Class2 : Script
        {
            public Class2()
            {
                Tick += (o, e) =>
                {
    
                };
            }
        }
    }
    
    

    Is multiple threads something like that in C#? Can you tell me?



  • @Niziul said in Wait() alternatives?:

    Is multiple threads something like that in C#? Can you tell me?

    I have no idea, I know pretty much nothing about C# (or really any others) besides somewhat reading and very basic/minimalist comprehension of it; I can only really say to look into possibly creating multiple tick handlers or multiple on tick stuff, if not into coroutines (likely with your own management/execution system); beyond that, before coroutines or threads, I did the "if statement and time comparison" method, though that was such a huge/major pain for larger scripts of mine that did multiple things (and needed to have several if/comparison shoved in all over the place which was/is messy in comparison to now)


Log in to reply
 

Looks like your connection to GTA5-Mods.com Forums was lost, please wait while we try to reconnect.