[solved] Switch Case broken in C3?

0 favourites
  • 3 posts
From the Asset Store
Toggles Dials Switches and Alerts ! A foundational sound grouping of Analog type switches and Steampunk effects
  • Hey everyone,

    I have the following piece of code

    Terminal.prototype.runFunction = function(command, runtime){
    	switch (command) {
    	 case "help":
    		this.fHelp();
    		break;	
    	 case "exit":
    		this.fExit(runtime);
    		break;	
    	 default:
    	 console.log("default");
    	 this.fUnknownCommand();
    	}
    }
    

    The switch statement works for the cases help and exit. However, if I handover a string that is not in one of the cases the default case will not run.

    I tested the code in the browser console and was able to run the default state.

    Is this a problem of C3?

    Cheers,

    Toby

    EDIT:

    Adding more cases show's that it is not a default case problem.

    	switch (command) {
    	 case "help":
    		this.fHelp();
    		break;	
    	 case "exit":
    		this.fExit(runtime);
    		break;
    	 case "exit -h":
    		this.fExitH();
    		break;
    	 case "sleep":
    		this.fSleep();
    		break;	
    	 default:
    	 	console.log("default");
    		//this.fUnknownCommand();
    }
    

    Every case after "exit" will not run.

    Tagged:

  • The browser runs your JavaScript code, not Construct. Switch-case is a basic feature that has been around for decades and if it was broken, significant portions of the entire web would stop working. So by far the most likely culprit is your code is wrong. It's hard to say more without seeing all your code.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Hi Ashley,

    thanks for getting back to me so quickly! You're right, I should have provided more information.

    I am trying myself at an ingame terminal and started with a rough implementation:

    function Terminal(runtime) {
     this.commandList;
     this.terminalInput = runtime.objects.tTerminalInput.getFirstInstance();
     this.terminalText = runtime.objects.tTerminalText.getFirstInstance();
     this.selectedObject;
     this.typeTime = 0.5;
    }
    
    Terminal.prototype.openTerminal = function (runtime, object) {
    	// save the selected object
    	this.selectedObject = object[0];
    	// save the available commands of the selected object
    	this.commandList = this.selectedObject.instVars.commandList.split(',');
    	this.terminalText.typewriterText("Hacking " + this.selectedObject.objectType.name, this.typeTime);
    	// show terminal UI
    	runtime.layout.getLayer("Terminal").isVisible = 1;
     	player.behaviors.Platform.isEnabled = 0;
    	player.instVars.terminalIsOpen = 1;
    	
    	
    };
    
    Terminal.prototype.closeTerminal = function (runtime) {
    	// hide terminal UI
    	runtime.layout.getLayer("Terminal").isVisible = 0;
     	player.behaviors.Platform.isEnabled = 1;
    	player.instVars.terminalIsOpen = 0;
    	
    };
    
    // get the cmd input of the player
    Terminal.prototype.getTextInput = function(runtime){
    	terminal.runCommanIfExists(this.terminalInput.text, runtime);
    	console.log("input: " + this.terminalInput.text);
    	this.terminalInput.text = "";
    }
    
    // check if entered command exists for the selected object
    Terminal.prototype.runCommanIfExists = function(input, runtime){
    	this.commandList.forEach(function(command) {
    		if(command == input){
    			terminal.runFunction(input, runtime);
    		};
    	}); 
    }
    
    Terminal.prototype.runFunction = function(command, runtime){
    	console.log("command: " + command);
    	switch (command) {
    		case "help":
    			this.fHelp();
    			break;	
    		case "exit":
    			this.fExit(runtime);
    			break;
    	 	case "exit -h":
    			this.fExitH();
    			break;
    	 	case "sleep":
    			this.fSleep();
    			break;	
    	 	default:
    	 	console.log("default");
    			//this.fUnknownCommand();
    	}
    }
    
    //
    //	TERMINAL FUNCTIONS
    //
    
    Terminal.prototype.fHelp = function(){
    	var string = "This object supports the following functions. Enter <command> -h to get more information.";
    	this.commandList.forEach(function(command) {
    		string += "\n- " + command;
    	});
    	this.terminalText.typewriterText(string, this.typeTime);
    }
    
    Terminal.prototype.fExit = function(runtime){
    	this.closeTerminal(runtime);
    }
    
    Terminal.prototype.fExitH = function(){
    	this.terminalText.typewriterText("exit - close the terminal", this.typeTime);
    }
    
    Terminal.prototype.fUnknownCommand = function(){
    	this.terminalText.typewriterText("Not a valid command, try help.", this.typeTime);
    }
    
    Terminal.prototype.fSleep = function(){
    	console.log("sleeping nowwww");
    }
    

    The player can enter commands in a text input field and when they hit enter their command is parsed.

    While writing this text I am realizing that only commands provided by the selected object will work. And my test object does not provide other commands than help and exit in its commandList.

    Wow.. so much for code blindness. But writing this post helped me find it, so thank you very much! Especially for the fast response. I will mark this as solved.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)