C
C#5w ago
Kagano

How can i ref in a lambda

I want that the you can overgive a float value in this method which by running the action will overwrite it
public Tween SetProperty(ref float value, float time, float delay, float start, float end)
{

Action<float> lerp = (past) =>
{
value = Raymath.Lerp(start, end, past);
};
}
public Tween SetProperty(ref float value, float time, float delay, float start, float end)
{

Action<float> lerp = (past) =>
{
value = Raymath.Lerp(start, end, past);
};
}
17 Replies
Jimmacle
Jimmacle5w ago
so you want past to be ref? or value
Kagano
Kagano5w ago
no, value i want that the user can overgive a variable value which will set to it, by every executing of the action
Jimmacle
Jimmacle5w ago
can you not define a delegate with a ref argument in this situation?
Kagano
Kagano5w ago
idk i dont tried
Jimmacle
Jimmacle5w ago
i mean you can, it's a matter of whether that works for you example from one of my projects
public delegate T LabViewReadDel<out T>(ref LabViewMessageReader reader);
public int[] ReadArrayInt() => ReadArray((ref LabViewMessageReader reader) => reader.ReadInt());
public delegate T LabViewReadDel<out T>(ref LabViewMessageReader reader);
public int[] ReadArrayInt() => ReadArray((ref LabViewMessageReader reader) => reader.ReadInt());
Kagano
Kagano5w ago
the variable is always 0
//runs in the update function
float useless = 0;
property.Lerp(time,ref useless);
//

public Tween SetProperty(float value, float time, float delay, float start, float end)
{
TweenProperty property = new TweenProperty(time, delay);
property.OnLerp += (float past, ref float f) => Lerp(past,ref value,start,end);
tweenElements.Add(property);
return this;
}

public class TweenProperty(float time, float delay, Action<float> action = default)
{
public readonly float Delay = delay;
public readonly float Time = time;
public Action<float>? Action = action;

internal event Property OnLerp;

public void Lerp(float past,ref float value)
{
OnLerp?.Invoke(past, ref value);
}
}
//runs in the update function
float useless = 0;
property.Lerp(time,ref useless);
//

public Tween SetProperty(float value, float time, float delay, float start, float end)
{
TweenProperty property = new TweenProperty(time, delay);
property.OnLerp += (float past, ref float f) => Lerp(past,ref value,start,end);
tweenElements.Add(property);
return this;
}

public class TweenProperty(float time, float delay, Action<float> action = default)
{
public readonly float Delay = delay;
public readonly float Time = time;
public Action<float>? Action = action;

internal event Property OnLerp;

public void Lerp(float past,ref float value)
{
OnLerp?.Invoke(past, ref value);
}
}
My button
public int test;

//in the constructor
tween.SetProperty(test, 10, 0, 5, 10);

//runs in the update function
Logger.Warn(test+"");


//output
[Test.RainbowButton:78][21:42] [WARN]: 0
public int test;

//in the constructor
tween.SetProperty(test, 10, 0, 5, 10);

//runs in the update function
Logger.Warn(test+"");


//output
[Test.RainbowButton:78][21:42] [WARN]: 0
Kagano
Kagano5w ago
i forgot to ref value in the parameter, but same problem
No description
reflectronic
reflectronic5w ago
test is 0 because you did not pass it by ref nothing is editing it
Kagano
Kagano3w ago
No description
reflectronic
reflectronic3w ago
yes, the parameter needs to be a ref parameter too
Kagano
Kagano3w ago
here we go again
No description
reflectronic
reflectronic3w ago
ok, so, this will not work well, it will have to be done differently
Kagano
Kagano3w ago
i don't had a idea here is the class
reflectronic
reflectronic3w ago
do you read from value inside of Lerp, or do you only write to it
Kagano
Kagano3w ago
i just try rewrite it
private void Lerp(float past, ref float value,float start, float end)
{
value = Raymath.Lerp(start,end, past);
}
private void Lerp(float past, ref float value,float start, float end)
{
value = Raymath.Lerp(start,end, past);
}
reflectronic
reflectronic3w ago
what you can do instead is pass an Action<float> it won't look that great, but, it would work
SetProperty(x => this.Whatever = x, a, b, c);
// ...

Tween SetProperty(Action<float> setValue, ...)
{

property.OnLerp += (past) => setValue(Lerp(...));
}
SetProperty(x => this.Whatever = x, a, b, c);
// ...

Tween SetProperty(Action<float> setValue, ...)
{

property.OnLerp += (past) => setValue(Lerp(...));
}
Kagano
Kagano3w ago
yeah that could work :/ but i wanted to do that you can via parameter tell which value should set looks like there isn't a good way
Want results from more Discord servers?
Add your server
More Posts