This forum is locked and will eventually go offline. If you have feedback to share you can find us in our Discord channel "MythoLogic Interactive" https://discord.gg/nECKnbT7gk

Forum rules

Parts of code executed sometimes

Here you can find answered ScriptAPI topics.
Forum rules
By using the forum you agree to the following rules.
Locked
User avatar
JakSparro98
Superfighter
Superfighter
Posts: 530
Joined: Fri Jul 15, 2016 7:56 pm
Started SFD: PreAlpha 1.0.5
Location: Rome, Italy
Gender:
Age: 27

Parts of code executed sometimes

Post by JakSparro98 » Thu Oct 06, 2016 4:31 pm

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.
0 x

User avatar
Gurt
Lead Programmer
Lead Programmer
Posts: 1887
Joined: Sun Feb 28, 2016 3:22 pm
Title: Lead programmer
Started SFD: Made it!
Location: Sweden
Gender:
Age: 36

Post by Gurt » Mon Oct 10, 2016 6:44 pm

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.
1 x
Gurt

Locked