Jump to content

How to define a function to return a value?


Recommended Posts

I notice that some functions can get you an output variable:

LAF RES_NUM_OF_SPELL_NAME STR_VAR spell_name = ~WIZARD_MAGIC_MISSILE~ RET spell_res END

PRINT ~Magic Missile is %spell_res%.spl~ // "Magic Missile is SPWI112.spl"

How do I write a function with that kind of "RET" output?  I currently have this:

DEFINE_PATCH_FUNCTION find_zclons_spell STR_VAR spell_to_find = ~~ BEGIN
// PATCH function run inside new spell to find and use spell entries from d5zclons.2da
 PATCH_IF (FILE_EXISTS_IN_GAME ~d5zclons.2da~) BEGIN
  INNER_ACTION BEGIN
    COPY_EXISTING ~d5zclons.2da~ ~override~
      COUNT_2DA_COLS cols
      READ_2DA_ENTRIES_NOW ~r2en_zclons~ cols
      FOR (row = 0; row < r2en_zclons; ++row) BEGIN
        READ_2DA_ENTRY_FORMER ~r2en_zclons~ row 0 z_ind
        READ_2DA_ENTRY_FORMER ~r2en_zclons~ row 1 mem_spell
        READ_2DA_ENTRY_FORMER ~r2en_zclons~ row 2 cast_spell
        READ_2DA_ENTRY_FORMER ~r2en_zclons~ row 3 spell_type
        READ_2DA_ENTRY_FORMER ~r2en_zclons~ row 4 spell_proc
        PATCH_IF (~%mem_spell%~ STRING_EQUAL_CASE ~%sought_spell%~) BEGIN)
          SET this_spell_ind = %z_ind%
          SPRINT spell_to_cast ~%cast_spell%~
          SPRINT type_of_spell ~%spell_type%~
        END
      END
    BUT_ONLY
  END
 END
END    //    end function

I want to use it more or less like this:

COPY ~might_and_guile/lib/d5_base.spl~ ~override/d5zbard.spl~
  LPF ADD_SPELL_EFFECT INT_VAR opcode = 171 target = 1 timing = 9 STR_VAR resource = ~d5bp102~ END
  LPF find_zclons_spell STR_VAR spell_to_find = ~d5bp102~ RET this_spell_ind END
  LPF ADD_SPELL_EFFECT INT_VAR opcode = 206 target = 1 parameter1 = (0 - 1) timing = 9 STR_VAR resource = EVAL ~d5zb%this_spell_ind%~ END
  LPF ADD_SPELL_EFFECT INT_VAR opcode = 171 target = 1 timing = 9 STR_VAR resource = ~d5bw219~ END
  LPF find_zclons_spell STR_VAR spell_to_find = ~d5bw219~ RET this_spell_ind END
  LPF ADD_SPELL_EFFECT INT_VAR opcode = 206 target = 1 parameter1 = (0 - 1) timing = 9 STR_VAR resource = EVAL ~d5zb%this_spell_ind%~ END

 

Edited by subtledoctor
Link to comment

Just include it in the function header:

DEFINE_PATCH_FUNCTION find_zclons_spell STR_VAR spell_to_find = ~~ RET this_spell_ind BEGIN

It will return whatever value that variable has when the function completes.

Just make sure the return variable always has it's value set before the function completes, as otherwise it will return an error.

Link to comment

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...