(Solved) Javascripting newbie needs help with switch statements

● ARCHIVED · READ-ONLY
Started by marbeltoast 4 posts View original ↗
  1. Hello, and thanks for clicking on my post. I am stuck with the following:

    I'm designing a rougelike game and will be using a random number generator (the one built into MV's variable controls) to choose dungeon floors, item tables for shopkeepers, and loot for treasure chests. Rather than having 100 "if" statements for each, I'm going to use switch statements.

    I have some code writing experience, but not any with javascript specifically, and I am new to the concept of a switch statement.
    I've got as far as passing (or maybe just "trying to pass") some information from the engine to this code:

    switch(i = $gameSwitches.Value(2)) {

    case i === 0:

    break;

    case i === 1:

    break;

    }

    Problem number 1:
    I don't know whether what I have here would work or not. I've tried running it and all it's done so far is tell me that "undefined is not a function" and mention yanfly's absorbtion barrier plugin in the error message for some bizzare reason. I've tried turning the plugin off, but that just moved it up from the bottom of the plugin list to the next highest plugin up. Why is it referencing my plugins at all? What have I failed to define?

    Problem number 2:
    Even if I can send info from the game to the script I'm writing, I don't know how to send info the other way. I know I'm supposed to write something between "case i === 0:" and "break;", but I have no idea what that something is. That's more of a general question, I know, but hey, when you're as stuck as I am, it's a good idea to ask a lot of questions.

    Whatever feedback you can provide is deeply appreciated. Thanks for reading to the end :)
  2. I've moved this thread to Learning Javascript. Thank you.

  3. First your switch syntax is wrong, let me try and help with an explanation.

    A switch needs a value not an assignment, you are assigning a value to i (unsure why), so rather than (i = $gameVariables....) it should simply be ($gameVariables...) The switch statement will take the value given and allow you to branch off with case(s) not much different than a couple if statement but it is much cleaner.

    Your second problem is again syntax, this time the case(s) value. A case value should be what you expect or what may be expected as the return value.
    Because $gameVariables.value(1) will be returning numbers, then all the case needs is the number not an assignment. So rather
    than case i === 0 it should simply be 0.

    Example code

    PHP:
        switch ($gameVariables.value(1)) {
          case 0:
            // If $gameVariables.value(1) returns 0 then do stuff here
            break
          case 1:
            // If $gameVariables.value(1) returns 1 then do stuff here
            break
    
          default:
            /**
            * If $gameVariables.value(1) returns anything other than what you have
            * then do stuff here
            */
        }

    To answer your question about a plugin displaying in the console it's because MV catch's an exception
    and proceeds to display the error stack, this is generally a trail of breadcrumbs of what the program was doing
    before it crashed, and it usually always includes other plugins because other plugins alias and overwrite default
    functions.

    I'm a bit confused as to what else you're asking, but I hope this helps in some ways for you.
  4. Alright, I think I get how the syntax works now, and any future problems are probably things I can figure out on my own vis sa vis the structure of switch statements, which means the next step is figuring out what to put as the "stuff" in "do stuff here", though methinks that's more of a general javascript knowledge thing that I can pick up from online tutorials and books on the subject.

    Thank you for your time :)