C
C#7mo 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
Jimmacle7mo ago
so you want past to be ref? or value
Kagano
KaganoOP7mo 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
Jimmacle7mo ago
can you not define a delegate with a ref argument in this situation?
Kagano
KaganoOP7mo ago
idk i dont tried
Jimmacle
Jimmacle7mo 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
KaganoOP7mo 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
KaganoOP7mo ago
i forgot to ref value in the parameter, but same problem
No description
reflectronic
reflectronic7mo ago
test is 0 because you did not pass it by ref nothing is editing it
Kagano
KaganoOP7mo ago
No description
reflectronic
reflectronic7mo ago
yes, the parameter needs to be a ref parameter too
Kagano
KaganoOP7mo ago
here we go again
No description
reflectronic
reflectronic7mo ago
ok, so, this will not work well, it will have to be done differently
Kagano
KaganoOP7mo ago
i don't had a idea here is the class
reflectronic
reflectronic7mo ago
do you read from value inside of Lerp, or do you only write to it
Kagano
KaganoOP7mo 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
reflectronic7mo 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
KaganoOP7mo 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