Dear forum users! In compliance with the new European GDPR regulations, we'd just like to inform you that if you have an account, your email address is stored in our database. We do not share your information with third parties, and your email address and password are encrypted for security reasons.

New to the forum? Say hello in this topic! Also make sure to read the rules.

Scripting 20 - Victory condition

A smaller forum with a few tutorials how to get started with the ScriptAPI.
Forum rules
By using the forum you agree to the following rules.
Post Reply
User avatar
Gurt
Lead Programmer
Lead Programmer
Posts: 1884
Joined: Sun Feb 28, 2016 3:22 pm
Title: Lead programmer
Started SFD: Made it!
Location: Sweden
Gender:
Age: 34

Scripting 20 - Victory condition

Post by Gurt » Sun Aug 25, 2019 12:41 pm

Scripting 20 - Victory condition

Scripting in SFD assumes you have a fair knowledge of C#.

The following code demonstrates how to trigger defeat/victory for different map types based on alive players.

Code: Select all

public void OnStartup()
{
	if (Game.GetMapType() == MapType.Challenge)
	{
		// Challenge maps have AutoVictoryConditionEnabled true by default and will play out like a versus game. 
		// Set it to false if you want to control when the user has completed/failed the challenge.
		Game.AutoVictoryConditionEnabled = false;
	}
}

// Example code to trigger victory/defeat for all map types through code if you need more advanced rules than a GameOverTrigger.
public void CheckGameOver()
{
	switch(Game.GetMapType()) 
	{
		case MapType.Custom:
		{
			// Custom map types requires you to increase the score of the individual users
			bool victory = false;
			foreach(IUser user in Game.GetActiveUsers()) 
			{
				IPlayer plr = user.GetPlayer();
				if (plr != null && !plr.IsDead)
				{
					user.IncreaseScore();
					victory = true;
				}
			}
			Game.SetGameOver((victory ? "Victory!" : "Defeat"));
		}
		break;
		case MapType.Survival:
		{
			// Survival map types are automatic. Just call Game.SetGameOver(). 
			// If anyone is alive the survival will progress to the next wave.
			// If everyone is dead the survival will restart at the current wave if any retries remains.
			Game.SetGameOver();
		}
		break;
		case MapType.Challenge:
		{
			// For challenges AutoVictoryConditionEnabled is by default true which means once every hostile enemy is dead the challenge is completed.
			// If it's false you need to set Game.ChallengeCompleted and call Game.SetGameOver() manually.
			bool victory = false;
			foreach(IUser user in Game.GetActiveUsers()) 
			{
				IPlayer plr = user.GetPlayer();
				if (plr != null && !plr.IsDead)
				{
					victory = true;
					break;
				}
			}
			Game.ChallengeCompleted = victory;
			Game.SetGameOver();
		}
		break;
		case MapType.Campaign:
		{
			// For campaigns you must decide when to progress to the next part, when the campaign is completed or if the users need to restart the current part.
			bool victory = false;
			foreach(IUser user in Game.GetActiveUsers()) 
			{
				IPlayer plr = user.GetPlayer();
				if (plr != null && !plr.IsDead)
				{
					victory = true;
					break;
				}
			}

			if (!victory)
			{
				// Restart current part again
				Game.SetCampaignMapPart(Game.CampaignCurrentMapPartIndex);
			} 
			else 
			{
				if ((Game.CampaignCurrentMapPartIndex + 1) == Game.CampaignTotalMapParts) 
				{
					// campaign won!
					Game.SetGameOver("The End");
				} 
				else 
				{
					// Go to next part
					Game.SetCampaignMapPart(Game.CampaignCurrentMapPartIndex + 1);
				}
			}
		}
		break;
		case MapType.Versus:
		{
			// AutoVictoryConditionEnabled is true by default for versus maps. No need to do anything.
		}
		break;
	}
}
2 x
Gurt

Post Reply