playMaker

Author Topic: Enable / Disable FSM state while game running?  (Read 1019 times)

viqusan

  • Playmaker Newbie
  • *
  • Posts: 6
Enable / Disable FSM state while game running?
« on: March 22, 2023, 10:33:38 PM »
Hey there,

I am new to unity and playmaker and have been trying to learn how to make a simple game on my own, as im sure others are as well.

I tried searching for an answer to this but i couldn't find a similar question.

Essentially, i have a state thats running every frame and it sends an event that will check to see if a feature has been unlocked or if its still locked.

What i want to do is once the feature has been unlocked I want to disable the state that was sending the event every frame. I do not want to disable the entire FSM bnut just the specific stat within.

I thought that I could just use another state to do this but im having a hard time figuring out an action that can do this. I was thinking maybe Set Property on the FSM and then using something like fsm.activestate.activeaction.active and having it set that to disable. But im not entirely sure what that property even does.

For properties like this, is there any wiki or something to reference?

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Re: Enable / Disable FSM state while game running?
« Reply #1 on: March 23, 2023, 01:23:50 AM »
There's an action called "Enable FSM" that does exactly that.

Now, do you really have to check every frame if that feature has been unlocked?

This is basically a question about architecture. But "every frame" shouldn't be used  most of the time. It's rather needed in combination with gameplay like controls, mechanics, etc.

For everything else, ask yourself, when do you need to know if that feature is unlocked? That's the moment you want to check the FSM that controls the unlocking.

Without really knowing what you're doing, you probably want to have a Unlock Controller and in that FSM you have a boolean called "isFeatureUnlocked". You would have it set to FALSE and the moment you unlock it, you set that bool to TRUE.

Now from wherever you need to, you can check that bool with GetFSMBool action and do with this information what you want to do.

Also, you most probably want to use PlayerPrefs to save that bool as well and when you start your game you want to load that data back in. So once it's TRUE, it's always TRUE (I actually recommend to use Easy Save plugin from the Asset Store for this purpose).

Hope that helps.

viqusan

  • Playmaker Newbie
  • *
  • Posts: 6
Re: Enable / Disable FSM state while game running?
« Reply #2 on: March 23, 2023, 03:27:16 AM »
Thanks for replying, thats super helpful. I will give that a go and see if that works. I just need to learn more about unity and playmaker.

I can add more detail this is just a pet project. I'm making a tycoon type idle game.

I have a main product prefab which i use to make all my products. In that I have all things associated to the product, UI (buttons to buy, progress bars, Text showing income, cost for upgrades, and icons.)

A product will have its own main loop FSM that lets a player use currency to upgrade, a state that increments income, and then also triggers some UI updates.

I've also got sub FSMs with each products that will help to truncate currencies from long version to shorter with a suffix.

Each product has its own ProductManager and this is what i need to check by frame to see if the product is purchasable or not.

In the ProductManager its essentially checking the player currency and seeing if there enough, (simply using a float compare (currency vs productcost) then the product will show that its purchasable. For now since I'm prototyping it i just have the color swapping.

In the main product parent i have an fsm called CheckUnlock which is sending an event to the productmanager within the product to initiate the float compare which triggers a available or notavilable state for the product.

So my whole thought process with this setup was that its probably not great if that CheckUnlock FSM is constantly running even after the product unlocks so i was looking for a way to disable the state within the FSM once the product was unlocked. Right now i have a send event on a different state in the main loop FSM that will disable the CheckUnlock FSM completely.

I am not sure this is the best approach either so that's why i came here.

(also i totally have easy save and some others in my cart, was just waiting to get some of this more fleshed out before pulling the trigger.)
« Last Edit: March 23, 2023, 03:49:36 AM by viqusan »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Enable / Disable FSM state while game running?
« Reply #3 on: March 23, 2023, 09:08:46 AM »
Hi.
As Christoph said, doing stiff every frame you want to avoid if not necessary.

If you need to, its better to do a Fsm Bool Test to check something on another fsm/object.

if you want to skip certain parts/states in you fsm you can use bools and Bool Test action to skip (on the Ecosystem there is also a FsmBoolTest action)

for save its not reccomended to use PlayerPrefs.
PlayerPrefs is good for basic settings like sound ( Volume fx / music)/ graphics settings for example)
just for prototype you can use them tho.

easy save is by far the best solution out there :)

if you set a save system, place them all on 1 object as later on you might have issues finding them back, if you need to make changes.

viqusan

  • Playmaker Newbie
  • *
  • Posts: 6
Re: Enable / Disable FSM state while game running?
« Reply #4 on: March 23, 2023, 02:36:01 PM »
Thanks so much for the reply. I am in the process of reworking things. Ill let you all know how it going and i hope its okay if I ping for further clarification if I stumble more.

viqusan

  • Playmaker Newbie
  • *
  • Posts: 6
Re: Enable / Disable FSM state while game running?
« Reply #5 on: March 23, 2023, 04:26:12 PM »
Update for everyone. Your insight was extremely helpful, it got me thinking more on my setup and I came up with a different solution that I think is more efficient then what I was using.

I didn't need to go the route of creating a new Bool variable or setting up a test.

Essentially in my GameManager I created a CurrencyListener FSM.

All the CurrencyListener does is wait for a CurrencyPulse event. As the in-game currency increases or decreases based on a purchase or income generation it will then Boradcast all to (currently) two different events. The events are sent to all Products ProductManagers and there are two FSMs that will trigger.

One FSM will check to see if a new product can be unlocked based on current currency. The other fsm will check to see if you have enough money to upgrade your existing products and make the associated button either interactable or not.

What do you all think of this solution?

Its not longer relying on an event to be sent every frame but now only sends an event when there is a currency change.

This also allowed me to simplify some things in my main loop for my products so that the product is not controlling whether it can buy something and now that's all in the product manager.

Again I really appreciate the help here even if I found a different solution. I think working in a silo can sometimes make it harder to see all the possibilities.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Enable / Disable FSM state while game running?
« Reply #6 on: March 24, 2023, 02:58:22 AM »
Hi.
Broadcast all is one to avoid as well :)
as it needs to check all objects in your scene for fsms then send event to all of those

viqusan

  • Playmaker Newbie
  • *
  • Posts: 6
Re: Enable / Disable FSM state while game running?
« Reply #7 on: March 24, 2023, 05:11:11 AM »
Really? Its working so well though. :(

The reason why i opted for broadcast all is because the currency listener is outside of my product prefab instances.

So would the recommendation then be to have a more targeted send event like "Send Event By Name" for each of my prefab products? So if I have say 10 product prefab instances then the currency listener would need to have 10 unique Send By Events to each?

When would you want to utilize broadcast all?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Enable / Disable FSM state while game running?
« Reply #8 on: March 24, 2023, 12:18:10 PM »
Hi.
you can set 1 GameObject as global.

Then add a 'Data' Fsm on that fsm you can set an array and add those prefabs to that array on spawn and remove on despawn.

but maybe instead of the "currency listener" sending an event, maybe the prefabs can send a event to the currency listener (for example if it would be a coin pickup, send event on pickup)
You can set a 'Game Object' (currency Listener) on the data fsm.

Broad cast all you could use for example if you have like 100 enemies and you need to send the event to all those.

viqusan

  • Playmaker Newbie
  • *
  • Posts: 6
Re: Enable / Disable FSM state while game running?
« Reply #9 on: March 24, 2023, 02:57:53 PM »
As always I appreciate the replies. I will explore your suggestion and let you know how it goes.

The reason I had gone with the approach I outlined is because the each product generates its own income at different intervals, so they know when currency is generated and updates the global currency var. Additionally the products will also know when a player has upgraded them and they will then subtract the upgrade cost from the global currency.

The issue I have been running into was that each product didn't have a way to know when the other products had generated income or the player used currency to upgrade the other products. This created a sort of delay for the various purchase buttons as they were only checking if they were purchasable on income generation or upgrade purchase happening within itself.

That's why I had created the currency listener.

Since each product only knows what "it" is doing with currency and not the other products I had set the prefab to have all products send a "pulse" out when they change the currency (increase or decrease). The intent of the currency listener was meant to be a sort of traffic controller for the various UI within the product prefab instances and help "connect" them.

Again, i totally appreciate the continued discussion on this. I am very much new to all this. My regular role is a producer....so I'm may be a little out of my depth for a bit. :P

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Re: Enable / Disable FSM state while game running?
« Reply #10 on: March 24, 2023, 03:34:34 PM »
I definitely think your new approach is way better. And just wanted to add to djaydino about the broadcast all event: never use it. lol

For real, the thing is if your project is small, you won't notice it, but over time your project will grow a lot and you will have more and more FSMs in your project and managers.

Now imagine, every time you have a currency change it has to go through all the FSMs in your project. Every time. Just to find the (currently) 2 FSMs that are really needed. Not good and you will have lags.

So what I probably would do in this case is to use a Game Object Array in your Currency Listener FSM where you add all the individual other FSMs (respectively their GameObjects with the buttons) and then every time you have a currency pulse event, you loop through these and send the needed event to those FSM to check if they can be unlocked and - if needed - to change the buttons accordingly.

I do something very similar but with rewarded video buttons in my project. So I check if Internet is on and if it's on I check if rewarded video is available. I then send from that main verifier FSM the needed events to each individual rewarded video buttons in my game using a loop through the entire array of rewarded video buttons. Works perfectly.

JaydenGadsdon

  • Playmaker Newbie
  • *
  • Posts: 1
Re: Enable / Disable FSM state while game running?
« Reply #11 on: July 23, 2023, 10:45:19 AM »
I think it is a nice idea to create a new game on your own if you have some technical background and creativity. I am a regular accountant and like statistics and gambling at the same time. I prefer easy things that is why I use no verification withdrawal casinos from checked resources https://auspokiesguide.com/best-casinos/no-verification-withdrawal/ . So, you select your website, enjoy the process and withdraw your winning amount in a few simple steps. I just love it.
« Last Edit: July 24, 2023, 04:02:58 AM by JaydenGadsdon »