MAJigsaw77 / hxdiscord_rpc

Haxe/hxcpp @:native bindings for Discord RPC.
https://lib.haxe.org/p/hxdiscord_rpc
MIT License
21 stars 9 forks source link

HaxeFlixel + this library dont kinda work. #7

Closed AdamAlNajar closed 2 months ago

AdamAlNajar commented 2 months ago

hello, im currently writing some rpc code for discord using this library, and so far a few things work as i need but i get this problem whenever i load into the menustate its supposed to update the rpc, but it updates then shows the rpc ready message again. maybe im doing something wrong in flixel, but better to be safe than sorry Here are the 2 classes rpcmanager and the state

rpc code : https://pastebin.com/RSGvJnWM flixel code : https://pastebin.com/cnYC6gkx

AdamAlNajar commented 2 months ago

my code could be dogshit just :rofl:

MAJigsaw77 commented 2 months ago

Maybe you should use this implementation instead

package backend;

#if DISCORD
import flixel.FlxG;
import hxdiscord_rpc.Discord as RichPresence;
import hxdiscord_rpc.Types;
import openfl.Lib;
import sys.thread.Thread;

class Discord
{
    public static var initialized(default, null):Bool = false;

    public static function load():Void
    {
        if (initialized)
            return;

        var handlers:DiscordEventHandlers = DiscordEventHandlers.create();
        handlers.ready = cpp.Function.fromStaticFunction(onReady);
        handlers.disconnected = cpp.Function.fromStaticFunction(onDisconnected);
        handlers.errored = cpp.Function.fromStaticFunction(onError);
        RichPresence.Initialize("1140307809167220836", cpp.RawPointer.addressOf(handlers), 1, null);

        // Daemon Thread
        Thread.create(function():Void
        {
            while (true)
            {
                #if DISCORD_DISABLE_IO_THREAD
                RichPresence.UpdateConnection();
                #end
                RichPresence.RunCallbacks();

                // Wait 2 seconds until the next loop...
                Sys.sleep(2);
            }
        });

        Lib.application.onExit.add((exitCode:Int) -> RichPresence.Shutdown());

        initialized = true;
    }

    public static function changePresence(details:String, ?state:String):Void
    {
        var discordPresence:DiscordRichPresence = DiscordRichPresence.create();
        discordPresence.details = details;

        if (state != null)
            discordPresence.state = state;

        discordPresence.largeImageKey = "icon";
        discordPresence.largeImageText = cast(Lib.application.meta['title'], String);
        RichPresence.UpdatePresence(cpp.RawConstPointer.addressOf(discordPresence));
    }

    private static function onReady(request:cpp.RawConstPointer<DiscordUser>):Void
    {
        if (Std.parseInt(cast(request[0].discriminator, String)) != 0)
            FlxG.log.notice('(Discord) Connected to User "${cast (request[0].username, String)}#${cast (request[0].discriminator, String)}"');
        else
            FlxG.log.notice('(Discord) Connected to User "${cast (request[0].username, String)}"');

        Discord.changePresence('Just Started');
    }

    private static function onDisconnected(errorCode:Int, message:cpp.ConstCharStar):Void
    {
        FlxG.log.notice('(Discord) Disconnected ($errorCode: ${cast (message, String)})');
    }

    private static function onError(errorCode:Int, message:cpp.ConstCharStar):Void
    {
        FlxG.log.notice('(Discord) Error ($errorCode: ${cast (message, String)})');
    }
}
#end
MAJigsaw77 commented 2 months ago

I used it in a project

AdamAlNajar commented 2 months ago

im gonna test it and tell u how it works out

MAJigsaw77 commented 2 months ago

im gonna test it and tell u how it works out

I suggest you to also add <haxedef name="DISCORD_DISABLE_IO_THREAD" /> for better performance.

AdamAlNajar commented 2 months ago

also, why is it wrapped in an if?

AdamAlNajar commented 2 months ago

is it something that i should add to my project.xml?

MAJigsaw77 commented 2 months ago

is it something that i should add to my project.xml?

Yes

AdamAlNajar commented 2 months ago

what also do u mean by initalized? what should i do with it? should i add anything in the entrypoint?

MAJigsaw77 commented 2 months ago

No no

AdamAlNajar commented 2 months ago

there is a problem in this line :

if DISCORD_DISABLE_IO_THREAD

            RichPresence.UpdateConnection();
            #end

i defined it in my xml

MAJigsaw77 commented 2 months ago

Remove the export and compile again

AdamAlNajar commented 2 months ago

yep everything works! thx!

MAJigsaw77 commented 2 months ago

yep everything works! thx!

Np