Jump to content

How to add For loop for 2da files?


Recommended Posts

I'm making a mod of the Beast Master kit and one of the changes I want to do is replace the rangers Charm Animal ability with one that has a penalty to the target. 

After this I need to change the Beast Masters 2DA file to give it this new spell. But since Rangers get Charm Animal 15 times, I don't want to put 15 lines of almost the exact same code, so I want to use a For loop to make things simpler... except I'm having trouble figuring it out.

The file is CLABRN04.2DA. Charm Animal is GA_SPCL311 and its set from level 1 and every 2 levels after that:
                                  1             2                3              4 
ABILITY1    GA_SPCL311  ****   GA_SPCL311  ****     etc.

The copied ability I named spclcbm.SPL.  So I need GA_SPCL311 replaced with GA_spclcbm.

I tried reverse engineering For loops from other posts but I need direct help:

        COUNT_2DA_COLS cols
        READ_2DA_ENTRIES_NOW rows cols
            FOR (cols = 1; cols < 40; ++cols) BEGIN
                READ_2DA_ENTRY_FORMER 1 cols cols ~spell~
                PATCH_IF ~%spell%~ STRING_EQUAL_CASE ~GA_SPCL311~ BEGIN
                    SET_2DA_ENTRY 1 cols 40 ~GA_spclcbm~
                END
            END

Link to comment

For stuff like that it's simpler, and probably just as effective, to use

COPY_EXISTING ~clabrn04.2da~ ~override~
  REPLACE_TEXTUALLY ~GA_SPCL311~ ~GA_SPCLCBM~
IF_EXISTS BUT_ONLY

But as an answer to your original question, since it could be useful in other places, you need to iterate over both rows and columns in order to read every entry.

COUNT_2DA_COLS cols
READ_2DA_ENTRIES_NOW rows cols
FOR (row = 0; row < rows; ++row) BEGIN
  FOR (col = 1; col < cols; ++col) BEGIN
    READ_2DA_ENTRY_FORMER rows row col ~spell~
    PATCH_IF ~%spell%~ STRING_EQUAL_CASE ~GA_SPCL311~ BEGIN
      do stuff
    END
  END
END

Also you use the same variable "cols" for both the total number of columns, and your iterating column number in the FOR clause. That's gonna screw things up. As you see I use the singular "col" for the latter variable.

Finally: not that this is a bit fragile.  If you try to iterate through 40 columns but some modder has added a row with only 39 entries, then the operation will fail. CLAB tables, in particular, tend to get pretty disorderly in games with a bunch of mods installed.  There's things  you can do to firm up the operation, which is why READ_2DA_* functions have that "required column count" field... but that can still fail because an entry you want to read and modify might be in a row that doesn't get handled.

Most 2da tables make nice rectangles and don't get screwed up. But CLAB tables are quite prone to becoming disorganized, and so I avoid using READ_2DA_ENTRY on them when I can.

Edited by subtledoctor
Link to comment
21 minutes ago, Jarno Mikkola said:

You could just make your own .2da file and COPY ~modfolder/filefolder~ ~override~ and be done with this.

Yeah but then I don't learn how to code the more basic stuff. And in cases were there's a lot of files that I could otherwise just make my own,  if I need to update something I just update some lines of code instead of having to open every file and edit. 
So I made sure to get in the habit of only using code when possible.  

Link to comment
27 minutes ago, subtledoctor said:

For stuff like that it's simpler, and probably just as effective, to use


COPY_EXISTING ~clabrn04.2da~ ~override~
  REPLACE_TEXTUALLY ~GA_SPCL311~ ~GA_SPCLCBM~
IF_EXISTS BUT_ONLY

But as an answer to your original question, since it could be useful in other places, you need to iterate over both rows and columns in order to read every entry.


COUNT_2DA_COLS cols
READ_2DA_ENTRIES_NOW rows cols
FOR (row = 0; row < rows; ++row) BEGIN
  FOR (col = 1; col < cols; ++col) BEGIN
    READ_2DA_ENTRY_FORMER rows row col ~spell~
    PATCH_IF ~%spell%~ STRING_EQUAL_CASE ~GA_SPCL311~ BEGIN
      do stuff
    END
  END
END

Also you use the same variable "cols" for both the total number of columns, and your iterating column number in the FOR clause. That's gonna screw things up. As you see I use the singular "col" for the latter variable.

Finally: not that this is a bit fragile.  If you try to iterate through 40 columns but some modder has added a row with only 39 entries, then the operation will fail. CLAB tables, in particular, tend to get pretty disorderly in games with a bunch of mods installed.  There's things  you can do to firm up the operation, which is why READ_2DA_* functions have that "required column count" field... but that can still fail because an entry you want to read and modify might be in a row that doesn't get handled.

Most 2da tables make nice rectangles and don't get screwed up. But CLAB tables are quite prone to becoming disorganized, and so I avoid using READ_2DA_ENTRY on them when I can.

It worked! Thanks!

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...