- Home /
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 !
Answer by Kacheek · May 09, 2014 at 06:18 PM
finally got a working solution for it ! delaying variables is actually easy :D 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 ! :)
and for the position update i was talking about... i only send the position twice a second but want a 0,1 second delay so its even easier than that ...
if(newpositioncamein)
{
incometime = Time.time;
}
if(incometime+0.1f <= Time.time)
{
transform.position = newposition;
}
Answer by Paximillian · May 08, 2014 at 12:07 PM
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.
"and not only for 1 second it should be applied the whole game through maybe even for hours if we got some addicts xD"
unfortunaly its not that easy :/ already thought of this but i meant with what i wrote that it in fact doesn't repeat itself
to be more clear i want to use this in networking the position of my object to others. ive wrote a good networking script already but i need the variable to be delayed by 0,1 sec to compensate lag
on the reciever side it should look like this
transform.position = recievedt position from the other client 0,1 sec ago
Well, there are ALOT of ways on compensating for lag. If this one is not something you think you can achieve, just try a different approach. Ins$$anonymous$$d of how you work this, which I suppose is similiar to interpolation, since I still don't completely understand what you do with this delay data, you can try extrapolating. Use the player's local velocity, position and direction to predict where they will be the next frame, then when you receive new data from the host, compare it and fix accordingly, if needed.
Can't you send the current position of each of those things along with the time passed since the last update, and then they'll locally Lerp the rotation? Same goes for the buttons, every time you send an update send the amount of time passed between it and the last update, and just wait on the client side until that much time passes before processing the button actions.
Well, you can always convert it to a byte and send it that way using bitwise operators, but sure, if you think you found a better solution, I'll be glad to hear how it turned out =)
Well, one point at a time: If the size of the data is a problem to you, like I said, using the binary of a byte type could be useful, and then you can store a number of variables in a single variable and pass that through the network, breaking it up again on receiving. I'm not going to go thoroughly on this subject here right now, but Google should have plenty of material on it. Considering you've never heard of bitwise operators though, it might be worth a read. It's basically manipulating the individual binary bits of a numeric value.
Secondly, there seems to be a confusion here about arrays. An array is not a class, and I don't think it's mentioned in the C# documentation of Unity because it really has nothing to do with Unity specifically, but with the language as a whole. $$anonymous$$SDN can help you with that.
Thirdly, if the approach with the time is the one that you want to take, then you're doing it a little wrong:
networkView.RPC("update$$anonymous$$ovement", RPC$$anonymous$$ode.Others,
myTransform.position, delayTime);
With delayTime being a float that tells it how much delay do you want on the client, and then use like a Coroutine or something to do the actual delay on the client.
I'm sorry I can't go more in depth about this right now, since I'm a little busy, but you should be able to find all of what I referenced on Google pretty easily. Just know that Coroutines in C# are a bit of a pain =S
Good luck.
Your answer
Follow this Question
Related Questions
How do I make it so that it wont edit a variable again for a certain ammount of time? C# 3 Answers
Yielding for a changing amount of time? 2 Answers
Delay Advertisements when switching between scenes? 0 Answers
How to carry over variable (transform.position) and use it as Vector3 for instanitating a prefab 2 Answers
How can I add a "delay" before switching back to realtime? (Read description for more detail, sorry) 1 Answer