Executing SQF Without Allocating Memory
First, we need a buffer to write our script into. Thankfully any existing GameDataCode variable contains an ArmaString pointer to its value. So when I run str(var) this ArmaString is what we see in the output.
As of Arma 2.04 that can be found here:
Using a variable in the uiNamespace such as BIS_fnc_diagWiki, we have a large buffer we can write with our script into. This string, however, is not used for GameDataCode execution. We can’t simply modify this and stick the GameDataCode pointer into onEachFrame. So we need to find a GameDataString variable that can be used. I will leave finding a string variable in the uiNamespace as an exercise for the reader.
Let’s jump right into the C++ code for this and explain what is being done step by step.
Step one is to find our target GameDataCode and GameDataString pointers in the uiNamespace.
Second, we need to identify the internal ArmaString value pointers for these objects. One of these two values will be where our attacking sqf code is written to.
Next, we need to be on the safe side and ensure our ArmaString buffer is large enough for our script (otherwise we need to pick a new SQF variable).
Now comes a very important step. When we write our attacking script we need to add “filler” sqf that will ensure our code matches the size of our original string. If we do not do this, Arma will automatically resize this string and bad things will happen in future steps. You may notice the minimum_extra_chars variable from above, this comes into use here.
Above we assumed that our GameDataCode buffer would be larger, but that may not always be the case. If our GameDataString is larger, we can just write to that.
However, if our GameDataCode is larger we need to swap the internal ArmaString pointers. This is like doing stringvariable = str(codevariable); (kinda).
Finally, we get to write our attacking SQF to the GameDataString’s (possibly new!) ArmaString pointer. We need to read out the original code for future steps here.
Okay! Time for a break. At this point we have a string in SQF that contains our attacking code without allocating any memory. We’ve achieved part of our goal. Next we will execute this code using the OnEachFrame method Douggem outlined.