0 votes

I have created simple 2D Arcade game for mobile device. Where as player can move left and right to avoid obstacles that is coming from top to bottom. My obstacles are sticking around the left and right edges of the mobile screen. This is a simple prototype, also It is working file.

Here is the Image File

The problem with this approach is all obstacles are added at run time with some logic. Also I have to perform some calculation on obstacles to fit on different screen size of mobile screen. The runtime math calculation on obstacles goes really big.

To avoid this I planned to go for the second solution But don't know how to achieve this in godot engine.

Here is my second solution
Instead of creating Obstacles at runtime and attaching it to the edges of the screen, I want to create a new scene that includes all the Obstacles placed inside of it as ready made one. In need I can attach this obstacles scene as a child of the main scene so that player can play around. But HERE COMES THE PROBLEM.

How can I create obstacles scene separately that can fit for different screen size?
Note [ Portrait is the only mode for mobile device]

in Engine by (113 points)

You actually have not described what problem you are facing in the second solution. No mindreaders around today :(

I am new to Godot and GdScript. Creating things dynamically itself is difficult for me but somehow I can manage creating things with editor. I am looking for runtime solution with easiest way.

If so, maybe you just may change a stretch mode of your whole application in [ Godot > Project > Project settings > Display > Window > Stretch > Mode | Aspect | Stretch ]

1 Answer

+1 vote
Best answer

I would..

A) create a scene for an Obstacle with a script with a func setHole(left :int, width :int, angle :float)

B) prepare few obstacles during first game loading (based on your sketch, you'll need ~8 of them?).

C) recycle obstacles going out of screen, set new hole via setHole, place them above the top of the game scene and make them move to bot again


If your obstacles are going to be made out of textures (fancy animations, pixel perfect graphics or so) you may create multiple types of obstacle scene and recycle few types.

If your holes are not going to be quads only, you also may want to prepare some types with different func setHole eg. setHoles(holes :Array) for multiple holes and so on.

I do not believe (based on your sketch) there are some heavy calculations needed for obstacle placing during runtime. May you elaborate on this point? What calculations are you perfrorming currently in solution 1?

In case you are going to have so many different types of obstacles (with saws, lasers, goos, spikes, two holes etc.) you may prepare that many obstacle scenes, precache (save to array) them per type and duplicate them via .duplicate() during loading. This way you'll go your heavy calculations once per obstacle only (I suppose that calculations are needed only for left and right sides of obstacles).

by (206 points)
selected by

From my point of view positioning, scaling, distancing parameters are seems complex. I will try to create simple obstacle as a scene with script attached init and add it to the scene at runtime as you said and will recycle it for complex levels.
This could help me. Thanks for your reply.

What is .dublicate() function you explained above. Is that same as scene.instance() can you explain it?

You can duplicate nodes with https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-duplicate

.instance() is not an approach I recommend because I adviced you to prepare your obstacles (set them correct widths) prior the game runnin and duplicate them on runtime. This way you do not need to calculate widths (what you believe is cpu heavy). If you'd .instance() new scenes for every obstacle, you'd have to calculate widths for them every time they are about to appear.

Feel free to share a full code if you need futher help, it's going to be easier to help you.

As you suggest I recycle my obstacles to have predefined with and height. So it will not calculated at runtime. Along with your approach duplicating node can save lots of cpu usage. Definitely I will use this approach. Good one :)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.