Function Call Weirdness

Get help using Construct 2

Post » Tue Mar 19, 2013 3:17 am

I decided to move all the asteroid destruction/splitting code in my game to a single function that will be called whenever one of the following occurs:

  • Player ship collides with asteroid
  • Enemy ship collides with asteroid
  • Player bullet collides with asteroid
  • Enemy bullet collides with asteroid


However, I am now getting a weird issue where the asteroid sometimes splits correctly, and other times does not split at all. I'm not sure if this is a function problem or a rendering problem, because I am also having weird issues where 'invisible asteroids' collide with my ship despite the fact that they never rendered on the screen.

Here is the function in question:



ImpulseCollision.capx

Can anyone help me out? I'm confused as to why this is happening after I moved my code to a function call. Is it possible for the function to be called multiple times? Say both the player bullet and the enemy bullet hits an asteroid. Previously, the code that is now in the function would have been duplicated as an event chain for the player bullet -> asteroid collision and the enemy bullet -> asteroid collision, but now both collisions will call the same function.
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,194

Post » Tue Mar 19, 2013 7:26 am

I believe that Functions act within their own scope - that is to say, they don't respect any picked objects at the point the function was called.
So, your function is currently being applied to every asteroid whenever it is called. Is that really what you wanted?

If you want to apply the function to only a specific asteroid, you can pass in its UID as a parameter to the function.tanoshimi2013-03-19 07:26:47
B
8
S
2
G
3
Posts: 83
Reputation: 2,668

Post » Tue Mar 19, 2013 7:29 am

People and their beta version capx files!
B
50
S
14
G
12
Posts: 813
Reputation: 16,764

Post » Tue Mar 19, 2013 7:29 am

Here you go:



I highlighted one odd line, why are you taking velocity from seemingly random object? :)

You need to pass the UID of colliding asteroid as parameter to the function, as visibility of picked objects does not carry into functions.

Some random general notes about the whole project that might help you in the long run:

You should definitively look into using families, soon you'll project will become very hard to manage as it grows bigger and bigger. For example, you could have family 'Bullets' which has Friendly and Enemy bullets. Then you could check for asteroid collisions for bullets without having to do separate events for all bullet objects.

You have lots of variables that you could use inbuilt functions for; for example EnemyCrescentCount could be replaced by using EnemyCrescent.Count which returns numbers of instances that exist in the layout. Keeping manual variables like that up to date can become tedious :)

You are making good progress with your project, keep it up! It's looking great! :)
B
24
S
8
G
7
Posts: 756
Reputation: 7,192

Post » Tue Mar 19, 2013 7:51 am

Thanks Vee!

I actually found out the problem is one line right above where you highlighted. Layer 0 = background layer, not the active game layer! So I changed it and now things are working more normally.

You are right that I should be passing in the object that is colliding with the asteroid and not just some random object. What I have is currently a result of copy-paste and not much refining yet. I should add a parameter to the function that affects the physics. However, I honestly do not see the physics library having much effect on my game objects. A lot of the in-game physics, such as the 'bounce' that occurs whenever a player ship collides with something, is done manually via CustomMovement because the physics library doesn't seem effective.

To be frank, there's a lot of weirdness going on that I'm not sure of. For example, the enemy ships have the Wrap behavior, but they don't wrap so I had to include my own wrapping code.

I've never heard of families in Construct 2, so I'll have to look them up. Right now I've been using groups, but the events sheet is getting really large so anything I can do to cut things down would be appreciated. I've also recently noticed that some of the things I am manually tracking are also automatically tracked from the engine, so I need to redo the events for those things.
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,194

Post » Tue Mar 19, 2013 7:56 am

[QUOTE=Tekniko] People and their beta version capx files![/QUOTE]

For the purposes of bug reporting, you have to use the latest beta version, otherwise you cannot submit a bug report because there is a chance it may have been fixed already in a beta release.

Despite the disclaimer that beta releases are 'betas', the fact that they have fixed numerous bugs since the last stable release means they are probably more 'stable' in terms of non-new features than the latest stable release is!
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,194

Post » Tue Mar 19, 2013 8:19 am

Okay, I've modified the function calls so they pass in a parameter containing the asteroid's UID, this way all the asteroids don't explode when only one of them get hit.

But now I'm having issues where sometimes asteroids you aren't hitting are splitting.



ImpulseCollision.capx
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,194

Post » Tue Mar 19, 2013 8:46 am

[QUOTE=Excal] Okay, I've modified the function calls so they pass in a parameter containing the asteroid's UID, this way all the asteroids don't explode when only one of them get hit.

But now I'm having issues where sometimes asteroids you aren't hitting are splitting.



ImpulseCollision.capx[/QUOTE]

Check my screenshot earlier, you need to pick the asteroid UID you give to function via parameter. The event is right under the function trigger. I also made few other minor changes to the function. :)
B
24
S
8
G
7
Posts: 756
Reputation: 7,192


Return to How do I....?

Who is online

Users browsing this forum: Virpoja, Yahoo [Bot] and 22 guests