gamemaster76 Posted March 4, 2021 Share Posted March 4, 2021 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 Quote Link to comment
subtledoctor Posted March 4, 2021 Share Posted March 4, 2021 (edited) 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 March 4, 2021 by subtledoctor Quote Link to comment
Jarno Mikkola Posted March 4, 2021 Share Posted March 4, 2021 You could just make your own .2da file and COPY ~modfolder/filefolder~ ~override~ and be done with this. Quote Link to comment
gamemaster76 Posted March 4, 2021 Author Share Posted March 4, 2021 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. Quote Link to comment
gamemaster76 Posted March 4, 2021 Author Share Posted March 4, 2021 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! Quote Link to comment
Recommended Posts
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.