Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callnative functions take ScriptContext argument #3294

Merged
merged 1 commit into from
Sep 11, 2023

Conversation

ghoulslash
Copy link
Collaborator

@ghoulslash ghoulslash commented Sep 11, 2023

Adds a struct ScriptContext argument to NativeFunc to allow more convenient scripting with callnative. We can make macros similar to battle scripts with .byte fields afterwards and use those in our callnative function

e.g.

.macro gettilebehaviorat x:req, y:req
    callnative NativeFunc_gettilebehaviorat
    .2byte \x
    .2byte \y
 .endm

void NativeFunc_gettilebehaviorat(struct ScriptContext *ctx)
{
    u16 x    = VarGet(ScriptReadHalfword(ctx));
    u16 y    = VarGet(ScriptReadHalfword(ctx));

    gSpecialVar_Result = MapGridGetMetatileBehaviorAt(x + MAP_OFFSET, y + MAP_OFFSET);
}

Copy link
Collaborator

@mrgriffin mrgriffin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this.

In case anybody is wondering "will this break people's existing callnatives?" the answer is no: callnative is used from asm, so there's no type checking of the functions (i.e. there's no compile error for callnative-ing a function that doesn't have a ctx parameter)... and that's fine because passing an extra argument (for arguments 1–4) doesn't have any effect on the stack, so existing functions can (and will) just ignore the extra argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants