Delaying a dynamic Variable(transform.position for Ex.)

Hello there
since its a bit complex and i dont think its neccesary i wont explain why i would want this but i really need it…

the question is :

how do i make a variable have the exact value another variable had lets say 1 second ago?

lets say the variable changes every 0,1 seconds to a random number

variableCurrent =

0,1 sec = 52
0,2 sec = 25
0,3 sec = 72
0,4 sec = 1
0,5 sec = 6
0,6 sec = 99
0,7 sec = 11
0,9 sec = 350238
1,0 sec = 78

and now i want “variableDelayed” to be

1,1 sec = 52
1,2 sec = 25
1,3 sec = 72
1,4 sec = 1
1,5 sec = 6
1,6 sec = 99
1,7 sec = 11
1,9 sec = 350238
2,0 sec = 78

and not only for 1 second it should be applied the whole game through maybe even for hours if we got some addicts xD

the things i thought of were using Invokerepeating but this wont work since you cant pass the variable through it you could only store the variable into another but since the variable changes faster than the delay is it will always have the wrong number assigned

InvokeRepeating("setdelayedVariable",1.0f,0);
varTemp = varCur;

void setdelayedVariable ()
{
varTemp = varDelay;
}

also keep in mind that i need a solutation that doesn’t only work if the curvar changes every 0,1 seconds and the delay is 1 sec

i would need this for different sorts of variables floats,bools,position with the position thing the curvar will be the transform position which will change ever frame and its speed wont be linear so just adding the speed it walks in 1 sec doesnt work too

you see ive already thought alot about this…

i really hope you guys can help me with that

a lot of thanks in advance !

The question wasn’t really very clear, but from what I gathered, the change only happens a set number of times and is then repeated in the same order every time?

Why not use something like an object pool? Initiate all the random values at the Start() function and save them somewhere, since you know how many times it needs to be changed before repeating, you know how many values to generate.

After you have the values, you can just scroll through them using an array or a list, and once you reach the last value, set the position of the value to use back to the first one.

finally got a working solution for it ! delaying variables is actually easy :smiley: as long as you dont have an infinite amount of values you can assign this variable to such as a float or a Vector3 … those are harder but for example a bool you can only have false and true right ? well and null but lets leave the null be null and not care about it…

also : even if in this exampe the delaytime is 1 im using 0.1 of course since i want the delay 0.1 seconds

the nice and easy code :

bool LeBool = false;

float delaytime = 1f;

if(whatever happens you want it to be delayed)
{
if(LeBool = true)
InvokeRepeating("makebooltrue", delaytime, 0);
if(LeBool = false)
InvokeRepeating("makeboolfalse", delaytime, 0);
}

void makebooltrue()
{
LeBool = true;
}

void makeboolfalse()
{
LeBool = false;
}

and for the axis input (W,A,S,D) you could just do it with bools too but since im using Input.GetAxis ( = float ) lets just use a float variable for it since it will only be -1, 0 or 1 we just need three voids again i know i know if you would use a joystick or gamepad it can be 0,92358932758923759 or something too but i don’t want to support joysticks anyway so i round the axis input beforehead anyway…

float Vinput;

float delaytime = 1f;

if(whatever happens you want it to be delayed)
{
if(Vinput = 1)
InvokeRepeating("Vinput1", delaytime, 0);
if(Vinput = 0)
InvokeRepeating("Vinput0", delaytime, 0);
if(Vinput = -1)
InvokeRepeating("Vinput-1", delaytime, 0);
}

void Vinput1()
{
Vinput = 1;
}

void Vinput0()
{
Vinput = 0;
}

void Vinput-1()
{
Vinput = -1;
}

but thank you Paximillian anyway ! i sure will look into using the binary of a byte even if this whole thing frustrated me a lot and used up a lot of titme im still happy since i could learn a lot by trying to solve it ! ive tryed out so many new things that i now know how to use ! :slight_smile: