How to add a completely new object from code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Szustarol

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?)

:bust_in_silhouette: Reply From: Bartosz

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()

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

swagatam98 | 2020-09-11 03:25