Problems with tracking instance states

Get help using Construct 2

Post » Fri Feb 08, 2013 3:04 am

Hello everyone,

I am encountering an amazingly frustrating problem that just has me ripping my hair out. It's bit complex so I will try to summarize.

I have a large number of instances of the same EnemyMonster.
The EnemyMonster has two instance variables important to this:
1) Behavior - an int 0, 1 or 2
2) searching - a bool

I also have a global variable called "whoSearching"

The behaviors are as follows
0 - Idle
1 - Attacking
2 - Searching for target

I am trying to track how many EnemyMonsters have Behavior set to 2, searching.

All EnemyMonsters start with Behavior 0 and searching as False, and whoSearching starts at 0.

When an EnemyMonster is set to Behavior 2 and its searching is False, I add 1 to whoSearching and set its searching to True so this will only run once for each EnemyMonster set to Behavior 2.

When an EnemyMonster is set to Behavior 0 or 1 and its searching is True, I subtract 1 from whoSearching and set its searching to False.

Being set to Behavior 2 is the only way to end up with searching as True on any given EnemyMonster, so it must pass through this state before being set to Behavior 1 or 0 has any effect on that instances searching bool or the whoSearching global.

And yet I still get some instances ending up with searching as True without ever being set to Behavior 2 causing my count to be way off.

I really do not understand why this is happening, it seems like such a simple concept but I just cannot seem to figure it out.

If anyone has any ideas I would really appreciate it.

http://dl.dropbox.com/u/61677895/Wastes.caproj

And here is the .caproj for anyone who wants to check out the nitty-gritty details of my script, maybe I'm making a different mistake somewhere and it's effecting this.

Thanks a lot for any help :)Spyral992013-02-08 03:05:59
B
5
Posts: 12
Reputation: 999

Post » Fri Feb 08, 2013 6:18 am

Could you post .capx instead of .caproj? .caproj alone doesn't tell much :)
B
24
S
8
G
7
Posts: 756
Reputation: 7,202

Post » Fri Feb 08, 2013 6:23 am

Ah, I see how that works now!

http://dl.dropbox.com/u/61677895/Wastes.capx

sorry about that, I didn't know how to find the capx file.
B
5
Posts: 12
Reputation: 999

Post » Fri Feb 08, 2013 7:07 am

It seems like it's a bit over complicated; few things:

You can do stuff like this instead of keeping manually count of things:

enemy.isSearching
enemy.pickedcount = 0
.. do stuff you do when no enemies are searching

Also, 'trigger once while true' condition would be useful for you on few occasions. I'll modify your .capx a but and post it here later today if I got some time :)
B
24
S
8
G
7
Posts: 756
Reputation: 7,202

Post » Fri Feb 08, 2013 9:27 am

Here you go, my take on what you did there:

Wastes simplified

I basically redid the whole thing, in my opinion at least it is a whole lot simpler to manage now :) I probably lost some of the functionalities you had there, but they should be pretty easy to add in there. I used only single variable for the AI state which makes it a lot cleaner, also I used picking objects bit more effectively than you did in the original. Hope this helps! :)
B
24
S
8
G
7
Posts: 756
Reputation: 7,202

Post » Fri Feb 08, 2013 5:13 pm

Thank you!

Haven't had the chance to look it over yet (I'm still at work) but I will first thing when I get home.

I had a feeling it was overcomplex as I added each new behavior I wanted over the previous stuff, but I was just too close to it all to see, thanks for the fresh eyes.

I definitely did not know I could count the number of instances with a variable set to true! I look forward to learning how to do that from your changes to my script.

I'm sure this will be very informative :)
B
5
Posts: 12
Reputation: 999

Post » Sat Feb 09, 2013 2:13 am

Alright, I have had the chance to look it all over!

Thanks a lot for taking the time to show me what's what :)

While I didn't take your work verbatim, it did teach me what I needed to know to solve this problem and I really appreciate that.

I am now able to move on with this project after being stalled for three days, I am in your debt.

Cheers!
B
5
Posts: 12
Reputation: 999

Post » Sat Feb 09, 2013 8:47 am

No problems, your original approach was a bit 'coderish', C2 can do things a bit more efficiently at some part. Nice that I was able to help! :)
B
24
S
8
G
7
Posts: 756
Reputation: 7,202


Return to How do I....?

Who is online

Users browsing this forum: Lof, YoHoho and 21 guests