How I made multi_button (a node that acts as a state machine ?)


        I wanted a button to manipulate the number of items transacted with each click.  It would be very tedious to only buy or sell one item at a time, especially to move volume and as the game progresses I think players will want to move volume.  I wanted w simple button with 3 states "x1, x10, x100"


       I started with a new scene and added in a button node, I called that node/scene "multi_button".  I then added a script with dictionaries to hold each state of the button and its attributes.  Using a dictionary should make it easy to organize, change or add properties to these states I think.



      Next, I created a variable to hold the current state appropriately named it current_state, now it was time to play with my states. I used the built-in on_button_pressed() signal to detect when the player wants to change the multiplier value (clicks on the button).  When clicked the button will run through an if/else block and change the state accordingly.



        Finally, I have a physics_process(delta) to set the text of the buttons label to the appropriate "name" so that way the player knows how many units they are buying or selling. I also send the states "factor" data to the global script so that information can interact with the other nodes.


      



      I have used / made a "state machine " before on a tutorial through heartbeast however that was at the beginning of my programming career so I should do some more research.  



      

Files

Archive.zip Play in browser
May 02, 2021

Leave a comment

Log in with itch.io to leave a comment.