This is a C variable referencing question in Unity and not a gameobject reference question.
I need to get the address of newVar, as in &newVar.
Is this possible in Unity? I have tried a number of searches and cannot find an answer.
There is documentation about how to do this in C but I need to do it in Unity.
I've never used Archimatix but looking at the manual I see online, it looks like this will work. In c#, lambdas capture variables by reference, so you can have the params read the result from a lambda to get an up-to-date variable.
public class ParamUpdater: MonoBehaviour {
[SerializeField] AXModel model;
List<AXParameter> params;
List<Func<float>> paramGetters;
// Use this for initialization
void Awake () {
// Establish refernces to AXModel parameters.
Params = new List<AXParameter>();
if (model != null)
{
Params.Add(model.getParameter("Engine.Param0"));
Params.Add(model.getParameter("Engine.Param1"));
// add more params here
}
paramGetters = new List<Func<float>>();
// create default getters
for (int i = 0 ; i < Params.Count ; i++)
{
paramGetters.Add(()=>0f);
}
}
public void ConfigureParamGetter(int index, Func<float> getter)
{
paramGetters[index] = getter;
}
public void Recalculate()
{
for(int i = 0 ; i < Params.Count ; i++)
{
float curVal = paramGetters[i]();
// Maybe a different method would be better?
// online API is very lacking...
Params[i].initiatePARAMETER_Ripple_setFloatValueFromGUIChange(curVal);
}
model.autobuild();
recalculate();
}
}
And then for example in a class where a float of interest lives, call ConfigureParamGetter with a Func that returns the variable. As mentioned before, the capture is done by reference, so it will always be up to date when called in ParamUpdater.
public class ImportantClass: MonoBehaviour
{
[SerializeField] ParamUpdater paramUpdater;
float importantFloat;
// Use Start to call after ParamUpdater initializes in Awake
void Start()
{
paramUpdater.ConfigureParamGetter(0, ()=>importantFloat);
}
// for example
void Update() { importantFloat = Time.time; }
// called whenever
void DoRecalculate()
{
paramUpdater.Recalculate();
}
}
Let me know if I made any syntax errors or any other problems, I can try and fix 'em.
You require Pointers and the unsafe code keyword.
Classes in C# are already references, so not much use converting to pointers. IE a GameObject is already a reference type, and it already works like a pointer.
For other things, such as unmanaged types, you might use unsafe code and pointers.
So you can do things such as char* pc = &c;
Also remember to "Allow unsafe code" with File > Build Settings... => Click Player Settings => Code Setting => Allow 'unsafe' code.