Issue using .Contains with application.loadedlevel/loadlevel - unity3d

I'm working on a script that returns to the correct scene when pressing escape.
In the DataUpgrade, Level Select, TransferData(x) it should return to StartMenu, in StartMenu it should close the game and in a Level(x) it should return to TransferData(x).
The following script has been made with help from FunctionR.
#pragma strict
import System;
function Update () {
if (Input.GetKey ("escape"))
{
var level : String = Application.loadedLevel.ToString();
//var transferData = "TransferData";
if(level == "StartMenu"){
Application.Quit();
}
else if(level == "DataUpgrade"){
Application.LoadLevel("StartMenu");
}
else if(level == "Level Select"){
Application.LoadLevel("StartMenu");
}
else if(level.Contains("TransferData"))
Application.LoadLevel("StartMenu");
else if(level.Contains("Level"))
Application.LoadLevel (Application.loadedLevel - 1);
//trying this out but isn't working either
/*else if( transferData in level)
Application.LoadLevel("StartMenu");
else if("Level" in level)
Application.LoadLevel (Application.loadedLevel - 1);*/
}
}
i'm not sure why it's not working, the scenes have the correct names and have also been added to build settings.
Thanks in advance

Quick Fix
This is how you fix it, and you don't need the for loop. I use Contains() to do the hard work.
import System;
function Update ()
{
if (Input.GetKey ("escape"))
{
var level : String = Application.loadedLevel.ToString();
if(level == "StartMenu")
Application.Quit();
else if(level == "DataUpgrade")
Application.LoadLevel("StartMenu");
else if(level == "Level Select")
Application.LoadLevel("StartMenu");
else if(level.Contains("TransferData")
Application.LoadLevel("StartMenu");
}
}
Smart Fix
You can also be smart and take advantage of the OR operator to reduce your related if statements.
else if(level == "DataUpgrade" || level == "Level Select" || level.Contains("TransferData")
Application.LoadLevel("StartMenu");
Sneaky Fix
You can also be sneaky since all other cases, except one, takes you back to StartMenu. You can have an else that guarantees a return to StartMenu.
if(Application.loadedLevel == "StartMenu")
Application.Quit();
else
Application.LoadLevel("StartMenu");

Replacing this line:
var level : String = Application.loadedLevel.ToString();
with
var level = Application.loadedLevelName;`
fixed the problem, everything is working now

Related

JS timeout causes eval exception

For some reason, one of my JS files is triggering an unsafe-eval Content Security Policy violation on my site. I thought this odd because there is no eval() anywhere in the file. The error happens on the following line:
setTimeout(callSpecific(), (lengthMF * (number.length + 2)));
The only thing I can see here is the arithmetic on the RHS that sets the timeout value. So, I tried:
setTimeout(callSpecific(), (parseInt(lengthMF) * (parseInt(number.length) + 2)));
Same thing. The variables themselves are not even strings - they are defined as:
var lengthMF = 150;
var number = ""; // yes, this is a string but number.length is not!
Why is this triggering a CSP violation? I have other setTimeout()s on the page and this seems to be the only problematic one. The weird thing is replacing the arithmetic expression temporarily with a constant (e.g. 50) does not cause the issue to go away.
If it's necessary, callSpecific() looks something like this:
function callSpecific() {
if (number == 0) {
operatorRing();
} else if (number.length == 2 && number.charAt(1) == 0) {
playReorder();
} else if (number.length == 3) {
//
} else if (number.length <7 || number.length > 11) {
//
} else if (number.length == 11 && (number.charAt(4) == 1 || number.charAt(4) == 0)) {
//
}
}

repl.it question "lovesPizza" in control flow coding giving me trouble

It says that I'm missing a ";" on line 5 else(typeOfPizza === "olives"){
function exerciseThree(typeOfPizza){
let lovesPizza;
if (typeOfPizza === "pepperoni"){
return "true";}
else(typeOfPizza === "olives"){
return "false";
}
Where you are using a condition for else it actually needs to be else if like this:
function exerciseThree(typeOfPizza){
let lovesPizza;
if (typeOfPizza === "pepperoni"){
return "true";}
else if (typeOfPizza === "olives"){
return "false";
}
}
You cannot have else(condition) the else if statement needs to be if(condition) {} else {} if you require a condition for the else then you need to do if(condition) {} else if(condition) {}

I am trying to update my for loop for Swift 3 and I can't figure it out. I keep getting errors

This is my original function in Swift 2:
// check on winning combinations
func checkWinnerMove(){
for var i = 0; i<winningCombinations.count && !isWinner;i += 1 {
if gameState[winningCombinations[i][0]-1] == activePlayer &&
gameState[winningCombinations[i][1]-1] == activePlayer &&
gameState[winningCombinations[i][2]-1] == activePlayer{
isWinner = true;
}else{
isWinner = false;
}
}
}
I have changed it to this:
// check on winning combinations
func checkWinnerMove(){
for i in 0 ..< winningCombinations.count && !isWinner{
if gameState[winningCombinations[i][0]-1] == activePlayer &&
gameState[winningCombinations[i][1]-1] == activePlayer &&
gameState[winningCombinations[i][2]-1] == activePlayer{
isWinner = true;
}else{
isWinner = false;
}
}
}
But keep getting a error when I add the
&& !isWinner
statment in the for-in loop. The error I get is:
No '..<' candidates produce the expected contextual result type 'Bool'
Any suggestions? Thank You!
Instead of forcibly trying to rewrite your original C-style for loop, consider what you're trying to achieve and attempt to re-write it in "native" Swift from scratch. How about breaking out of your loop once your true condition is met, instead of keeping it in the loop signature? E.g.
for i in 1...5 {
print(i)
if i == 3 { break }
} // 1 2 3
Applied to your example
func checkWinnerMove()
isWinner = false
for i in 0 ..< winningCombinations.count {
if gameState[winningCombinations[i][0]-1] == activePlayer &&
gameState[winningCombinations[i][1]-1] == activePlayer &&
gameState[winningCombinations[i][2]-1] == activePlayer {
isWinner = true
break
}
}
}
The explicit by index access of the (unknown for us) gameState and winningCombinations sequences is quite "unswifty" w.r.t. in the dangers of runtime exceptions for indices out of range for the sequences. So bear in mind that there are safer ways to perform such access.
For future reference, consider reading How to create a Minimal, Complete, and Verifiable example: since we (the potential answerer's of your question) don't have access to/the full information regarding isWinner, winningCombinations, gameState or activePlayer, we can't verify your example. Also, since the question cover a concept, it could be boiled down to a more minimal form. (Welcome to StackOverflow!)

Using && In an If Statement Swift 2.0

I am trying to make a very basic console game of rock paper scissors. I'm not sure how to go about using && in Swift 2.0. I want to basically say:
if (computerChoice == rock && userChoice == paper) {
print("User wins")
}
Well you appear to have it setup correctly. You could set this up using an enumeration variable.
enum RPSOption {
case Rock, Paper, Scissors
}
Then you just use it like so:
let computerChoice: RPSOption = RPSOption.Rock // or something else
let userChoice: RPSOption = RPSOption.Paper // or something else
if( computerChoice == .Rock && userChoice == .Paper ){
// do something
}
// do similar if statements for other conditions

Checking multiple conditions for a string in C#

I want to check multiple conditions in a string in C# but it throws error saying Cannot use && for string or boolean
if ((duStart.Trim() != "" && duStart.Trim() != null) &&(duEnd.Trim() != "" && duEnd.Trim() != null))
{
//do this
}
else
//do that
The code you've given compiles fine. Here's a short but complete program - I've changed the whitespace of your line of code, but that's all:
using System;
class Test
{
static void Main()
{
string duStart = "X";
string duEnd = "X";
if ((duStart.Trim() != "" && duStart.Trim() != null) &&
(duEnd.Trim() != "" && duEnd.Trim() != null))
{
Console.WriteLine("Yes");
}
}
}
Having said that:
If you're going to use the same value (the trimmed version of duStart, for example) multiple times, there seems little point in computing it twice. I'd have used extra local variables (trimmedStart, trimmedEnd) here
Trim never returns null, so those tests are pointless.
Using string.IsNullOrWhitespace is probably a better idea here. Why bother creating strings that you're never going to use?
you can simplify the condition by writing:
if( !string.IsNullOrEmpty(duStart.Trim()) && !string.isNullOrEmpty(duEnd.Trim()) )
{
}
Check for the Null first for duStart and duEnd. Then try Trim the string. Trim cannot be applied on a null value. So, below code block should work for you.
if ((duStart != null && duStart.Trim() != "") && (duEnd != null && duEnd.Trim() != ""))
{
//do this
}
else
{
//do that
}