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

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.
Locked
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

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

Locked