Login  Register
 



Post new topicReply to topic Go to page 1, 2, 3, 4, 5  Next
 
Author Message
 PostPosted: 01 Mar, 2011 
 
User avatar

Joined: 19 Jul, 2009
Posts: 2972
Offline
SC2 Mod Support Minimod v7
Allows any number of mods to co-exist by enabling non-destructive hooking of original game files.

Requires the DLC for multiplayer/skirmish mods. Making this change without the DLC enabled would require that lua.scd be replaced, which is exactly the kind of thing this aims to avoid. With the DLC, no .scd replacement is needed, and only a handful of files need to be shadowed.

Supports campaign mods as of v6. These do not require the DLC to function and can be used with vanilla SC2, as campaign support shadows files using the CampaignBranch mod.

Download v7

The mod is configured for use with CerusVI's SC2 Mod Manager. Place the .scd within \gamedata to enable it without the mod manager, and remove any previous versions of the mod.


How to hook files in your mod:
·········································································································
  1. Read the guides on non-destructive hooking in the [FA] Useful Information sticky in Mod Developer Support. The basic methods for hooking classes and functions in Forged Alliance apply to SC2 as well. You want to avoid copy-pasting code (especially entire files) whenever possible, as this will ruin any compatibility benefits that this method enables.

  2. Create a folder within your mod's .scd called /modhook/<mymod>, where '<mymod>' is the name of your mod. Make sure the name of this folder is unique, so that it will not conflict with other mods.

  3. Duplicate the relative path of any files you want to hook, within this folder. For example, if you want to hook /lua/sim/Unit.lua, you would create a blank file in your mod with the path /modhook/<mymod>/lua/sim/Unit.lua. This file will then be loaded along with the original.

  4. Hook or alter any tables, functions, or classes in the file using non-destructive methods. As above, consult the modding guides on the GPG Mod Developer Support forums, and don't hesitate to post any questions you may have. Increased mod compatibility will benefit nearly everyone who wants to use your mod, and it isn't difficult to learn the best ways to keep your mod non-destructive.

Campaign mod support uses the same hook folder and structure, so normal mods are supported in the campaign. However, keep in mind that skirmish/multiplayer mods made without the campaign in mind can have unintended consequences or side effects in campaign missions, and are not guaranteed to work without problems.


How it's implemented:
·········································································································
The three core init files (RuleInit.lua, SessionInit.lua, and simInit.lua) are shadowed and their doscript function is hooked to search the /modhook folder for matching paths every time a new lua file is imported, executing those files into the same environment as the original. .bp files are skipped for performance and compatibility reasons, and because blueprints already have their own non-destructive modification method (merging).

In v3 and beyond, several other engine-loaded files will be shadowed and an identical method used to hook copies of these files. This is necessary because any files loaded directly by the engine into a separate lua state skip the doscript hooks in the three core init files. Hooking these files in your mod should work seamlessly - use the same method detailed above. If you run into any files that you cannot hook that are not already on this list or the "can't hook" list below in Limitations, please let me know by posting in this thread.

All of the following files and all files that they introduce can now be hooked non-destructively by other mods.

Files currently shadowed:
- /lua/RuleInit.lua (Blueprint state core)
- /lua/simInit.lua (Sim state core)
- /lua/SessionInit.lua (User state core)
- /lua/ui/game/Commands/orderstable.lua (UI ability definitions)


Other features:
·········································································································
An included config.lua hook adds several debugging features:
  • Warnings and stack tracebacks for non-existent global variable accesses will be displayed in the log when the /log parameter is present
  • A global __stacktraceback(n) function (used by the above) has been added that re-creates an error message stack traceback starting n levels back, mimicking the format of an error() without the halt
  • Lua-called WARN and error messages will be displayed in the log, SPEW messages will as well when the /logspew parameter is present or __logspew is set to true
  • LOG messages with multiple comma-separated parameters will be concatenated with a space instead of '\000' (thanks Neruz)


Limitations:
·········································································································
This implementation has one limitation that the SC2 engine's integrated (but currently broken) hooking feature does not-- it does not allow access to local variables within the original file being hooked, nor within any other mods' hooks. All global variables, tables, functions, and classes are accessible and can be overridden or modified, but there is no way to make locals available to hooked files. For sim and unit modding, this will usually just mean you have to re-declare any of the local imports at the top of the file that the rest of your hook needs to use.

Additionally, some engine-loaded UI files cannot be hooked, as they are loaded directly into a very limited Lua state with absolutely no global functions available (including LOG, doscript, or even debug methods). This means that there is absolutely no way to hook these files, and they must be overridden via .scd replacement (or shadowed, if any are found that aren't already shadowed by the DLC). As above, if you locate any files that you cannot hook that aren't on these lists, please post their paths in this thread and I'll investigate whether or not they can be individually shadowed and made hookable.

Files that cannot be hooked:
- /lua/ui/game/research/research_grid.lua
- /lua/ui/game/research/research_layout.lua

Because of this, research tree mods cannot be made directly compatible with one another. While unfortunate, even with hooking it would be very unlikely that any two mods that alter the research tree layout or requirements would work together due to conflicting changes.


Version History:
·········································································································
v1:
  • First release
v2:
  • The non-existent global variable warning is skipped unless the /log commandline parameter is present
  • Shadowed /lua/sim/Buff.lua to fix an error that was spamming the log with these messages
v3:
  • Shadowed new files for hooking:
    - /lua/ui/game/Commands/orderstable.lua
v4:
  • Re-worked orderstable.lua hooking method so that it actually has access to UIOrdersTable (this was more difficult than it should have been due to a custom metatable in use for that file)
  • Insured that all file doscripting is done with protected calls to prevent crashes
v5:
  • Complete structural overhaul:
    - Only the three state init files are shadowed, and can now be hooked by other mods
    - Core functionality is now modular and can be easily applied to engine-loaded files
    - Full backward compatibility is maintained for any mods requiring or compatible with v3+
  • orderstable.lua is now a 'real' doscript hook with a normal shared environment
  • config.lua and Buff.lua functionality moved to hooks
  • The log now shows the lua state that a file is hooked into, and displays any errors resulting from a protected call failure for that file
v6:
  • Campaign mod support added
    - Uses the same hooking method as skirmish mods, meaning that normal mods will work in the campaign (assuming no unintended conflicts/side effects).
  • Non-destructive Blueprint Category support added
    - Uses key name values from four new blueprint tables: Add/RemoveCategories and Add/RemoveBuildableCategory
    - See this post for details.
v7:
  • Fixed a problem with category removal - this feature will now work correctly and list successful category changes in the log


Last edited by Mithy on 15 Apr, 2011, edited 11 times in total.

Top
 Profile  
 PostPosted: 01 Mar, 2011 
 

Joined: 01 Jan, 2011
Posts: 1040
Offline
Now if we had a way to shadow any core game files. I'd like to create my All Factions mod, which is just a single file, as a standalone mod rather than having to modify lua.scd.


EDIT: What I'm experimenting with: I want to shadow lua/common/factions.lua. I noticed that DLC1 is shadowing lua/common/TerrainTypes.lua, and does so by providing a copy of "mods" in lua.scd itself. So, following it as a prototype, I create the following:

- /gamedata/z_lua_dlc2.scd/mods/DLC2/shadow/mod_info.lua
- /gamedata/z_lua_dlc2.scd/mods/DLC2/shadow/lua/common/factions.lua

- /gamedata/lua.scd/mods/DLC2/shadow/mod_info.lua
- /gamedata/lua.scd/mods/DLC2/shadow/lua/common/factions.lua

I increase/change the value of the mod ID in mod_info.lua.

I use the same files in z_lua_dlc2.scd for lua.scd, just as DLC1. The only difference in approach between my "DLC2" and DLC1 is that the latter is all compiled, which shouldn't matter.

Can't get it to work.

_________________
Stin wrote:
Now I know why I added you to my ignore list. I was hoping you were no longer an idiot.

Too bad we can't all be as smart as you, Stin. Wait a minute... that's a good thing.


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 

Joined: 02 Jul, 2010
Posts: 1266
Offline
Mithy don't re-enable non-existent global variable access warnings it creates a problem with in game lag and can also crash the game.

_________________
Creator of SupCom2
Revamp Expansion Mod
Image
Image
Image


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 
User avatar

Joined: 19 Jul, 2009
Posts: 2972
Offline
madface wrote:
Now if we had a way to shadow any core game files. I'd like to create my All Factions mod, which is just a single file, as a standalone mod rather than having to modify lua.scd.

Dude, you no longer need to shadow anything with this. Do you know what hooking means? You can edit anything in factions.lua non-destructively using this mod. It totally precludes/invalidates shadowing as a means of modding.


liveordie wrote:
Mithy don't re-enable non-existent global variable access warnings it creates a problem with in game lag and can also crash the game.

Uh, did you even read the rest of that paragraph where I clearly detailed how I worked around that issue so that it doesn't do that?

Unless there's a point in the game where hundreds or thousands of non-existent variable accesses are happening per second, and the log is open, this can't cause any lag.


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 

Joined: 01 Jan, 2011
Posts: 1040
Offline
I tried your hooking approach, but I couldn't get it to work. Following your instructions, I created the following:

/gamedata/all_factions.scd/modhook/all_factions/lua/common/factions.lua
/gamedata/all_factions.scd/mod_info.lua

but it didn't work.

_________________
Stin wrote:
Now I know why I added you to my ignore list. I was hoping you were no longer an idiot.

Too bad we can't all be as smart as you, Stin. Wait a minute... that's a good thing.


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 

Joined: 02 Jul, 2010
Posts: 1266
Offline
Mithy wrote:
madface wrote:
Now if we had a way to shadow any core game files. I'd like to create my All Factions mod, which is just a single file, as a standalone mod rather than having to modify lua.scd.

Dude, you no longer need to shadow anything with this. Do you know what hooking means? You can edit anything in factions.lua non-destructively using this mod. It totally precludes/invalidates shadowing as a means of modding.


liveordie wrote:
Mithy don't re-enable non-existent global variable access warnings it creates a problem with in game lag and can also crash the game.

Uh, did you even read the rest of that paragraph where I clearly detailed how I worked around that issue so that it doesn't do that?

Unless there's a point in the game where hundreds or thousands of non-existent variable accesses are happening per second, and the log is open, this can't cause any lag.


No it causes lag even with out a log running this was the reason i pulled my 1.0 release i was playing around with it just before i released it and didn't know it lagged the game like it did if you 10x game speed zoom in and out it stutters really bad, Test it with the normal game at 10x game speed you will see theres no such problem.

_________________
Creator of SupCom2
Revamp Expansion Mod
Image
Image
Image


Last edited by liveordie on 01 Mar, 2011, edited 1 time in total.

Top
 Profile  
 PostPosted: 01 Mar, 2011 
 
User avatar

Joined: 19 Jul, 2009
Posts: 2972
Offline
liveordie wrote:
No it causes lag even with out a log running this was the reason i pulled my 1.0 release i was playing around with it just before i released it and didn't know it lagged the game like it did if you 10x game speed zoom in and out it stutters really bad, Test it with the normal game at 10x game speed you will see there no such problem.

I don't suppose you have a log of that? Because I'm seeing maybe a two dozen errors total per game. That's nowhere near enough to cause any problems.


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 

Joined: 02 Jul, 2010
Posts: 1266
Offline
Mithy wrote:
liveordie wrote:
No it causes lag even with out a log running this was the reason i pulled my 1.0 release i was playing around with it just before i released it and didn't know it lagged the game like it did if you 10x game speed zoom in and out it stutters really bad, Test it with the normal game at 10x game speed you will see there no such problem.

I don't suppose you have a log of that? Because I'm seeing maybe a two dozen errors total per game. That's nowhere near enough to cause any problems.


Before you look at this i haven't changed any buffs in my mod.
http://pastebin.com/UspmmvMr

Edit tested it with the normal game same thing.

_________________
Creator of SupCom2
Revamp Expansion Mod
Image
Image
Image


Last edited by liveordie on 01 Mar, 2011, edited 1 time in total.

Top
 Profile  
 PostPosted: 01 Mar, 2011 
 

Joined: 01 Jan, 2011
Posts: 1040
Offline
Either I'm doing something wrong, or I don't have access to change the variables I want via this approach.

Following the tutorial from FA, I'm not dealing with functions and I don't need to preserve something from before that I would then add ('hook') my new code to. I simply want to alter the entries for a variable. I know you say to avoid cut+paste, but since I want to overwrite the existing variable values, I don't see any way around a cut+paste of the file and just make my changes directly. So, in effect what I'm doing is shadowing but in a hooking environment without preserving the underlying code as per the tutorial.


EDIT: It's working. I'm an idiot. I completely forgot to download your .scd! :roll: :roll: :roll:

_________________
Stin wrote:
Now I know why I added you to my ignore list. I was hoping you were no longer an idiot.

Too bad we can't all be as smart as you, Stin. Wait a minute... that's a good thing.


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 
User avatar

Joined: 19 Jul, 2009
Posts: 2972
Offline
madface wrote:
I tried your hooking approach, but I couldn't get it to work. Following your instructions, I created the following:

/gamedata/all_factions.scd/modhook/all_factions/lua/common/factions.lua
/gamedata/all_factions.scd/mod_info.lua

but it didn't work.

The mod_info.lua won't do anything, but that should be the correct path.

I just tried this with the path \gamedata\all_factions.scd\modhook\all_factions\lua\common\factions.lua, and it worked fine? I did not try a blanket re-definition of the Factions table (as that's unnecessarily destructive), and instead just directly change the AdditionalInitialUnits subtables.

Image
Download


I see you found your problem, but:
madface wrote:
Following the tutorial from FA, I'm not dealing with functions and I don't need to preserve something from before that I would then add ('hook') my new code to. I simply want to alter the entries for a variable. I know you say to avoid cut+paste, but since I want to overwrite the existing variable values, I don't see any way around a cut+paste of the file and just make my changes directly. So, in effect what I'm doing is shadowing but in a hooking environment without preserving the underlying code as per the tutorial.

With tables, by far the easiest and least destructive way to alter them is to just directly index the value within them that you want to change. In this case:
Code:
Factions[1].AdditionalInitialUnits = { 'UIL0001', 'UCL0001',}
Factions[2].AdditionalInitialUnits = { 'UIL0001', 'UUL0001',}
Factions[3].AdditionalInitialUnits = { 'UUL0001', 'UCL0001',}
is all that's needed. While it's unlikely that someone else will make a compatible change to this file, it's still possible, and this is a good habit to get into anyway.

If you wanted, you could even make it compatible with other initial-unit mods, by doing this:
Code:
local uefUnits = Factions[1].AdditionalInitialUnits
local cybUnits = Factions[2].AdditionalInitialUnits
local iluUnits = Factions[3].AdditionalInitialUnits

table.removeByValue(uefUnits, 'UUL0002')
table.insert(uefUnits, 'UIL0001')
table.insert(uefUnits, 'UCL0001')

table.removeByValue(cybUnits, 'UCL0002')
table.insert(cybUnits, 'UIL0001')
table.insert(cybUnits, 'UUL0001')

table.removeByValue(iluUnits, 'UIL0002')
table.insert(cybUnits, 'UCL0001')
table.insert(cybUnits, 'UUL0001')


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 

Joined: 01 Jan, 2011
Posts: 1040
Offline
Thanks so much Mithy! I wasn't sure how to properly use indices, so that's why I was just using copy+paste on the whole thing. But this is much more elegant. Thanks! :P :P :P

_________________
Stin wrote:
Now I know why I added you to my ignore list. I was hoping you were no longer an idiot.

Too bad we can't all be as smart as you, Stin. Wait a minute... that's a good thing.


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 

Joined: 02 Jul, 2010
Posts: 1266
Offline
Mithy do you see now what im talking about did you have a look over the log i linked?

_________________
Creator of SupCom2
Revamp Expansion Mod
Image
Image
Image


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 
User avatar

Joined: 19 Jul, 2009
Posts: 2972
Offline
liveordie wrote:
Before you look at this i haven't changed any buffs in my mod.
http://pastebin.com/UspmmvMr

Edit tested it with the normal game same thing.

Yep, looks like GPG boned that one. It's supposed to be def.DurationPulse, not buffTable.DurationPulse. This what happens when you have no error reporting. Not sure why I didn't see it in my games, unless only those two illuminate units use a REFRESH type buff stacking.

Ok, tell you what - next version will include both a fix for this, and will check for the /log commandline parameter. So it won't be enabled for a normal game no matter what.


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 

Joined: 02 Jul, 2010
Posts: 1266
Offline
Mithy wrote:
liveordie wrote:
Before you look at this i haven't changed any buffs in my mod.
http://pastebin.com/UspmmvMr

Edit tested it with the normal game same thing.

Yep, looks like GPG boned that one. It's supposed to be def.DurationPulse, not buffTable.DurationPulse. This what happens when you have no error reporting. Not sure why I didn't see it in my games, unless only those two illuminate units use a REFRESH type buff stacking.

Ok, tell you what - next version will include both a fix for this, and will check for the /log commandline parameter. So it won't be enabled for a normal game no matter what.


O ok cool but i was running the DLC :? with that log running, is that error worth fixing or would it be located in the engine?

Code:
info:         #00001b90[/mods/dlc1/shadow/units/illuminate/uib0001/uib0001_script.lua]:53 in function: 1317A1EC


how can it be reading this file when its not even there ?

_________________
Creator of SupCom2
Revamp Expansion Mod
Image
Image
Image


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 
User avatar

Joined: 19 Jul, 2009
Posts: 2972
Offline
liveordie wrote:
O ok cool but i was running the DLC :? with that log running, is that error worth fixing or would it be located in the engine?

Did you look at the error? That's why I have that message - it points right to the problem, which is Buff.lua line 71. So yeah, it's fixable in lua just fine, and it just means I have to add one extra file to the mod. Or, I could start a bugfix mod, but I'm not sure I have the motivation to maintain another one of those.

liveordie wrote:
Code:
info:         #00001b90[/mods/dlc1/shadow/units/illuminate/uib0001/uib0001_script.lua]:53 in function: 1317A1EC


how can it be reading this file when its not even there ?

It is, in lua.scd. Why it's not also in z_lua_dlc1.scd, I don't know. I am certainly no fan of the way they have this game's scds set up, let me tell you.

According to the copy in uncompiled_lua.scd, that shadow file enables illuminate land factories to be built on water. I didn't know they could do that?


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 

Joined: 02 Jul, 2010
Posts: 1266
Offline
Mithy wrote:
liveordie wrote:
O ok cool but i was running the DLC :? with that log running, is that error worth fixing or would it be located in the engine?

Did you look at the error? That's why I have that message - it points right to the problem, which is Buff.lua line 71. So yeah, it's fixable in lua just fine, and it just means I have to add one extra file to the mod. Or, I could start a bugfix mod, but I'm not sure I have the motivation to maintain another one of those.

liveordie wrote:
Code:
info:         #00001b90[/mods/dlc1/shadow/units/illuminate/uib0001/uib0001_script.lua]:53 in function: 1317A1EC


how can it be reading this file when its not even there ?

It is, in lua.scd. Why it's not also in z_lua_dlc1.scd, I don't know. I am certainly no fan of the way they have this game's scds set up, let me tell you.

According to the copy in uncompiled_lua.scd, that shadow file enables illuminate land factories to be built on water. I didn't know they could do that?


The file is there i was looking in uil0001 :P but still doesn't explain why its reporting a error missing code in the script or some think, Would this cause any problems with the game ?

_________________
Creator of SupCom2
Revamp Expansion Mod
Image
Image
Image


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 
User avatar

Joined: 19 Jul, 2009
Posts: 2972
Offline
It's reporting a non-existent global variable access because 'buffTable' doesn't exist within the scope of line 71 in Buff.lua. 'buffTable' is supposed to be 'def', but was copy-pasted from a line further down where 'buffTable.DurationPulse' is referenced by another function, in which buffTable is actually defined.

Yes, it would cause problems, and would result in DurationPulse buffs not being refreshed. I don't think GPG actually uses any DurationPulse buffs, so with the warning disabled, the problem was never noticed.

Releasing a new version that both fixes Buff.lua and makes it so that the non-existent global warning only happens with /log enabled.


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 

Joined: 01 Jan, 2011
Posts: 1040
Offline
Mithy wrote:
Code:
local uefUnits = Factions[1].AdditionalInitialUnits
local cybUnits = Factions[2].AdditionalInitialUnits
local iluUnits = Factions[3].AdditionalInitialUnits

table.removeByValue(uefUnits, 'UUL0002')
table.insert(uefUnits, 'UIL0001')
table.insert(uefUnits, 'UCL0001')

table.removeByValue(cybUnits, 'UCL0002')
table.insert(cybUnits, 'UIL0001')
table.insert(cybUnits, 'UUL0001')

table.removeByValue(iluUnits, 'UIL0002')
table.insert(cybUnits, 'UCL0001')
table.insert(cybUnits, 'UUL0001')

Your code only removes 1 of the 2 initial engineers. I found that you have to invoke 'table.removeByValue(uefUnits, 'UUL0002')' a second time in order to remove the other engineer. So, removeByBalue seems to be per-item removal rather than per-class removal.

_________________
Stin wrote:
Now I know why I added you to my ignore list. I was hoping you were no longer an idiot.

Too bad we can't all be as smart as you, Stin. Wait a minute... that's a good thing.


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 

Joined: 02 Jul, 2010
Posts: 1266
Offline
Mithy wrote:
It's reporting a non-existent global variable access because 'buffTable' doesn't exist within the scope of line 71 in Buff.lua. 'buffTable' is supposed to be 'def', but was copy-pasted from a line further down where 'buffTable.DurationPulse' is referenced by another function, in which buffTable is actually defined.

Yes, it would cause problems, and would result in DurationPulse buffs not being refreshed. I don't think GPG actually uses any DurationPulse buffs, so with the warning disabled, the problem was never noticed.

Releasing a new version that both fixes Buff.lua and makes it so that the non-existent global warning only happens with /log enabled.


Ok cool thanks mithy would you mind if i apply your fixes to my mod?

_________________
Creator of SupCom2
Revamp Expansion Mod
Image
Image
Image


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 

Joined: 01 Jan, 2011
Posts: 1040
Offline
Mithy: I understand it from context that you are allowing me to use as a template for my mod the above code you provided. But I just want to make sure that you're ok if I distribute a slightly-altered version (just change the variable names to suit my style) as a mod under my name??? The code is simple and so straightforward that there's not much that I can alter. I will be making more complex starting conditions in the future as my other mod progresses, and so I would like to use this as a base. I will acknowledge and credit you.

_________________
Stin wrote:
Now I know why I added you to my ignore list. I was hoping you were no longer an idiot.

Too bad we can't all be as smart as you, Stin. Wait a minute... that's a good thing.


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 
User avatar

Joined: 19 Jul, 2009
Posts: 2972
Offline
liveordie wrote:
Ok cool thanks mithy would you mind if i apply your fixes to my mod?

I'd prefer you just link to this one if you're going to require it, so I can keep it updated on my own.

These fixes won't actually affect anything for the end user of your mod unless you start using DurationPulse buffs. And if you use the hooking support, only one active mod at a time can provide that without causing biiig problems, so again, it's best to just link to this thread or the download page to insure that people who do end up using multiple mods only have one hooking support mod running at a time.


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 
User avatar

Joined: 19 Jul, 2009
Posts: 2972
Offline
madface wrote:
Your code only removes 1 of the 2 initial engineers. I found that you have to invoke 'table.removeByValue(uefUnits, 'UUL0002')' a second time in order to remove the other engineer. So, removeByBalue seems to be per-item removal rather than per-class removal.

Yeah, I thought about that. Obviously I didn't test that. Looks like table.removeByValue doesn't take a number parameter, so it's either run it twice, or do your own loop to iterate/compare/remove.

madface wrote:
Mithy: I understand it from context that you are allowing me to use as a template for my mod the above code you provided. But I just want to make sure that you're ok if I distribute a slightly-altered version (just change the variable names to suit my style) as a mod under my name??? The code is simple and so straightforward that there's not much that I can alter. I will be making more complex starting conditions in the future as my other mod progresses, and so I would like to use this as a base. I will acknowledge and credit you.

If you're talking about the all-factions code, yep, that's fine.


Top
 Profile  
 PostPosted: 01 Mar, 2011 
 
User avatar

Joined: 19 Jul, 2009
Posts: 2972
Offline
Did this to entertain myself while I was waiting something - just a different take on non-destructive table manipulation:
Code:
local removeEngineers = {
    [1] = 'UUL0002',
    [2] = 'UCL0002',
    [3] = 'UIL0002',
}

for index, faction in Factions do
    --Remove engineers
    while table.find(faction.AdditionalInitialUnits, removeEngineers[index]) do
        table.removeByValue(faction.AdditionalInitialUnits, removeEngineers[index])
    end
    --Add other ACUs
    for index2, faction2 in Factions do
        if index2 ~= index then
            table.insert(faction.AdditionalInitialUnits, faction2.InitialUnit)
        end
    end
end
This will remove any number of engineers, and in the case of an ACU-replacement mod, it will use the new ACUs.

Practically speaking, it's not any different than the two previous methods, and I did it mostly to pass time and to give an example of cool stuff you can do with global tables that can allow multiple mods that would otherwise be incompatible to co-exist peacefully. The chances of someone making a mod with new ACUs is pretty low. It could even be expanded further to support added factions by using string.find or string.sub to remove engineer bpids instead of using a lookup table, but of course faction mods are impossible in SC2 (and that would have taken an extra 5 or 10 minutes to do).

Note how I used table.find and table.rBV to remove engineer entries rather than a normal for loop, because if you do a table.remove on the table that you're iterating (including via table.removeByValue), it shuffles all subsequent entries down and causes the loop to skip one index in the table. This method effectively iterates for each remove attempt using while and table.find.


Top
 Profile  
 PostPosted: 02 Mar, 2011 
 
User avatar

Joined: 15 Feb, 2007
Posts: 20020
Location: Presumably, at the time of posting, his computer.
Offline
Ignore me, I'm a moron.

_________________
I'm watchin you!
Image


Top
 Profile  
 PostPosted: 02 Mar, 2011 
 

Joined: 01 Jan, 2011
Posts: 1040
Offline
Mithy wrote:
Did this to entertain myself while I was waiting something - just a different take on non-destructive table manipulation:
Code:
local removeEngineers = {
    [1] = 'UUL0002',
    [2] = 'UCL0002',
    [3] = 'UIL0002',
}

for index, faction in Factions do
    --Remove engineers
    while table.find(faction.AdditionalInitialUnits, removeEngineers[index]) do
        table.removeByValue(faction.AdditionalInitialUnits, removeEngineers[index])
    end
    --Add other ACUs
    for index2, faction2 in Factions do
        if index2 ~= index then
            table.insert(faction.AdditionalInitialUnits, faction2.InitialUnit)
        end
    end
end
This will remove any number of engineers, and in the case of an ACU-replacement mod, it will use the new ACUs.

Practically speaking, it's not any different than the two previous methods, and I did it mostly to pass time and to give an example of cool stuff you can do with global tables that can allow multiple mods that would otherwise be incompatible to co-exist peacefully. The chances of someone making a mod with new ACUs is pretty low. It could even be expanded further to support added factions by using string.find or string.sub to remove engineer bpids instead of using a lookup table, but of course faction mods are impossible in SC2 (and that would have taken an extra 5 or 10 minutes to do).

Note how I used table.find and table.rBV to remove engineer entries rather than a normal for loop, because if you do a table.remove on the table that you're iterating (including via table.removeByValue), it shuffles all subsequent entries down and causes the loop to skip one index in the table. This method effectively iterates for each remove attempt using while and table.find.


I'll stick with the second version you suggested before. It gets the job done and still allows the concept to be extended in the future. This new code is a bit overkill, but I do very much appreciate the help it provides me to get familiar with this type of coding. Thanks for all the help!

_________________
Stin wrote:
Now I know why I added you to my ignore list. I was hoping you were no longer an idiot.

Too bad we can't all be as smart as you, Stin. Wait a minute... that's a good thing.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic Go to page 1, 2, 3, 4, 5  Next



Quick Tools

Search for:
Jump to:  

© 2002-2010 Gas Powered Games Corp. All Rights Reserved. Gas Powered Games is the exclusive trademark of Gas Powered Games Corp.
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
 
Home| Games | Company | News & Press | Support
  Terms of Use   |    Copyright Information   |    Privacy Policy