+2 votes

Hello!
I want to create a function that parses a .txt file which encodes different in-game objects (something like a map).
For example the file'd look like this:

#wall#, 10, 10, 'grasswall.png', rect_collide=true, gravity=true;
&player&, 20, 20, hitpoints=3;
#tile1#, 20, 200, 'brick_tile.png', rect_collide=false, gravity=false;

etc.
So a file which holds all the static objects in the game.
Now I need to add those objects to the 2D game space.
How do I create, say, a static object with collider and given texture from code and
then display it at desired position in the game?
I am making a platformer game with levels created by users.
(I know how to parse my format, but how do i open it and then create
those objects when the game opens?)

in Engine by (14 points)

1 Answer

+8 votes

new static body: StaticBody2D.new()
new collision shape : CollisionShape2D.new()
add make collision shape work in static body your_static_body.add_child(your_collision_shape)
creating sprite: Sprite.new()
setting texture to sprite: your_sprite.texture = your_texture
"gluing" sprite to static body: your_static_body.add_child(your_sprite)
setting position your_static_body.global_position = Vector2(10,10)

to open file you do File.new().open(path_to_your_file, File.READ)
then you can read it e.g. your_file.get_as_text()
and rememer to close it your_file.close()

by (1,022 points)

Hi... Spot on reply... Thank you... I have a follow up question can I add my custom made sprite via coding I mean with _draw() commands? Thanks again

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 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 webmaster@godotengine.org with your username.