Synchronization? between different scripts

Hello everyone

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?

Ps: I’m writting the scripts using C#.

Thanks alot
Lee

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?

Hi there,

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.

I think I see some holes on your logic

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.

  1. In the 2 player scripts, place a static variable playerHealth
  2. In a 3rd script place a bool haveWinner and make it false in the Start(). Also create a function that will receive a winner parameter
  3. In the 2 player script Update, check if health < 1 and if so, set the static bool to true and send the winner parameter to the win function
  4. In the 3rd script you can now control the animation that must be played using the receiving parameter and perform some other functions

Should work then

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.

ok, i get it now, sorry.

What are you using to trigger the changoing of the variable from 0 to 1 or -1?

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.