Making Scripts Callable from Items - haven1433/HexManiacAdvance GitHub Wiki

Credits to Darthatron on PokeCommunity! Tutorial modified for use in HMA by Mechanical / Ben

How to run a script from an items "Use" command or from registering it from select: IN HMA!

(This tutorial is for Pokemon FireRed.)

This is supposed to give you the opportunity to call event scripts directly from your bag. This gives you the option to do so many things like interacting with the overworld itself, or creating callable scripts like the Move Tutors or Move Reminders, or even the Pokemon Daycare and making them usable right out of your bag. This is a no brainer for debugging as well.

First, you need to write the script that you want to be used with an Item. Second, you need to write down the offset of where you inserted your script.

Third, you need to create the Item you want that script to be used with in the Items Table. Preferably, you can take one of the ????? Slots and give them a name.
If you want the Item to be infinite use, you can make it a Key Item by picking the "key" pocket option. Give the tab "bagkeyitem" a value of 1 so it shows up in your inventory.

If you want the Item to be consumable then pick the pocket option "item" and insert a line of code somewhere in your script that takes away one copy of this Item after using it with the "removeitem" command. For this to work you need to set the Item Type to "Field_item."

The next step is to put this (assembled) code somewhere:

10 B5 04 1C 78 46 13 30 0C 49 08 60 20 1C 0C 49 00 F0 10 F8 10 BC 01 BC 00 47 10 B5 04 1C 0A 48 05 49 00 F0 07 F8 20 1C 06 49 00 F0 03 F8 10 BC 01 BC 00 47 08 47 C0 46 E5 9A 06 08 98 99 03 02 3D 10 0A 08 09 75 07 08 XX XX XX 08

Where XX XX XX is the location of your script reversed. 800300 would be 00 03 80, for example. (Replace the final 08 with an 09 if the location of the script is at or after 0x1000000 in an expanded ROM.)

By entering this hex somewhere in the ROM and putting it's offset+1 in the "Field Usage" box in the Item Manager, you can call any script by using the item from the bag or by select. :) Say I put the routine at 800000 in the ROM, the Field Usage box would be 08800001.

Here is the Source Code of the Routine:

.text
.align 2
.thumb
.thumb_func
.global Hax
	
setup:
	push {r4, lr}
	mov r4, r0
	mov r0, pc
	add r0, #0x13
	ldr r1, .unk_02039998
	str r0, [r1]
	mov r0, r4
	ldr r1, .sub_080A103C
	bl bx_r1
	pop {r4}
	pop {r0}
	bx r0

main:
	push {r4, lr}
	mov r4, r0
	ldr r0, .ScriptToCall
	ldr r1, .CallScript
	bl bx_r1
	mov r0, r4
	ldr r1, .del_c3_from_linked_list
	bl bx_r1
	pop {r4}
	pop {r0}
	bx r0

.align 2
bx_r1:
	bx r1

.align 2
.CallScript:
	.word 0x08069AE4+1
.unk_02039998:
	.word 0x02039998
.sub_080A103C:
	.word 0x080A103C+1
.del_c3_from_linked_list:
	.word 0x08077508+1
.ScriptToCall:
	.word 0x08800300