Amineri Posted May 14, 2013 Share Posted May 14, 2013 As was accidentally discovered by johnnylump recently, it is possible to use a local variable reference in a function that it was not originally defined in. I've played around with this a bit more, and it works generally, not just with local variables that are the same name. That is :: A local variable reference defined / used in one function can be used in another function So far I've only used this for local variables that are defined in a different function but within the same class. Even with that limitation, this will be very useful. Here is the function (new version) decompiled. function InitAlienLoadoutInfos(){ local int PlayerIndex, iAlienPods; PlayerIndex = 0; J0x0B: // End:0x42E [Loop If] if(PlayerIndex < 4) { // End:0x51 if(m_arrTeamLoadoutInfos[PlayerIndex].m_eTeam == 16) { return; } // End:0x420 else { // End:0x420 if(m_arrTeamLoadoutInfos[PlayerIndex].m_eTeam == 0) { m_arrTeamLoadoutInfos[PlayerIndex].m_eTeam = 16; // End:0x324 if(!m_bUseAlienInfo) { iAlienPods = 0; J0xC5: // End:0x321 [Loop If] if(iAlienPods < m_kAlienSquad.arrPods.Length) { Index = 0; J0xFB: // End:0x313 [Loop If] if(Index < 5) { // End:0x160 if(Index == 0) { m_iLowestDifficulty = m_kAlienSquad.arrPods[iAlienPods].eMain; } // End:0x1B5 if(Index == 1) { m_iLowestDifficulty = m_kAlienSquad.arrPods[iAlienPods].eSupport1; } // End:0x20A if(Index == 2) { m_iLowestDifficulty = m_kAlienSquad.arrPods[iAlienPods].eSupport2; } // End:0x25F if(Index == 3) { m_iLowestDifficulty = m_kAlienSquad.arrPods[iAlienPods].eMainAltWeapon; } // End:0x2B4 if(Index == 4) { m_iLowestDifficulty = m_kAlienSquad.arrPods[iAlienPods].eSupport1AltWeapon; } // End:0x305 if(m_iLowestDifficulty != 0) { m_arrTeamLoadoutInfos[PlayerIndex].m_arrUnits.AddItem(BuildAlienContent(byte(m_iLowestDifficulty), 0)); } ++ Index; // [Loop Continue] goto J0xFB; } ++ iAlienPods; // [Loop Continue] goto J0xC5; } } // End:0x41D else { m_arrTeamLoadoutInfos[PlayerIndex].m_arrUnits.AddItem(BuildAlienContent(byte(m_kAlienInfo.iPodLeaderType))); m_arrTeamLoadoutInfos[PlayerIndex].m_arrUnits.AddItem(BuildAlienContent(byte(m_kAlienInfo.iPodSupporterType))); m_arrTeamLoadoutInfos[PlayerIndex].m_arrUnits.AddItem(BuildAlienContent(byte(m_kAlienInfo.iRoamingType))); } // [Explicit Break] goto J0x42E; } } ++ PlayerIndex; J0x42E: // [Loop Continue] goto J0x0B; } return; } The Index variable is a local variable that was defined in another function (in the same class as this, XGMissionDesc). I suspect that this could get a person into trouble if the local variable used was from a function in the call sequence to the current one. That is, if LocalVar is defined in FunctionA, and FunctionA calls FunctionB, and FunctionB calls FunctionC, and I re-use LocalVar in FunctionC, I suspect it might result in some aberrant behavior when the code returns back to FunctionA. Still, this may be pretty useful to people. Link to comment Share on other sites More sharing options...
anUser Posted May 14, 2013 Share Posted May 14, 2013 so you just call it using the same hex code it had in it's function? Anyway it's indeed a great finding, I remember discarding a project because I didn't have a counter variable to iterate an array Link to comment Share on other sites More sharing options...
johnnylump Posted May 14, 2013 Share Posted May 14, 2013 johnnylump, idiot savant of x-com modding :) Link to comment Share on other sites More sharing options...
Amineri Posted May 14, 2013 Author Share Posted May 14, 2013 so you just call it using the same hex code it had in it's function? Anyway it's indeed a great finding, I remember discarding a project because I didn't have a counter variable to iterate an array Yes, you use the same reference as used in the defining function. Including the 00 local variable token. johnnylump, idiot savant of x-com modding :smile: Many (maybe most) great discoveries were made via accidents! Take your place amongst the greats :D Link to comment Share on other sites More sharing options...
anUser Posted May 14, 2013 Share Posted May 14, 2013 I've just thought that this trick can be used as a way to fake extra arguments for custom function calls, like firaxis did in BuildItems with that category variable... call me dumb, but I didn't thought of this at first and now it seems so great to me :) Link to comment Share on other sites More sharing options...
Recommended Posts