playMaker

Author Topic: Weird integer calculation problem  (Read 1530 times)

Jimmy P

  • Playmaker Newbie
  • *
  • Posts: 5
Weird integer calculation problem
« on: October 23, 2014, 06:50:12 AM »
Sorry about the poor thread title, I know it's not very descriptive of my problem.

I've made a simple turn based combat system, but I run into a weird problem in the FSM that takes care of my Enemy GameObject health status. It apparently works fine; basically, when my player hits an attack, an event is sent to the Enemy FSM to inflict damage. The calculation checks if the enemy health Int is greater than the damage inflicted. If it's not, the enemy GO is destroyed. If it is greater, it moves on to the next state which subtracts the damageinflicted Int from the enemy health Int, then returns to the start of the FSM and waits for another attack. I got a custom action for Int Subtract.

I hope that's clear. I'm very new to all this and don't know how to express myself perfectly.

Now, when I set the enemyhealth int to 10, and player's damage int to 5, it is logical that the enemy should be destroyed after 2 attacks. However, it takes 3. I've messed around with the numbers, and it always takes one more attack than it logically should. I tried to run the FSM step by step, and THEN it worked as it should! The enemy GO was destroyed after the appropriate number of attacks. Running the FSM normally returns the unexpected behavior where it takes one more attack than expected.

If not for the fact that it behaves appropriately when run step by step, I would keep looking for flaws in my logic before posting this. I'm definitely open to the possibility that I have made some stupid mistake(s).

I expect I'd be able to figure this out, or atleast some workaround, given enough time. I just thought it was really weird that it works as expected when running it step-by-step, but not otherwise. I'm hoping someone will have some experience with something similar. Any help or insight would be appreciated.

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Weird integer calculation problem
« Reply #1 on: October 23, 2014, 07:13:47 AM »
Subtract the damage first, then use Int Compare and fire the destroy event if Health is less than or equal to zero.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

Jimmy P

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Weird integer calculation problem
« Reply #2 on: October 23, 2014, 07:29:20 AM »
Ah! That sounds much smarter than the way I've done it. Silly mistake on my part, no surprise there. Thanks a lot for the fast reply, I'll try this out when I get home from work.