I’m trying to create a hierarchical state machine, but don’t understand how to transition to a superstate

I have a CharacterController that implements a StateMachine that uses a number of states and transitions. The StateMachine checks for a valid transition to a new state at the start of every frame, and either moves to a new state or continues ticking the current state. Here is a brief example of all of this working together:

public class CharacterController : MonoBehaviour {     public StateMachine { get; private set; }           private void Awake()     {         InitializeStateMachine();     }      private void Update()     {         StateMachine.Tick(); // Checks for valid transitions, and then sets and/or ticks the appropriate state     }      private void InitializeStateMachine()     {         StateMachine = new StateMachine();          // Create states         var idleState = new IdleState(this);         var walkState = new WalkState(this);         var jumpState = new JumpState(this);          // Add some transitions         Func<bool> idleToWalk() => () => IsGrounded && velocity.x != 0;         Func<bool> idleToJump() => () => IsGrounded && Input.GetKeyDown(Keycode.Space);                                     // From       To       Condition         StateMachine.AddTransition(idleState, walkState, idleToWalk());         StateMachine.AddTransition(idleState, jumpState, idleToJump());     } } 

I really like this implementation because the states and the StateMachine are decoupled; only the "motor" class knows about both of these things, which makes it very easy for me to keep my code clean. Specifically, I like that the individual states don’t have state-switching code in them; then, if I want to make changes to how transitions between certain states occur, I have all of that code in one place (the InitializeStateMachine method, or I can add transitions in specific places later).

However, as my project grows I’m running into an issue where I’m having to create many transitions to states that share similar functionality. For example, the idle and walk states only work if my controller is grounded, so if I have a fall state then I need to make transitions from the fall state to every possible grounded state:

Func<bool> fallToIdle() => () => IsGrounded && velocity.x == 0; Func<bool> fallToWalk() => () => IsGrounded && velocity.x != 0; Func<bool> fallToSlide() => () => IsGrounded && velocity.x != 0 && Input.GetButtonDown("Slide"); 

It would be nicer if I could simply transition to some kind of generic GroundedState that handles the next steps as needed.

I read that one way to help solve this problem is to create a hierarchical state machine, but, though the concept makes sense to me, I don’t understand how to implement it in the context of my current system.

I assume that I would need to make a GroundedState parent class from which any ground states inherit, but how do I actually make transitions from fall to grounded, and then get from grounded to, for example, walk or slide in a single frame? Would the GroundedState need a reference to the StateMachine class so that it can set the correct substate accordingly?

This script doesn’t work and I don’t know why. | Unity 2D C#

Here is the army code:

using System.Collections; using System.Collections.Generic; using UnityEngine;  public class Army : MonoBehaviour {     public int ArmySize = 100;      private Vector2 lastClickedPos;     private bool moving = false;     public float speed;      public SpriteRenderer sprite;      private GameObject Village;      private void Start()     {         Village = GameObject.FindGameObjectWithTag("Village");     }      private void Update()     {         if(Input.GetMouseButton(0))         {             sprite.enabled = true;             lastClickedPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);             moving = true;         }          if(moving && (Vector2)transform.position != lastClickedPos)         {             float step = speed * Time.deltaTime;             transform.position = Vector2.MoveTowards(transform.position, lastClickedPos, step);         }         else         {             moving = false;         }          if(ArmySize < 0)         {             ArmySize = 0;         }     }      private void OnTriggerEnter2D(Collider2D other)     {         if(other.gameObject.CompareTag("Village"))         {             Debug.Log("Attack");              if (ArmySize <= Village.GetComponent<Village>().strength)             {                 Debug.Log("Lost");                 ArmySize -= 30 % Village.GetComponent<Village>().strength;             }              if (ArmySize > Village.GetComponent<Village>().strength)             {                 Debug.Log("Won");                 ArmySize += 15% Village.GetComponent<Village>().strength;                 Destroy(other.gameObject);             }         }     } } 

and here is the village code:

using UnityEngine;  public class Village : MonoBehaviour {     public int strength;     private int x;      private void Start()     {         StrengthValue();         strength = x;          Debug.Log(gameObject.name + " ---- " + strength);          transform.localScale = new Vector3(strength / 100, strength / 100, 1);          if(transform.localScale.x < 1 && transform.localScale.y < 1)         {             Debug.Log(gameObject.name +  "Is Destroyed");             Destroy(gameObject);         }     }      private void StrengthValue()     {        x = Random.Range(10, 500);     } } 

the problem is that it sometimes doesn’t destroy the village even though the army size is bigger than the village’s strength. I also got an error but I can’t seem to re-create it. I think it was something about an object reference? But don’t think that that’s 100% right.

SQL Query to Return Rows that don’t exist as blank

I have a database of information of a possible 1-100 rows. The issue is some of the numbers don’t exist.

When I run a query the following results are retuned:

Unit,Location,Name,Description
1,1,a,xxxx
1,2,a,xxxx
1,3,a,xxxx
1,6,a,xxxx
1,7,a,xxxx
1,9,a,xxxx
1,10,a,xxxx

How can I get the following result instead:

Unit,Location,Name,Description
1,1,a,xxxx
1,2,a,xxxx
1,3,a,xxxx
1,4,-,—-
1,5,-,—-
1,6,a,xxxx
1,7,a,xxxx
1,8,-,—-
1,9,a,xxxx
1,10,a,xxxx

Why don’t AccountingForm, NumberForm and DecimalForm print more decimals?

I have the following set of equations:

xs = 9295050963679385441209; ys = 10721945986215692199666; x = xs - 10000; exponent = 0.666451549104308964``18; xs1gt = Power[xs,exponent]; 

Which should produce ~437295921404696.997750975489799605.

If I naively print xs1gt, I get this:

4.372959214046970*10^14 

I looked for solutions on StackExchange, and I found the How to avoid the scientific notation in output? thread. Unfortunately, none of the solutions proposed there worked:

AccountingForm[xs1gt, 33] DecimalForm[xs1gt, {15, 18}] N[xs1gt, 33] NumberForm[xs1gt, 33] NumberForm[xs1gt, 33, ExponentFunction->(Null&)] NumberForm[xs1gt, 33, ScientificNotationThreshold->{-Infinity, Infinity}] 

Outputs:

437295921404697.0 437295921404697.000000000000000000 4.372959214046970*10^14 4.372959214046970*10^14 437295921404697.0 437295921404697.0 

I searched high and low for alternatives, and I stumbled upon InputForm and SetPrecision, which finally gave me satisfactory results:

InputForm[xs1gt] SetPrecision[xs1gt, 33] 

Outputs:

4.372959214046969977509754897996045`16.295988813986288*10^14 4.37295921404696997750975489799605*^14  

Now my question is why didn’t the other approaches, i.e. AccountingForm, DecimalForm and NumberForm, produce a similar result with 33 significant figures of precision (15 digits and 18 decimals)? I am especially confused by DecimalForm not having worked the way I expected.

Why don’t my keypresses work in Pygame?

I don’t know why my attempt to read keypresses in Pygame is not working.

import pygame  pygame.init()    white = (255, 255, 255) green = (0, 255, 0) blue = (0, 0, 128) black = (0, 0, 0) red = (255, 0, 0)  w = 500 h = 500  x = 15 y = 480 vel = 5 screen = pygame.display.set_mode((w, h))  square = pygame.image.load(r'/Users/lanik2009/Downloads/square.PNG') square = pygame.transform.scale(square, (46, 40)) triangle = pygame.image.load(r'/Users/lanik2009/Downloads/triangle.PNG') triangle = pygame.transform.scale(triangle, (46, 40))  def player():     screen.fill(black)     pygame.draw.circle(screen,white, (15, 480), 10, 500)     screen.blit(triangle, (80, 460))     screen.blit(square, (180, 460))          running = True  while running:            for event in pygame.event.get():         if event.type == pygame.QUIT:             running = False      userInput = pygame.key.get_pressed()     if userInput[pygame.K_LEFT]:         x = -vel     if userInput[pygame.K_RIGHT]:         x = vel          player()     pygame.display.update()  

Loco translated language files in plugin folder don’t work

I created an own plugin wished to make it translatable. So I made a template with loco translate. pot file was put to plugins/my-plugin/lanuages/ folder as desired. Than I made a translation to my language (hungarian) to the same location. Nothing happend. Than I created one more translation on the same language but not to Author (plugins/my-plugin/languages) destination but System (languages/plugins/my-plugin). And it worked that way. I dont’t now why it does not work in plugin folder. Plugin header is set to /languages/ to store the translation files. POT file also got there by default so it has no sense. I want to provide some translation with my plugin but it would work if the translation were in plugin not in system folder. Any idea?

Why Beat ’em Ups don’t allow players to face toward or away from the screen

Most Beat ’em Ups don’t allow players to face away or towards the screen: example, in Streets of Rage, when you press up, the character moves up but still facing either left or right and same goes when you press down. Even in modern Beat ’em Ups, this is the case. What is the reason for this? I used to think this is because the graphics are 2D so its harder or more work to get characters to face towards or away from the screen. But even in Beat ’em Ups that uses 3D graphics, this is still the case, like Double Dragon Neon.

One more thing, if you know any Beat ’em Ups that does do this, can you post the name of the game here. I am interested on how that behaves.