Hi!
I am a GDevelop beginner and trying different things.
Among other things I use an external event in 3 scenes to center the camera on an object. I do this to keep a layer always centered even with larger screen width.
Now the code of this external event, used individually, has a different result than if I execute this code via an event.
Here is the code:
For example, when I go back to the start screen, the code in the event creates another, new “Center” object.
If I store the exact code separately at “At the beginning of the screen”, then (as desired) no new “Center” object is created.
How can I explain this?
Is the code executed differently in external events?
Hi, I did not quite understand very much the question, can you please explain in more details what is currently happening and what should happen?
If I understand correctly, you said that this event works as expected when at the regular scene eventsheet, but not when called from an external event?
Why do you use “Among objects center, create object named Center” instead of the regular “Create object Center” action?
Currently, what is the method that you use to know that multiple Centers are being created, since they are being hidden?
Is the start screen a different scene? If yes, are you using the “Change the scene” action or the “Go back to previous paused scene” one?
If you are changing the scene via “Change the scene” then it is really supposed to create a Center object if the external event is being called.
Yes, to make it visible, I make 2 screenshot with a non hidden “Center” object (a little square),
The camera is centered dynamically to this sqare even after resizing the window, it works great:
If I visit this game scene a second time, you can see the camera is still centered but the layer content is not (happens if I use the code via an external event):
PS: The second weird thing now is, that it does now not create a second square, like I saw and wrote it in my initial post!
I just followed a tutorial on YT, I tried the method now that you have mentioned and I accidentially used now the hard coded game resolution width – and now it works!
So the variable SceneWindowWidth() and alternative ScreenWidth doesn’t contain the right value.
Yes, sorry, for the test I have remove the “Hide” action. But in my tests after this post, I now can see only one square!
But what is unclear to me, are there more square instances overlapping each other?
In the console I only find a way to log the start of “At the beginning of the scene”
And it fires 2 times (as I expected).
But I suppose after loading another scene all instances in it are removed, right?
The example project uses “Change the scene”.
Yes that is correct!
So what I found out until now is:
The behavior has nothing to do with the use of an external event, because after some tests it occurs even if I use the commands directly!
The behavior has only to do with the used functions “SceneWindowWidth()” and “SceneWindowHeight”.
Likewise with “ScreenWidth()” and “ScreenHeight()”.
I have recorded a video here that shows how the values of SceneWindowWidth() and SceneWindowHeight() in the debug text change afterward when I return to the start scenen and change the width of the game window:
Once I hardcode the values, the code works exactly the same (and the way I want it to) when used separately or via external event.
In so far my problem is solved, but for me the question remains: Why do the two functions above return different values depending on whether I use them inside an external event or directly in the scene?
I think the reason the object is created in a different position is because you are entering that scene with a different window size every time, and it will create it at the camera center (which is the window center), so…