playMaker

Author Topic: How do I make this faster?  (Read 1430 times)

paradiseprime

  • Full Member
  • ***
  • Posts: 105
How do I make this faster?
« on: April 11, 2019, 11:38:15 AM »
Ok so I got a pretty consistent way to take damage.

*Made it so weapon handles damage calculations instead of player as it is more likely that player will be hit multiple times at the time same time than weapon

*Collider passes through all Targets and stores them in an array.

*I cycle through them and get the Defense values from the current target and do the calculations with the Weapon Attack stats vs the Defense stats and create a "damage" value then send a event to "hit" on the Target and get the Event sender "damage" value and subtract that from the health.

This works up to near 10 enemies being attacked at the same time then it starts to slow down and in some cases skips over some enemies and doesnt send damage. How do I make the calculations faster? I condensed the math into a simple 1 action math expression instead of 10 different float operators.

Is there an action that's the same as get fsm variables but gets them from different fsm and is 1 action?

paradiseprime

  • Full Member
  • ***
  • Posts: 105
Re: How do I make this faster?
« Reply #1 on: April 11, 2019, 11:38:57 AM »
here are the other images

GonerGames

  • Junior Playmaker
  • **
  • Posts: 53
Re: How do I make this faster?
« Reply #2 on: April 11, 2019, 08:41:17 PM »
You could probably speed this up by putting the onus on the player or enemy to calculate damage.

Simplified version:
The weapon can have a set float damage value and we use a tag of "weapon". *Note name of FSM in the inspector must match the Fsm Name in next step. Let's use the default "FSM" for this example*
Now set the weapon with a Playmaker local float variable called Hit_Damage = 5. No actions

Your player/enemy:
Will now have a FSM to watch for collision event.
Will have a variable called Health/Life = 10 (let us get a couple of hits before we die).

State 1 = Collision event (3D or 2D) -> On collision enter, Tag = "weapon", send event "hit", Store collider "Hit_With"

State 2 = Get FSM Float *This is where we get the colliding weapon damage*
               GO = Specify GO, Object = "Hit_With", Fsm Name "FSM", Variable name = "Hit_Damage" *Matches weapon variable name*, Store value = "Damage"

State 3 = Float subtract of Health/Life - Damage. Add in your defence score here to reduce damage

State 4 = Float compare to check if health is equal or less than 0. If 0 then go to state 5, if not go back to state 1

State 5 = destroy player/enemy i.e the death of the object.

With this you can simplify how damage is applied to the player/enemy.
In short the weapon has a value that then gets subtracted from the objects life. Quicker than storing all the hits in an array and cycling through them.

Hope I'm not sending you down a wrong path. I've always considered weapons/bullets to be a static damage item. Then it is up to the player/enemy to check on how to deal with that damage.

paradiseprime

  • Full Member
  • ***
  • Posts: 105
Re: How do I make this faster?
« Reply #3 on: April 12, 2019, 07:58:50 PM »
You could probably speed this up by putting the onus on the player or enemy to calculate damage.

Simplified version:
The weapon can have a set float damage value and we use a tag of "weapon". *Note name of FSM in the inspector must match the Fsm Name in next step. Let's use the default "FSM" for this example*
Now set the weapon with a Playmaker local float variable called Hit_Damage = 5. No actions

Your player/enemy:
Will now have a FSM to watch for collision event.
Will have a variable called Health/Life = 10 (let us get a couple of hits before we die).

State 1 = Collision event (3D or 2D) -> On collision enter, Tag = "weapon", send event "hit", Store collider "Hit_With"

State 2 = Get FSM Float *This is where we get the colliding weapon damage*
               GO = Specify GO, Object = "Hit_With", Fsm Name "FSM", Variable name = "Hit_Damage" *Matches weapon variable name*, Store value = "Damage"

State 3 = Float subtract of Health/Life - Damage. Add in your defence score here to reduce damage

State 4 = Float compare to check if health is equal or less than 0. If 0 then go to state 5, if not go back to state 1

State 5 = destroy player/enemy i.e the death of the object.

With this you can simplify how damage is applied to the player/enemy.
In short the weapon has a value that then gets subtracted from the objects life. Quicker than storing all the hits in an array and cycling through them.

Hope I'm not sending you down a wrong path. I've always considered weapons/bullets to be a static damage item. Then it is up to the player/enemy to check on how to deal with that damage.

This works ok. I am pretty sure I have tried this method before but the issue with it is that if enemies are overlapping then it doesnt register all the enemies or sometimes it will hit the same enemy multiple times in one swipe. My original idea was to have the target hit calculate the damage and cycle through all weapons that hit it, calculate the damage, then deal the damage but it end up getting stuck.

GonerGames

  • Junior Playmaker
  • **
  • Posts: 53
Re: How do I make this faster?
« Reply #4 on: April 13, 2019, 12:16:05 AM »
I'm not sure your exact layout of your game, so the above may not work for you. I do see what you are trying to do. By having mutiple arrays looping, checking for duplication, and then calculating damage from another loop and then finally clearing the array will get costly on a single process. You may want to pull these out of the main thread and put it in a coroutine. This way the damage still gets calculated and applied without bogging down the main thread. I haven't used the Playmaker Start Coroutine action, so I can't walk you through that. Hopefully someone else has and can guide you through.
I know people either love or hate coroutines :) . If your not planning on having 1000 run at once you should be fine.

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: How do I make this faster?
« Reply #5 on: April 13, 2019, 12:40:26 PM »
I think the standard is with Set Event Data, Get Event Info.

The weapon sets the data into a “package” and the receiver picks up the info to process it. It should normally be near instantaneous with no problems. But when the health FSM is a huge labyrinth of states, then it takes a bit more.

Use Start state to initialise everything, reset health, load from data container etc. Then put the logic for taking damage in a single standalone state, with your own custom “on hit” global event (sent by your weapons, set with set event info in their damage dealing FSM ).

On hit, get event info, get damageReceived variable, substract from health, then null damageReceived (since it was processed). And that’s it. Put a finished event there, and have it go to an empty “done” state, so you see it did the thing. With debug mode on, you can also count how often it gets there.

Where done is now, you can then add the checks and compare logic to set off hit animation, check if you’re dead etc. Our you can make it more modular which is generally better.

« Last Edit: April 13, 2019, 12:45:02 PM by Thore »