• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

[Script✓] in GBA FIreRed script, is it possible to "dereference" a variable when passing as a parameter to a function?

3
Posts
1
Years
    • Seen Mar 21, 2023
    been using the pret disassembly of firered to make some changes to the game.
    ive got to a point where i write a value into a temp variable and attempting to pass that variable to a function that is expecting a literal value.

    for example, setwildbattle is normally used like this
    setwildbattle SPECIES_SNORLAX, 30
    but if you did something like this
    Code:
    setvar VAR_TEMP_0, SPECIES_SNORLAX
    setwildbattle VAR_TEMP_0, 30
    and since setwildbattle is defined like this in c
    Code:
    bool8 ScrCmd_setwildbattle(struct ScriptContext * ctx)
    {
        u16 species = ScriptReadHalfword(ctx);
        u8 level = ScriptReadByte(ctx);
        u16 item = ScriptReadHalfword(ctx);
    
        CreateScriptedWildMon(species, level, item);
        return FALSE;
    }
    it will try to use the variable index as the species id.

    my question, is there any way to pass the value stored in a variable to a function from script that would not require writing a new function in c that calls VarGet with the variable index?
    ive looked through the source and havent noticed anywhere this is actually done so i wouldnt be surprised if i have to keep my initial solution of just writing a new function, but still curious.
     
    451
    Posts
    6
    Years
    • Seen today
    been using the pret disassembly of firered to make some changes to the game.
    ive got to a point where i write a value into a temp variable and attempting to pass that variable to a function that is expecting a literal value.

    for example, setwildbattle is normally used like this

    but if you did something like this
    Code:
    setvar VAR_TEMP_0, SPECIES_SNORLAX
    setwildbattle VAR_TEMP_0, 30
    and since setwildbattle is defined like this in c
    Code:
    bool8 ScrCmd_setwildbattle(struct ScriptContext * ctx)
    {
        u16 species = ScriptReadHalfword(ctx);
        u8 level = ScriptReadByte(ctx);
        u16 item = ScriptReadHalfword(ctx);
    
        CreateScriptedWildMon(species, level, item);
        return FALSE;
    }
    it will try to use the variable index as the species id.

    my question, is there any way to pass the value stored in a variable to a function from script that would not require writing a new function in c that calls VarGet with the variable index?
    ive looked through the source and havent noticed anywhere this is actually done so i wouldnt be surprised if i have to keep my initial solution of just writing a new function, but still curious.

    Modifying the function to also accept variables is simple, just do:
    Code:
    u16 species = VarGet(ScriptReadHalfword(ctx));

    This works because VarGet actually returns its parameter if it's smaller than VARS_START(=0x4000), so the above code will work with both literal values and variable IDs, as long as your literal values are smaller than 0x4000.
     

    Lunos

    Random Uruguayan User
    3,114
    Posts
    15
    Years
  • been using the pret disassembly of firered to make some changes to the game.
    ive got to a point where i write a value into a temp variable and attempting to pass that variable to a function that is expecting a literal value.

    for example, setwildbattle is normally used like this

    but if you did something like this
    Code:
    setvar VAR_TEMP_0, SPECIES_SNORLAX
    setwildbattle VAR_TEMP_0, 30
    and since setwildbattle is defined like this in c
    Code:
    bool8 ScrCmd_setwildbattle(struct ScriptContext * ctx)
    {
        u16 species = ScriptReadHalfword(ctx);
        u8 level = ScriptReadByte(ctx);
        u16 item = ScriptReadHalfword(ctx);
    
        CreateScriptedWildMon(species, level, item);
        return FALSE;
    }
    it will try to use the variable index as the species id.

    my question, is there any way to pass the value stored in a variable to a function from script that would not require writing a new function in c that calls VarGet with the variable index?
    Yes, it's perfectly possible.
    There's multiple overworld scripting functions that already read the value from a variable as a parameter in the codebase by default, as a matter of fact
    https://github.com/pret/pokefirered/blob/master/asm/macros/event.inc#L989-L991
    https://github.com/pret/pokefirered/blob/master/src/scrcmd.c#L1733-L1735
     
    3
    Posts
    1
    Years
    • Seen Mar 21, 2023
    Modifying the function to also accept variables is simple, just do:
    Code:
    u16 species = VarGet(ScriptReadHalfword(ctx));

    This works because VarGet actually returns its parameter if it's smaller than VARS_START(=0x4000), so the above code will work with both literal values and variable IDs, as long as your literal values are smaller than 0x4000.

    this is what i am already doing, was just hoping for a different way that doesnt involve changing the original functions or exposing new functions to script. this seems like it would become cumbersome if you have to do it for every function, especially ones like
    these where you just want to specify the level from a variable.

    is there anyway to access the "local variables" in script? would be nice to be able to just have a single function for cases like this that would be defined like the load functions
    Code:
    bool8 ScrCmd_loadvar(struct ScriptContext * ctx)
    {
        u8 index = ScriptReadByte(ctx);
        ctx->data[index] = VarGet(ScriptReadHalfword(ctx));
        return FALSE;
    }
    and just being able to use a value in the ScriptContext::data array as a parameter. i dont have much knowledge of assembly for any platform, so im not sure exactly how the ScriptContext stack works.
     
    Back
    Top