I’m developing a fighting game which using a variable to decide which side to take the next action, the player or the rival. The rule is that when the variable is 1, the player will take a move and play a piece of animation. While the rival will do if the variable is -1. After playing the move animation, the variable will be reset to 0 waiting for next round accessing. Both the player character and the rival carry a script to detect the value of this variable every now and then.
I’ve tried to put the code in OnGUI or Update function, but sometimes the animations won’t be played or sometimes the animations will be played more than once then the character’s health will be reduced until it die. I think this problem may be caused by the different frequency the two functions are called. It’s like the common problem which happens in a multi-thread program. Does anyone have any idea about how to fix this kind of problem? Maybe another function can be used to locate the variable changing code?
You should not have the need if your logic flow is correct. Can you maybe post some code snippets? Are you using 2 different scripts to check the variable state or are you controlling both players from one script?
Thanks for your patience. Actually I use 3 scripts to control the variable, one is to set its value and two are checking it and decide what to perform. The two scripts are on the player and the rival respectively.
The two scripts which are checking the variable are like the sample below. In the FixedUpdate function, the winner variable will be check all the time to decide what to do.
void FixedUpdate () {
if (winner == 1)
{
//Play animation
}
else if (cc.winner == -1)
{
//Reduce health
winner = 0;
}
And the sample of the one script which aims at changing the variable is here.
void FixedUpdate () {
if (condition1)
{
winner = 1
}
else if (condition2)
{
winner = -1;
}
If you have any new ideas, please do not hesitate to reply me.
Thanks.
For example, on the first script, if winner is not -1, it could change to 0. The other script is unconditionally changing the winner values. So you have a “race” condition, and results are unpredictable.
Thanks Chubbspet. But I don’t think you understand my logic flow. The winner is a variable to decide every single hit. For example, if the winner is 1 then the rival character will hit the player once. After that the variable will be reset and a new loop will start. It’s not a normal fighting game but a complicated one. So the help you provided is a little bit away from the point while i’m still appreciated about it. Now I have a new query, is there any method can make these scripts work like a multi-thread program? When the variable is being called no one else can call it utill it’s released. Do I have to write a transaction or there’s an easy way? Thanks again.
Just a timmer, if the player presses the correct button during the countdown period, the player will have the right to hit. If not so the rival will do. Do you think that combining these scripts together will work better???
It might…try that first and let know if any joy? It might also give you better perspective what teh flow of your pseudo code is and if there are any loopholes. Maybe also post that combined code.