https://wiki.beyondunreal.com/w/index.php?action=history&feed=atom Function call specifiers - Revision history 2017-11-18T03:08:14Z Revision history for this page on the wiki MediaWiki 1.25.1 https://wiki.beyondunreal.com/Function_call_specifiers?diff=33084&oldid=prev Wormbo: New page: UnrealScript provides two special keywords for calling functions that would otherwise not be accessible: '''Super''' for calling overridden functions and '''Global''' for calling n... 2008-10-01T18:23:50Z <p>New page: <a href="/UnrealScript" title="UnrealScript">UnrealScript</a> provides two special keywords for calling <a href="/Functions" title="Functions">functions</a> that would otherwise not be accessible: &#039;&#039;&#039;Super&#039;&#039;&#039; for calling overridden functions and &#039;&#039;&#039;Global&#039;&#039;&#039; for calling n...</p> <p><b>New page</b></p><div>[[UnrealScript]] provides two special keywords for calling [[functions]] that would otherwise not be accessible: '''Super''' for calling overridden functions and '''Global''' for calling non-state functions. However, these function call specifiers are only allowed for calling functions on the same instance (or class for static functions) as the code calling the functions.<br /> <br /> ==Super==<br /> The keyword '''Super''' is used to call an overridden version of a function on the same instance or class.<br /> <br /> ===Simple version: Direct parent state or class===<br /> Syntax:<br /> '''Super.'''functionname'''('''''[parameters]''''')'''<br /> <br /> The simple version uses the parent context of the current state or class to call the function with the specified name. The parent context of a state is either the parent state or, if the state doesn't extend any other state, the state's containing class. The parent context of a class is its parent class.<br /> <br /> '''Note:''' If the current context doesn't actually override the function called through '''Super''' this call will almost be the same as calling the function directly.<br /> <br /> ===Advanced version: Indirect parent class===<br /> Syntax:<br /> '''Super('''classname''').'''functionname'''('''''[parameters]''''')'''<br /> <br /> The advanced version looks for a class with the specified name. If the call originated in state code or a state function, it looks for a state with the same name in that class and uses that as context for calling the function. If no such state is found, the class is used as context.<br /> <br /> '''Note:''' This behavior does not play nice with states extending another state with a different name. See [[#Examples|examples]] below.<br /> <br /> ==Global==<br /> The keyword '''Global''' is used to call the most-derived non-state version of a function. If the instance is not in any state, this has the same effect as calling that function directly.<br /> <br /> Syntax:<br /> '''Global.'''functionname'''('''''[parameters]''''')'''<br /> <br /> ==Examples==<br /> For this example we'll use two classes. The parent class A:<br /> &lt;uscript&gt;<br /> class A extends Actor;<br /> <br /> function F(); // doesn't override anything<br /> <br /> state X<br /> {<br /> function F(); // overrides A.F<br /> }<br /> &lt;/uscript&gt;<br /> ...and the derived class B:<br /> &lt;uscript&gt;<br /> class B extends A;<br /> <br /> function F(); // overrides A.F<br /> <br /> state X // extends A.X<br /> {<br /> function F(); // overrides A.X.F<br /> }<br /> <br /> state Y extends X<br /> {<br /> function F(); // overrrides B.X.F<br /> }<br /> &lt;/uscript&gt;<br /> Now we have an instance of class B that wants to call various implementations of function F():<br /> &lt;uscript&gt;<br /> F();<br /> Global.F();<br /> Super.F();<br /> Super(A).F();<br /> &lt;/uscript&gt;<br /> Which implementation those four lines will actually call depends on where exactly they are in class B.<br /> <br /> ===Case 1: No state===<br /> If the instance of B is not in any state, the following implementations are called:<br /> #B.F()<br /> #B.F()<br /> #A.F()<br /> #A.F()<br /> No surprises here.<br /> <br /> ===Case 2: State X===<br /> If our B instance is in state X, the following implementations are called:<br /> #B.X.F()<br /> #B.F()<br /> #A.X.F()<br /> #A.X.F()<br /> Just as expected.<br /> <br /> ===Case 3: State Y===<br /> If the B instance happens to be in state Y, things look a bit different:<br /> #B.Y.F()<br /> #B.F()<br /> #B.X.F()<br /> #A.F()<br /> Wait, &lt;code&gt;Super(A).F()&lt;/code&gt; calls the non-state version of F() in class A? Unfortunately this is a limitation of the advanced version of the '''Super''' keyword. It doesn't actually check state inheritance, but only looks for a state with the same name in the specified class.<br /> <br /> ===Case 4: Code in state X after switch to state Y===<br /> This one is a bit tricky and something you have to watch out for, because it may cause you some trouble. Pretend we have the following function in state B.X:<br /> &lt;uscript&gt;<br /> function G()<br /> {<br /> GotoState('Y');<br /> F();<br /> Global.F();<br /> Super.F();<br /> Super(A).F();<br /> }<br /> &lt;/uscript&gt;<br /> The code being executed may still be in state X, but the instance has already switched to state Y. Which versions of F() will be called?<br /> #B.Y.F()<br /> #B.F()<br /> #A.X.F()<br /> #A.X.F()<br /> The second one was expected, '''Global''' doesn't care about the state and just calls the global version. Numbers 3 and 4 were kind of expected as well, the code is in state X, so '''Super''' calls would be relative to that.<br /> <br /> The first one is a bit surprising, though. Code in one state calls a function in a different state! Similar things would happen if you switch from the global state to X or Y or from X or Y to the global state, a simple call to F() will call the new state's implementation.<br /> <br /> Be very careful when calling functions after a state switch, the compiler doesn't check for this case. If you try to call a function that is available in the old state, but not in the new state, the game will crash mercilessly!<br /> <br /> {{navbox unrealscript}}</div> Wormbo