Using function paramters to give objects targets.

Get help using Construct 2

Post » Thu Mar 17, 2016 7:46 pm

I have 2 Enemy objects: Enemy 1 and Enemy2. It is a respawn game so there is always only one of each object at one time. Both enemies have an instance variable which determines their target. What I want to accomplish is having a function that gives them a new target everytime they die or they kill their target.

What I tried was having the function

EVENT - On FUNCTION
--- SUBEVENT1 - Parameter 0 = 1
------ SUBEVENT1A - If .... then Enemy1 set instance variable Target to...
--- SUBEVENT 2 - Paramater 0 = 2
------ SUBEVENT2A - If .... then Enemy2 set instance variable Target to...


on Enemy1 destroyed - call FUNCTION(1)

But this does not work. The enemy keeps the same target that it had when it died. I also have:

ON start of layout - Call Function(1) & Call Function(2) but this doesn't seem to work either.
B
9
S
2
Posts: 37
Reputation: 729

Post » Thu Mar 17, 2016 8:07 pm

You are setting an object's instance variable and than destroying an object, so obviously changes are not saved.
Do it "On object create".
ImageImageImage
B
27
S
16
G
74
Posts: 986
Reputation: 41,691

Post » Thu Mar 17, 2016 8:27 pm

Well I do have it on a wait timer before it runs the call so it should respawn in time. But even calling on start of layout the function doesn't work. I believe it's my main sub events for the function that is the issue.
B
9
S
2
Posts: 37
Reputation: 729

Post » Thu Mar 17, 2016 8:52 pm

You are setting an INSTANCE variable which means it will be gone together with the instance of the object.

You should provide some screens, so we could actually see what are you doing there.
ImageImageImage
B
27
S
16
G
74
Posts: 986
Reputation: 41,691

Post » Thu Mar 17, 2016 9:52 pm

Not sure why you insist on using a Function. If you really want too, i can show you.
But. Consider this.
'On x Destroyed' is allready a function with as parameter(0): the destroyd object. It gets called by a trigger, just automaticaly and with not headaches.

See:
https://
drive.google.com/open?id=0B1SSuCVV8v74amROMHVHR3lKU28
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Fri Mar 18, 2016 1:37 am

99Instances2Go wrote:Not sure why you insist on using a Function. If you really want too, i can show you.
But. Consider this.
'On x Destroyed' is allready a function with as parameter(0): the destroyd object. It gets called by a trigger, just automaticaly and with not headaches.

See:
https://
drive.google.com/open?id=0B1SSuCVV8v74amROMHVHR3lKU28



I have the enemy finding a new target when their target dies, when they die, and when they heal back up after retreating. So I'm using a function to save the hassle of writing that every time. I'll post screenshots in a second. Thanks for the response I'll see if I can use what you've given me.
B
9
S
2
Posts: 37
Reputation: 729

Post » Fri Mar 18, 2016 1:55 am

BackendFreak wrote:You are setting an INSTANCE variable which means it will be gone together with the instance of the object.

You should provide some screens, so we could actually see what are you doing there.


First pic is my on start of layout event. The function is called but does not change the target. (please ignore the getTarget(1) it's no longer in my project)

Image

2nd pic is of my Enemy object and instance variable default at None.

Image

3rd pic is from preview mode. The object is not moving and I have text to show it's current target which is still None

Image

4th pic is the actual function code

Image

Note: Also the function DOES call at the start of layout because the text is set to CALLING during preview mode, but it does not set the target.
B
9
S
2
Posts: 37
Reputation: 729

Post » Fri Mar 18, 2016 2:05 am

Problem solved. A friend found the issue, I'm missing an option for if both players health are above zero. SILLY ME! Thanks for the responses!
B
9
S
2
Posts: 37
Reputation: 729

Post » Fri Mar 18, 2016 2:12 am

If object "Enemy" exists at the start of layout, then the "Target" instance variable will be set if any condition combination within the function is TRUE. I believe none of these conditions are TRUE when you run the function. Otherwise there's a C2 bug - which I doubt.

Also keep in mind that you are changing the instance variable for currently existing object instances. You are not changing the default value, so once you destroy Enemy you have to set the variable again.
ImageImageImage
B
27
S
16
G
74
Posts: 986
Reputation: 41,691


Return to How do I....?

Who is online

Users browsing this forum: David181, Google [Bot] and 19 guests