Page 1 of 1

Parts of code executed sometimes

Posted: Thu Oct 06, 2016 4:31 pm
by JakSparro98
The script works as intended in editor mode but when I test in a play, the script continues executing code but discontinuously.

Here is the script, activate it by pressing ALT and D, if you keep testing you will notice the counter will still monitoring the script, but sometimes the jump isn't applied.

Code: Select all

Events.UpdateCallback m_updateEvent = null;
bool Activated=true;
Dictionary<int, Player> Plist = new Dictionary<int, Player>();

public void OnStartup(){
m_updateEvent = Events.UpdateCallback.Start(OnUpdate);
}

class Player{
public bool toggle=true;
public int count=2;

public void Activate(int x){
IObjectTimerTrigger timer= (IObjectTimerTrigger) Game.CreateObject("TimerTrigger");
timer.CustomID=x.ToString();
timer.SetIntervalTime(500);
timer.SetScriptMethod("Reset");
timer.Trigger();
}
}

public void Reset(TriggerArgs arg){
IObjectTimerTrigger timer= (IObjectTimerTrigger) arg.Caller;
Plist[Int32.Parse(timer.CustomID)].toggle=true;
timer.Remove();
}



 public void OnUpdate(float elapsed) 
 {
   foreach(IPlayer temp in Game.GetPlayers())
   {
	if(!temp.IsBot)
	{
		if(!Plist.ContainsKey(temp.UniqueID))
		{
		Player x=new Player();
		Plist.Add(temp.UniqueID,x);
		}
Game.ShowPopupMessage(""+Plist[temp.UniqueID].count);

		if (temp.IsWalking && temp.IsBlocking && Plist[temp.UniqueID].toggle && Plist[temp.UniqueID].count>0 ) 
		{
		Plist[temp.UniqueID].toggle=false;
		temp.SetLinearVelocity(new Vector2(0f,10f));
		Plist[temp.UniqueID].count--;
		Plist[temp.UniqueID].Activate(temp.UniqueID);
		}


		if (temp.IsOnGround && Plist[temp.UniqueID].toggle)
			Plist[temp.UniqueID].count=2;

		if (!temp.IsOnGround && Plist[temp.UniqueID].count==2)
			Plist[temp.UniqueID].count=1;
   }
  }
}
If this is a mistake of mine please tell me why in the editor test (f5) it works well and not in the real play.

Re: Parts of code executed sometimes

Posted: Mon Oct 10, 2016 6:44 pm
by Gurt
SetLinearVelocity() on players isn't recommended through ScriptAPI alone as the velocity can be overriden depending on what the player is doing at the moment. You can force the positional update better by calling SetWorldPosition() on the player before/after applying the velocity.