Jump to content

Renaming functions and variables


Amineri

Recommended Posts

So this doesn't fall under the category of "has game effect", but can definitely be useful for us modders.

 

I often end up re-purposing a variable or function into doing something else.

 

For example, I've recycled the XGUnit class functions:

 

DebugAnims -- now used to set leader perks and stat upgrades

DebugShowOrientation -- now used to set / refresh unit damage reduction

IsInFront -- now used to search a unit's TInventory (small and large items) for a particular items presence

 

Well, I've discovered that I can rename these so that it's easier to remember what's going on. As I add more and more functions, and re-use local variables in odd contexts, it gets harder and harder to keep track of what I've done, let alone explain it to someone else.

 

It turns out that there is only a single name table that defines the function name once. For commonly used names (like "I", or "m_kUnit"), this won't work, but for some things it works beautifully.

 

I did a text search through the XComGame.upk for DebugShowOrientation and it appeared only once. I then did a text replace of this text with "Calc_DamageReduction". Upon reloading the upk in UE Explorer, I saw:

 

Function definition:

simulated function Calc_DamageReduction()
{
    local int I;
    local XComCoverPoint kCoverPointHandle;
    local Vector arrColor, VDir;

    J = 0;
    // End:0x2B
    if(GetPawnType() == 23)
....

Function usage:

    iRank = m_iCoverBits;
    Calc_DamageReduction();
    // End:0x51
    if(m_iCoverBits <= 10)

For future mods I plan on including some hex replacements that will rename some function and variable names in order to make future mod-mods and navigation/understanding of the modded code easier.

Link to comment
Share on other sites

Oh, I forgot to mention something critical -- the new names MUST have exactly the same number of characters as the old names. It looks like the name table is packed together with no "whitespace", so attempting to change the number of characters in a name will almost certainly result in a non-functioning upk.

 

Proposed replacement name functions for the pieces I've changed:

 

DebugShowOrientation => Calc_DamageReduction (this function is being used to update a unit's damage reduction stat)

DebugAnims => UpgrdAlien (this function is used to individually upgrade alien stats and assign perks)

IsInFront => TIHasItem (this function scans through the TInventory small and large item arrays and returns true if the unit has the item)

Link to comment
Share on other sites

In order to better ensure uniqueness of the replacement strings, I'm including in the replacement hex the preceding 4-byte integer which holds the size of the string (I'm not including the null-termination character, though).

 

Here are four replacements I've made so far:

change:
DebugShowOrientation
15 00 00 00 44 65 62 75 67 53 68 6F 77 4F 72 69 65 6E 74 61 74 69 6F 6E

to:
Calc_DamageReduction
15 00 00 00 43 61 6C 63 5F 44 61 6D 61 67 65 52 65 64 75 63 74 69 6F 6E


change:
IsInFront
0A 00 00 00 49 73 49 6E 46 72 6F 6E 74

to:
TIHasItem
0A 00 00 00 54 49 48 61 73 49 74 65 6D

change:
TShotInfo_ToString
13 00 00 00 54 53 68 6F 74 49 6E 66 6F 5F 54 6F 53 74 72 69 6E 67

to:
DmgModByPerksItems
13 00 00 00 44 6D 67 4D 6F 64 42 79 50 65 72 6B 73 49 74 65 6D 73

change:
DebugAnims 
0B 00 00 00 44 65 62 75 67 41 6E 69 6D 73 

to: 
UpgrdAlien 
0B 00 00 00 55 70 67 72 64 41 6C 69 65 6E
Edited by Amineri
Link to comment
Share on other sites

Oh, I forgot to mention something critical -- the new names MUST have exactly the same number of characters as the old names. It looks like the name table is packed together with no "whitespace", so attempting to change the number of characters in a name will almost certainly result in a non-functioning upk.

Unless you also adjust the name of some other function accordingly and update the size integers for both...

I just tested it and it works without problem.

 

But then again renaming unrelated functions might be defeating the purpose of making things easier to understand :smile:

 

Edit: Using the cut and paste functionality in HxD and ignoring the warning about changing file size makes this much easier. Just Cut away hexes from the function which should have shorter name, update the size for it, insert next to the one you want to expand and update size, and your done...

 

But if you want to distribute it to someone else then you probably should do it the hard way and modify all hexes between both function names... Or consider including the entire updated table in the mod.

Edited by Bertilsson
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...