Is there a way to refer to objects outside of a function? + Function nesting


I’m at work on a more serious, larger project right now. If I want to make the code useable, I need to make it REuseable. So I’m in the process of making some functions for my events, but I ran into a major issue…

Before I get to that, let me show you a Construct 3 function (KiwiStory example):

Let me point out 2 things here:

  1. You can define the “enemies” family without needing to make a parameter for it.
  2. There’s another function nested inside this one, Explosion_1, and it works perfectly without needing parameters.

Now, let me show you what I was trying to do:

Confession time. I’m not a fan of the way GDevelop’s functions work…

If you want to refer to objects, variables, behaviors, ANYTHING really, you need to add an entire parameter for it, and definite it in the function call. You can lock them to specific objects either, so you can put pretty much any object in that parameter.

Also note that I tried to nest the “respawn” function inside of the “hurt” function, in an attempt to make the code tidier (And in case I need to use the respawn function elsewhere). However, since I have the “respawn” function referring to a composite part of the player (Robirdy in this case), now the “Hurt” function needs the composite object AND it’s variable in it, just so the nesting can work…

Let’s go back to Construct 3 event. If I were to make that in GDevelop, I would need to add a parameter for the Health variable. Worse yet, if that nested function were to work at all, I would need to add parameters for the explosion object, the animations, AND the behavior within the enemy damage function, on top of having to add them as parameters in the Explosion_1 function too!

Basically, I’m wondering…

Is there a secret technique in GDevelop that lets me refer to things OUTSIDE of a function without needed a parameter for it like Construct 3 does?

I’m not familiar with Construct. If you create it as a behavior, then the functions could share the behavior properties. You can require a behavior to have other behaviors if you set a property as a behavior. So, you could require something like the health behavior. That way you would have the functions available for your functions. You can also create behaviors and use them within other behaviors and their functions and events would be automatically available.

I agree with you. I don’t like all of the required setup. It seems like it could be simpler. Although, it makes things much easier once it’s setup. You can also copy functions so the parameters wouldn’t need to be setup each time or use a behavior with share properties that can be setup in the behavior tab.

Thank you for your feedback. As a workaround, I discovered the “Extract Events to a Function” option. While it’s not perfect, it’s certainly a workaround that saves me the headache of getting it to work manually.

Thank you again!

1 Like