- Home /
Possible to lock a variable like moveSpeed?
In the 3D Platform Tutorial there is a ThirdPersonController script that utilizes a private variable called "moveSpeed". I have made the variable just a regular var by deleting "private" from the line of code so that I can use it in the ThirdPersonStatus script. I am doing this because I would like to achieve a completely motionless character upon respawn, otherwise if the character is moving upon death he carries over the motion values when respawned. In other words, if the character is flying through the air or is running when killed, he respawns with that motion still going and then comes to a stop so long as you aren't pressing anything on your input device. So again, I was trying to manipulate the moveSpeed variable to "0" when he respawns, and I have achieved this by using these lines of code:
var controller: ThirdPersonController;
controller = GetComponent(ThirdPersonController);
controller.moveSpeed = 0.0;
this is used in the function Die() of the ThirdPersonStatus script. My question goes beyond this though. I was wondering if for some reason ever that I want to lock a variable in another script, that is, keep it from changing despite what you press on your input device, how would I do that? I understand there is a "lock" operator in Javascript but I do not know how it is used. I was thinking along the lines of this:
lock.controller.moveSpeed;
//or
controller.moveSpeed.lock;
but when I use either of these (not at the same time, of course) I get this error: "Expressions in statements must only be executed for their side-effects." I do not know what this means, I am still learning. I am just wondering because it would be good to know how to disable the moveSpeed temporarily, if I wanted, for what ever purpose related to the game it's used in. I've also tried using
controller.moveSpeed.enabled = false;
but that doesn't seem to work either. What can I use to "lock" a variable to what ever value at any given time? Thank you for your time!
Answer by zyzyx · Aug 28, 2012 at 12:12 PM
I'm not sure how it is done in UnityScript.
In C# you could use a bool variable and a property to "lock" your variable.
public bool LockMoveSpeed;
private float moveSpeed;
public float MoveSpeed
{
get { return value; }
set
{
if (!LockMoveSpeed)
moveSpeed = value;
}
}
I'm new to program$$anonymous$$g and game design and I've only been fooling around with Java the past week or so, but I appreciate your feedback! This is a great idea I didn't think of before, to make the value a condition. Can C#, Java and Boo work together on the same game objects, or do all the scripts on a given object have to be same i.e. all Java, or all C# etc?
Answer by Dasherz · Aug 28, 2012 at 12:50 PM
why not just set movespeed to 0 upon death or reset all variables???
yeah i know, I did that and it works great, but my question is say I want to use something that the character picks up or triggers or whatever and the speed he had when going into that trigger or upon picking that item up is then locked? So like if I pick up an item, and I'm traveling at a moveSpeed of 5.68397 or whatever, then that number gets locked until I drop that item, remove it or collide with another trigger or something. The thing is when you just reset the value to zero, you can then press forward on your keyboard or joystick and the value changes again. $$anonymous$$y thought was to have the value locked so that input devices no longer have any effect, you know? But I think "zyzyx" brought up a good point that I could just make the value a condition so that it remains locked until the condition is expired or another condition overrides it or whatever. Anyway, thanks for the feedback, very much appreciated!
Your answer
Follow this Question
Related Questions
Is there any performance or memory usage diffrence? 0 Answers
Moving platform not taking mesh with it 0 Answers
3D Platformer Tutorial - 5 Answers
3D Platform Tutorial Problem 1 Answer
Lerpz tutorial problems 2 Answers