Make in same extension elements/objects visible to each other in functions

The objects, behaviors and functions declared into a single extension should be visible to each other as type.

In this example it means that, in all the ExampleObjectFunction, ExampleBehaviorFunction, ExampleGlobalFunction, you could for instance:

  • Create an object of type ExampleObject
  • Do a for each object of type ExampleObject or ExampleBehavior, and then some action on those

This way, the compartimentation limited to the extension is not jeopardize, but you have a lot more flexibility to create a segregated full working package as an extension.

Is it related to this topic?

You should give some real life examples of how the feature can be used to back your request.

Let’s say:

  • Your extension contains a Button custom object.
  • The current scene has 3 objects PlayButton, SettingsButton, QuitButton of type Button.
    Which one of the 3 objects should be instanced?

in what way can you not put the object to affect as a parameter?

i would like an example in which that would be too cumbersome.

i take it back.

i have forgot how annoying it is that behaviours literally cannot access any objects outside it’s scope. i mean, this doesnt encorage modular coding (within the same game).

of course i see the appeal of this constraint for extensions that will be shared publicly. but if its for personal code organization, its pretty bleak.


ex: lets say that i want to put all enemy behaviours/attack/movement in a behaviour,

  • how do i check collisions?
  • how do i manage aggro?
  • how do i manage attacks?

those things require interactions with other objects


everytime i used behaviours i regretted it.

i end up only using event group and external events.

(functions are fine by themselves tho)

give the option to pass it.

am i wrong?

It’s not simple but they can. One option is to call a function and pass the other objects as parameters. Not great. It’s hardly automatic like just adding a behavior.

The other option is to create another behavior for the other objects similar to the platform or Pathfinder behaviors. Those behaviors use managers to pass the other objects. I never dug too deep into it. I think it could keep the locations and sizes in an extension array or structure variable.

Another approach might involves Javascript. IDK.

Or I guess you could use a external events instead of an extension.

None of them are ideal. It would be nice if there was an easier way. It’s difficult bc extensions are purposely isolated.

yes.

they are isolated on purpose, which i great in some sense. but heard other people saying that “omg, using extensions is so better for organizing my code”. so i try it, i want to love it, but i hate it.


yea. i find myself having to put half of my code in main sheet and half of it in the behaviour, even though i’m trying to add one feature. it makes the feature split between far away places and it breaks LOB (locality of behaviour)


there could be an option to allow “dangerous features” like OP proposed, and disallow it for sharing. (though that seems anti-GDevelopian)

a good one would be to give more love to external events (allowing them to be called with params, allow recursive functions, etc…)

Here some instances of behaviors relying on other objects.

1 object moving relatively to another one

These extensions ask you to call an action every frame to know the position of the other object to follow. This is not ideal for extensions in the store because they won’t do anything if you don’t know about this action. But, when you write your own custom behavior you know what you did so it’s kind of fine.

This one uses JavaScript because it was more convenient for users.

Character movement and obstacles

In addition to built-in extensions, there are some extensions are also do this. They are using JavaScript to do fast collision checks.

For this one, there is only one behavior that is both the character and the obstacle.

Putting optimization aside, they can be done with just events also by relying a behavior action with a parameter for obstacles.

the point is still there though.

of course these extensions use custom functions to be used in the editor cuz its for everyone, so the creator must make it flexible.

but an extension that we use for personal project its annoying that you start making the behaviour, and everythings fine until you can’t stay in lifecycle event cuz ya need another object. and now you start working at two places for one thing.

whats nice about behaviours is lifecycle events (this and enums). if i start adding regular functions, i start to wonder why is it a behaviour in the first place. (of course none of this apply to shared extension cuz they gotta be clean).


its like keith said. i wish i could just, add the behaviour. and get immersed in the design of the behaviour.


i understand you not want to globalize extensions, so a plan B would be to bring more love to external events and give more modular power to those so we don’t have to rely on extensions for cleaner code

What is the issue about making a behavior action with object parameters to pass the other objects you need and call this action every frame in the scene events?

If you mean allowing to pass values and picked objects to external events, it’s kind of what an extension function already is.

The good thing about not having access to the whole scene in functions is that you clearly see what each function uses just by looking at the scene events. It gives you a sum-up of the interactions between objects and the main rules of the game without it being lost into technical details.

This template is a good example of this. The main events are somewhat easy to understand since all the details like visual effects and advanced logic like how explosion spread on the board are hidden in custom extensions.

If the extensions had access to the whole scene, indeed you could even have no event in the scene at all, but it would actually make the events harder to understand because you would lost the separation between the big picture and small details.

Yes. It was the starting point, this describe an example.

Ok, so let’s imagine a few ones.

  1. In the related example, I want to have a behavior which allow me to get the most valid object matching criteria under the mouse, this means that for this behavior, I need a gloabl function called each frame, which cycle through all the object matching the criteria, compare to the known top value and possibly store the new value and object, and at the end inform the topmost value object that it is the adequate object.
  2. Let’s say I want to make a magnet behavior. I have 2 behavior + and -, that I can attach to object, and also a power parameter. I want the magnet to get attracted by ALL objects of the opposite sign, with a strengh of attraction depending of the power and distance. At each cycle, I will go in each objects with the behaviour, check ALL objects with opposite sign, evaluate direction/distance vs power and then come out with a movement vector for this object. I don’t need to do a single thing in the event sheet (except changing power according to some game rules), it is all inside the behavior.
  3. Another real life example: I want to create a custom behavior which indicates a ressource gain/losses on a object, for instance the mine just generated 2 gold. I attach the behavior to the mine object, and when the mine generated the gold, I trigger the behavior of the mine, inside the behavior it creates a custom object of type VFX gold (which disappear after time), place it at the mine position, set the same layer as the mine, and so on.

I can dig the example if necessary.

On my point of view, it creates a Button only, because that is the only one which is known inside the behavior. The other are known at scene level only.

But if the create object is a big issue, then just limit to the access to other object, this would be on my mind already a step forward.

That’s exactly what I don’t want to do if I define a custom behavior. I want to have most of the things running transparently in the background, except if it is not needed. See the magnet example, in game I just change the power of the magnet, the rest is done behind the hood.

Agreed on this, that is why I request more possibility INSIDE the extension, to work with ALL components defined in the extension itself. But still segregated from the outside.

Behavior like the YSort just run in the background, you have no feedback, and this is good this way.

Some other behavior may request activators, like shake, but they still run in the background once activated.

Some other can provide feedback, but may also run completely transparent, like the TopDownMovement: you may just declare it, and don’t see it in the event sheet, or you may have interaction in the event sheet.

Some other requires input and provide output, but it is different.

Also I forgot to add, when I pass an object as argument, I cannot say it is an object with behavior X and access to this behavior properties and function.

An example to show what I mean.

I want to create a CRPG custom extension:

  • The character have a behavior “character” with stat: STR, DEX, …
  • The interactable object have another behavior “test opponent” with stat and threshold
  • The character behavior hav function to make a stat check, with random 1-20 + stat
  • I want to pass a “test opponent” to the “character”, so I can make a stat check of the right stat and compare to the threshold.
    • This is not possible, because I do not see and known the type of the other behavior in the same extension

Do you mean something like the built-in “Draggable object” behavior where it moves the object under the cursor with the greatest Z-order?

I guess it falls into the “Character movement and obstacles” in my previous message.

The following template uses:

  • a behavior to animate a floating label
  • a behavior to wobble the tile

I guess that if I wanted to handle the whole animation with only 1 action I could either:

  • Make an extension function “animate production” with 2 object parameters: the floating label and the tile
  • Put these 2 objects inside a custom object and handle it in an object function “animate production”

With a custom object, I guess you can also handle the timer and the score change, but, in my opinion, it would be mixing the main rules into the details which leads to harder to read events.

Button is an object type not an object. For instance, you can’t create a Sprite. You can only create a Coin that is an object of type Sprite.

The Y-sort behavior doesn’t require any interaction with another object. So, it’s not a good example for this feature request.

You can add behavior parameters after the object parameter to tell which behaviors you require as explained in the doc:

i think i can see your main point somewhere in there.

do you perhaps argue that moving everything from main sheet to behaviour would make the event sheet too empty and the behaviour too clogged? and that it would simply move the problem?

is your ideal game structure like this?:

  • main sheet → for changing state of behaviour
  • behaviour → handle the state

i guess thats a good argument and for that reason i will stop arguing.


I wouldn’t use these words but I think you got the idea.
I guess it could be put like this:

  • scene events → the interactions between objects, what you might say to someone to explain the general idea of how the game works.
  • extensions events → complicated stuff that actually do the job, what you might be proud to explain to a fellow coder or just juicing animations or implementation details.

It’s not a strong rule and there is definitely a need to:

  • remember other objects that were passed in an action parameter of the behavior
  • or just know all of the objects with a given behavior

since some JS extensions do this. But a workaround exists and adding 1 action in the scene events is a small price to pay. I mean it’s not great, but it’s not what will make your events hard to read.

To sum up, 2 feature requests has been discussed:

  • Be able to use other objects in life-cycle function of behaviors (objects passed in parameters before or all objects with a given behavior)
    • I think it would be great to have it but it sounds hard to implement and a workaround exists so I doubt it will become a priority any time soon.
  • Scene functions where you can access all the objects like in an external events
    • It’s also something hard to implement and I’m not sure it would actually allow to better organize events. It’s a bit like external events, they allow to split the mess but it’s still a mess.

@davy I have the feeling that we are not understanding each other. So I will try to explain differently. Take note that in the title “Make in same extension elements/objects visible to each other in functions” I imply “functions defined in the SAME extension” and not “functions created through the scene event sheet”

Yes, I agree with this view. In my view, extensions should be module which are independant, portable. And they should not need to be triggered from the outside, except for passing parameters.

As said, the Y sort is a good example: I attach it to my object, and it do stuff, that I do not care about.

I am not asking to have the scene components visible inside the extension. I am asking to have the components defined in a same extension visible to other components: Custom objects defined in the extension and their properties & functions, Behaviors defined in the extension and their properties & functions, global functions defined in the extension, visible to each other as long as they are in the same extension.

A concrete example, inspired from this thread:

I want to make an extension which allow me to check if isometric object are behind or in front of each other, not all of them have square shape. For this I need at each frame:

  1. cycle through all the object with behavior and assign them a unique Z value, and reset behind list of each objects.
  2. cycle trhough all the object with behavior, calculate the (isometric projected) leftest and rightest point based on the origin
    1. cycle through all other object with this behavior, compare the origin with the 1st object leftest and rightest point.
      1. If the condition are met, and the Z value is lower, then exchange the Z value.
      2. Also store the unique ID of the object, to compare, so that the exchange does not happens twice

As you said this is the complicated stuff that I could make with code, and that the user don’t want to know about, it just sorts the objects according to the rules and works. In theory, it shall have not necessitate as single call in the event sheet to work.

But it is currently not possible to implement this way. I can possibly trick a simple foreach object through the functions called each frame, however I cannot compare object with the same behavior in the extension. I also cannot do a for each objects with the behavior in the global function, because the global function in the function is not even aware that the behavior exists.

And that is the feature I request: Everything defined in the extension should be accessible inside this same extension.

And if this not possible, I could be satisfied with this alternate reduced feature:

  • Inside an extension behavior, there shall be a way to make a foreach on all object (and pickall, Condition/action, …) with this behavior, and allows interaction beetween themselves.

For my short GD experience, for each and in beetween object interaction is the most missing part.

=> Could also be very helpful, but this is in my view another feature request.

=> This is not related to my initial request.

I hope this give more clarity to the request.

A dependency goes one way. An extension is dependent of the other extension it uses. By “use”, I mean:

  • actions, conditions and expressions the extension calls
  • behavior types the extension requires by parameters or properties
  • object types the extension requires by parameter or child-object

Extensions can not be dependent of the scene events. The system doesn’t allow it. Calling an action of the extension from scene events makes the scene dependent to the extension not the other way around.
You can’t do much if you don’t want to call extension actions. For instance, the platformer extension would be useless if you hide all its actions.

The Y-sort is a special case where no interaction is needed and your feature request is to access other object instances, so it’s out of topic.

Choosing which behavior type you want to see all the object instances would just be a generalization of what you ask. It falls in the same case to me (the 1st feature in my previous message).

The 2nd feature is AldebaranStudio’s suggestion.

It sounds like it would be easier to cut the object in isometric cube tiles though. Looping on every pair of object instances is usually not a great idea because if you have 1,000 instances, you have to check 500,000 pairs.

You can add behavior parameters after the object parameter to tell which behaviors you require like I suggested in a previous message.

Not exactly the same request, but accomplishing what I’m proposing would likely resolve any issues OP has:

We should add the ability to use objects as properties in behaviors. Being able to have an object as a property would allow us to, for example, make the behavior automatically create, destroy, or otherwise interact with instances of the selected object, including through lifecycle events.

For example, you could make it so that on an object’s destruction, you create a particle emitter. Currently, to do so, you need to add the creation of that theoretical particle emitter to the event that deletes the object. And if you want to do that with something like a tween or other timer-based actions, you need to either create yet another event to delete the object at the end of the tween, or set a timer that creates the particle emitter at the exact moment of the tween’s end. Both approaches bloat the number of events needed to do something that is relatively simple.

The advantage of doing it that way is that not only can you pick the object arbitrarily through properties (thus not limiting you to just objects in the same extension).

Alternatively, the ability to create an object from it’s name without referring to a specific group would equally solve the issue, as you could simply pass the object’s name as a string through properties.

currently properties can be defined through the extension, this means that you would not know whether the object actually exist in the game/scene that it is used in. until of course, its defined on the scene.

so… it would need to a a special property, that is blank before being assigned. (though its a cool idea) i don’t know how much does it follow GD’s philosophy.

You can also call a function that plays the explosion animation and delete the object:

I think it’s easier to follow what the events do this way. Especially because when something explode there are likely actions to apply on players or the environment so you need an event in any case.