Search Unity

Need help with debug new to coding

Discussion in 'Editor & General Support' started by AdamNowo, Dec 4, 2018.

  1. AdamNowo

    AdamNowo

    Joined:
    Dec 4, 2018
    Posts:
    2
    PlayerStateListener


    // Every cycle of the engine, process the current state.
    void onStateCycle()
    {
    switch (currentState)
    {
    case PlayerStateController.playerStates.idle:
    playeranimator.setbool("walking, false");
    break;

    case PlayerStateController.playerStates.left:
    playeranimator.setbool("walking", true);
    break;

    case PlayerStateController.playerStates.right:
    playeranimator.setbool("walking", true);
    break;
    }
    }

    // onStateChange is called when we make a change to the player's state from anywhere within the game's code.
    public void onStateChange(PlayerStateController.playerStatesnewState)
    {
    // If the current state and the new state are the same, abort - no need to change to the state we're already in.

    if (newState == currentState)
    return;

    // Check if the current state is allowed to transition into // this state. If it's not, abort.
    if (!checkForValidStatePair(newState))
    return;

    // Having reached here, we now know that this state change is // allowed. So let's perform the necessary actions depending // on what the new state is.
    switch (newState)
    {
    case PlayerStateController.playerStates.idle:
    break;

    case PlayerStateController.playerStates.left:
    break;

    case PlayerStateController.playerStates.right:
    break;
    }

    // And finally, assign the new state to the player object
    currentState = newState;
    }
    // Compare the desired new state against the current, and see // if we are allowed to change to the new state. This is a
    // powerful system that ensures we only allow the actions to // occur that we want to occur.

    bool checkForValidStatePair(PlayerStateController.playerStatesnewState)
    {
    bool returnVal = false;
    }
    // Compare the current[…]”

    //Excerpt From: “Unity 2D Game Development.” iBooks.


    PlayerStateController


    using UnityEngine;
    using System.Collections;
    public class PlayerStateController : MonoBehaviour
    {
    public enum playerStates
    {
    idle = 0,
    left,
    right,
    jump,
    landing,
    falling,
    kill,
    resurrect
    }

    public delegate void playerStateHandler(PlayerStateController.playerStatesNewState);

    public static event playerStateHandlerOnStateChange;
    void LateUpdate()
    {
    // Detect the current input of the Horizontal axis, then
    // broadcast a state update for the player as needed.
    // Do this on each frame to make sure the state is always
    // set properly based on the current user input.
    float horizontal = Input.GetAxis("Horizontal");
    if (horizontal != 0f)
    {
    if (horizontal < 0f)
    {
    if (onStateChange != null) onStateChange(PlayerStateController.playerStates.left);
    }
    else
    {
    if (onStateChange != null) onStateChange(PlayerStateController.playerStates.right);
    }
    }
    else
    {
    if (onStateChange != null) onStateChange(PlayerStateController.playerStates.idle);
    }
    }
    }

    //Excerpt From: “Unity 2D Game Development.” iBooks.
     
    Last edited: Dec 6, 2018
  2. Please put your code among code tags, you can find the button in the toolbar when you're editing your post. You can edit your post clicking on the "edit" word at the bottom of your post.

    I'm not sure, because your code is a mess in this form, but this line should be replaced with this one:
    Code (CSharp):
    1. public void onStateChange(PlayerStateController.playerStates newState)
    .
    Code (CSharp):
    1. bool checkForValidStatePair(PlayerStateController.playerStates newState)
    .
    Code (CSharp):
    1. public delegate void playerStateHandler(PlayerStateController.playerStates newState);
    .
     
  3. AdamNowo

    AdamNowo

    Joined:
    Dec 4, 2018
    Posts:
    2
    Code (CSharp):
    1. public static event playerStateHandlerOnStateChange; }
    The rest of the code is good but this line still says identifier expected
     
  4. Check the } in that line, are you sure you need it? I advise you to look for a beginner tutorial on C# first, because clearly you have no idea how a C# code should even look like. You shouldn't work on advanced topics like events and such. Learn the basic syntax first.

    https://mva.microsoft.com/en-us/tra...solute-beginners-16169?l=Lvld4EQIC_2706218949
     
  5. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I suspect it is the same issue, missing a space