Skip to content

Native Interoperability #63

Closed
Closed

Description

@Tiaansu

I am wondering if native interoperability is possible for this kind of native(s)?

native MyNative(id, output[], size = sizeof output, ...);
native MyNativeImpl(id, AmxString:output, size, ...) = MyNative;

stock String:MyNativeStr(Template:id, ...)
{    
    new String:output = str_new_buf(8192);
    MyNativeImpl(id, output, 8192, ...); // I know this is invalid
    return output;
}

Activity

  1. IS4Code commented on Aug 25, 2025

    @IS4Code
    Owner

    Yes, that is already possible, see the wiki. The tag is AmxStringBuffer: in that case. However note that since v1.4.2, both tags should behave more or less the same, so the code you should actually be valid.

  2. Tiaansu commented on Aug 26, 2025

    @Tiaansu
    Author

    Hello, an update to this. I just read this part:

    Unfortunately, printf cannot be modified in such a way, because it doesn't use the standard AMX API to access its parameters. For variadic functions (with ...), the conversion to AmxString is not done automatically and must be done manually.

    and it answered my question. I am asking about the variadic arguments.

  3. IS4Code commented on Aug 26, 2025

    @IS4Code
    Owner

    Then see the example below that paragraph, about CallLocalFunction. It works too, but you need to call str_buf_addr manually.

    Do you have any particular code or native function you are trying to adapt? I am not aware of any variadic functions that would use output buffers.

  4. Tiaansu commented on Sep 3, 2025

    @Tiaansu
    Author

    Hi, I don't think that using str_buf_addr will work on my case.

    As per native function, I want to use it on pawn-templates by Southclaws. (I made my own, but it's not on the github yet, uses the latest samp-rs, just linked Southclaws' plugin for example :))

  5. IS4Code commented on Sep 8, 2025

    @IS4Code
    Owner

    Why? Have you tried it?

  6. Tiaansu commented on Sep 9, 2025

    @Tiaansu
    Author

    Yes. It works like for this:

    native MyNative(id, output[], size = sizeof output, ...);
    native MyNativeImpl(id, AmxString:output, size, ...) = MyNative;

    but in my case (returning PawnPlus string) it won't work (or is there a way to do this?):

    native MyNative(id, output[], size = sizeof output, ...);
    native MyNativeImpl(id, AmxString:output, size, ...) = MyNative;
    
    stock String:MyNativeStr(Template:id, ...) // <-- this
    {    
        new String:output = str_new_buf(8192);
        MyNativeImpl(id, output, 8192, ...);
        return output;
    }
  7. IS4Code commented on Sep 9, 2025

    @IS4Code
    Owner

    That one should work in theory (just prefer AmxStringBuffer instead of AmxString). If not (please share how exactly it does not work), it might be an issue with pawn-templates. I know there is (was?) a deficiency in samp-rs that made returning these problematic.

  8. Tiaansu commented on Sep 10, 2025

    @Tiaansu
    Author

    Hi, the deficieny in samp-rs is now gone and this line of code below is working fine:

    #include <open.mp>
    #include <PawnPlus>
    #include <templates>
    
    main()
    {
        new Template:templateBanned = CreateTemplate(
            "Your account {{ name }} has been banned!\n\n\
            Reason: {{ reason }}\n\
            Duration: {{ days }} days\n\
            If you disagree, please file an appeal at: {{ forum }}");
    
        new String:dest = str_new_buf(1024);
        RenderTemplateStr(templateBanned, dest, 1024,
            PAIR_STR("name", "playerName"),
            PAIR_STR("reason", "Hello World"),
            PAIR_INT("days", 30),
            PAIR_STR("forum", "https://forum.website.com")
        );
        str_resize(dest, str_findc(dest, '\0'));
        print_s(dest);
    }

    what I mean is not working is this:

    native RenderTemplateImpl(Template:id, AmxStringBuffer:dest, len, ...) = RenderTemplate;
    stock String:RenderTemplateStr(Template:id, ...)
    {
        new String:dest = str_new_buf(1024);
        RenderTemplateImpl(id, dest, 1024, ...); // this will throw a compilation error
        str_resize(dest, str_findc(dest, '\0'));
    
        return dest;
    }

    Link to my pawn-templates repo: https://github.com/Tiaansu/pawn-templates

  9. IS4Code commented on Sep 10, 2025

    @IS4Code
    Owner

    Don't confuse me like that. 😄 I thought the ... was meant to be a placeholder. Of course you can't use it like that, that is a limitation of Pawn.

    There are a few options ‒ use #emit to call it manually or y_va for nicer syntax. PawnPlus also offers a solution ‒ list_get_args("+"), list_new_args in combination with a macro, and pawn_call_native to call the native with the l specifier. This however won't work if the argument is supposed to be a reference.

  10. Repository owner locked and limited conversation to collaborators on Sep 10, 2025
  11. converted this issue into a discussion #65 on Sep 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions