playMaker

Author Topic: Help with reactoring FSM [SOLVED]  (Read 179 times)

krmko

  • Sr. Member
  • ****
  • Posts: 422
    • View Profile
    • Fat Pug Studio
Help with reactoring FSM [SOLVED]
« on: November 04, 2017, 03:08:18 PM »
Hi guys,

I while ago i made a weapon pickup and upgrade FSM system which was quite complicated and needed to be in every weapon to be functionable. Recently i got an idea to make a centralized system for that, but i'm hitting some walls.

Here's the setup:

I've got a hash table of all player weapons objects with key being their names (PulseGun1, PulseGun2 to PulseGun5...every level of weapon is considered a new weapon, but they share the same tag, PulseGun).

On the player, where the pickup system will be located, i have active and inactive weapon. So, when i hover over the pickup weapon, i compare the active weapon tag (let's say it's PulseGun, but we don't know of what level since levels 1-5 share the same tag) with pickup weapon which is also tagged PulseGun.

In that case, if we have PulseGun3 as active weapon, it should be upgraded to PulseGun4 since we picked up the weapon of the same type/tag.

Now, since we have no idea which tags matched on runtime, we need to to another tag check. Is Active Weapon X tag? No. Is Active Weapon Y tag? No. That would take maximum 30 iterations (total weapon number) until we get to is Active Weapon PulseGun Tag? Yes! Now all we need to do is run another check, this time by name. Is Active weapon name PulseGun1? No. Is active weapon name PulseGun2? No. Is Active Weapon Name PulseGun3? Yes! Despawn Active Weapon and Spawn PulseGun4 as active weapon.

Now this doesn't take much (35+ iterations top for each weapon), but it's a hell of a lot of states. I could make multitag and multistring comparison actions to shorten everything to two states (one for checking weapon tag PulseTag and one for checking weapon String, PulseGun1, PulseGun2...) But it still requires a lot of typing and is poorly scalable, especially if i pickup a weapon that's differently tagged than Active Weapon. In that case i'd need to check tag and name of Active Weapon to get to the information of which level is the Active Weapon and the replace it with the same level of Pickup Weapon, which is horrible.

Any advice on this one?
« Last Edit: November 07, 2017, 07:49:27 AM by krmko »

krmko

  • Sr. Member
  • ****
  • Posts: 422
    • View Profile
    • Fat Pug Studio
Re: Help with reactoring FSM
« Reply #1 on: November 04, 2017, 03:10:22 PM »
Sorry, double post.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2265
    • View Profile
    • jinxtergames
Re: Help with reactoring FSM
« Reply #2 on: November 04, 2017, 09:17:15 PM »
Hi,
Maybe the action "Get Name" can be useful?
it will get the name of a game object.

krmko

  • Sr. Member
  • ****
  • Posts: 422
    • View Profile
    • Fat Pug Studio
Re: Help with reactoring FSM
« Reply #3 on: November 05, 2017, 02:18:57 AM »
To make it more clear, this is how would it look



@Djaydino

Well that's ok, but what with name? Let's say i have PulseGun3 with tag PulseGun equipped and i pickup another PulseGun. We can name it whatever and put whatevet tag we want to identify it, but i don't think that makes it easier.

I'm trying to avoid any of the logic on the weapon pickup, though it seems easiest to do it that way.


djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2265
    • View Profile
    • jinxtergames
Re: Help with reactoring FSM
« Reply #4 on: November 05, 2017, 05:45:03 AM »
Hi,
when you pick up a pulsegun (tag) you can use that to reference the needed hastable, then you can use the name as a key.

So the pulsegun (tag) would be the weapon type and the name used for model number.

Another approach would be to place an fsm and add some standard variable for each weapon ("weapon type" , "Weapon model nr, other useful data)
then on picking up a weapon you "get fsm string" to get the data from the picked up weapon.
This approach i see many times in coded (C#) projects.

Instead of using strings i tend to use int numbers and make "id numbers" for items (weapons and other stuff) as it is easier to access arrays (i use the id number as index number)

krmko

  • Sr. Member
  • ****
  • Posts: 422
    • View Profile
    • Fat Pug Studio
Re: Help with reactoring FSM
« Reply #5 on: November 05, 2017, 10:06:25 AM »
I'm afraid i don't understand your advice, but i made it quite simple.

Tag represents a weapon type, when weapon is picked up, it compares tags of the pickup and active weapon. If they match, you go to upgrade state. If they don't match, you go to replace state.
 
Upgrade state: All the weapons are WeaponNameWeaponLevel (Weapon1, Weapon2...to Weapon5). We already have the weapon tag which led us to upgrade state of the Active Weapon. We get the last right character of the Active weapon string, convert it to integer, add 1, and covert it back to string. Then we get the active weapon string lenght, subtract 1, and get left character in that length (that's weapon name without level). Build two strings into a new one and call it out if a hashtable.

Something similar is for replace, only you don't add 1 to converted string integer, you just delete the last character of the weapon pickup string and append the active weapon string last character.

All that's left to do is make a check if active weapon level is already at five to abort the upgrade since it's at maximum level.

Pretty neat and only in couple of states, in comparison to a monster fsm applied to every weapon object.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2265
    • View Profile
    • jinxtergames
Re: Help with reactoring FSM
« Reply #6 on: November 05, 2017, 02:19:00 PM »
Hi,
Its not really an advice, only a different approach.

If it works with minimal actions and Cpu usage its good.

And believe me, i did those things before also, and i am still updating some of my older projects with tons of states in it  :o :o :D

krmko

  • Sr. Member
  • ****
  • Posts: 422
    • View Profile
    • Fat Pug Studio
Re: Help with reactoring FSM [SOLVED]
« Reply #7 on: November 07, 2017, 05:09:43 AM »
I believe so, i'm always fiddling with my stuff :D

Unfortunately, since i'm using pooling, all of the objects get "clone x" appended to their names, i'm not quite sure hot to work this one around.
« Last Edit: November 07, 2017, 05:59:27 AM by krmko »

krmko

  • Sr. Member
  • ****
  • Posts: 422
    • View Profile
    • Fat Pug Studio
Re: Help with reactoring FSM
« Reply #8 on: November 07, 2017, 07:49:15 AM »
It was a little bit of complication with string, but it works for now, phew.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2265
    • View Profile
    • jinxtergames
Re: Help with reactoring FSM [SOLVED]
« Reply #9 on: November 07, 2017, 08:19:29 AM »
Hi,
How are you pooling. you own system or a asset?

krmko

  • Sr. Member
  • ****
  • Posts: 422
    • View Profile
    • Fat Pug Studio
Re: Help with reactoring FSM [SOLVED]
« Reply #10 on: November 07, 2017, 10:46:18 AM »
Pool Boss, so all the pooled stuff has a pool as a parent u less specifically spawned under a parent and adds _(clone_x) to the name, so i had to subtract last 11 characters to get the weapon name, then get the name of the weapon name by getting string left with the number of characters that's the remainder of subtract operation. Something similar to get the weapon level so i can add 1 and build a new string to get the object from hashtable which has names for values, and something similar for the replace weapon operation. As the last part, gui is refreshed to display a new weapon/weapon level.

The best part is that it's all in one fsm in a few states only and it works for all weapons since all weapons are using uniform names and are stored in a hash table. Might make a tutorial for it!

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2265
    • View Profile
    • jinxtergames
Re: Help with reactoring FSM [SOLVED]
« Reply #11 on: November 07, 2017, 04:18:39 PM »
Hi,
Won't you get issues if the clones reaches 10 or 100?

krmko

  • Sr. Member
  • ****
  • Posts: 422
    • View Profile
    • Fat Pug Studio
Re: Help with reactoring FSM [SOLVED]
« Reply #12 on: November 07, 2017, 11:08:43 PM »
No, this is for pickups only, they won't go beyond 2 clones, 3 in extreme cases. After being picked up they go back to the pool with the orginal clone number.
« Last Edit: November 07, 2017, 11:57:28 PM by krmko »