Is it possible to place a StaticBody from code?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By cleeverz
:warning: Old Version Published before Godot 3 was released.

Hello there!

I’ve built a StaticBody2D body called wall in my little game experiment Godot, that just sits there and blocks collision. It’s working fine.

The next thing I wanted to do was to build a node called wall_ manager that places instances of these walls as a border around the screen. However I’ve run up against this:

Here’s the code just to place one wall:

func _ready():
	var wall = wall_scene.instance()
	var wall_offset = Vector2(12, 12)
	wall.set_position(get_global_pos() + wall_offset)
	add_child(wall)

And here is the error produced:

Invalid call. Nonexistent function 'set_position' in base 'StaticBody2D'.

On the one hand, StaticBodies aren’t meant to be moved, so this makes sense, but on the other hand, if I want to put a static body into the game via code, which seems reasonable, this leaves me wondering how.

Is there a way to do this that I’m missing? Or is a StaticBody maybe the wrong construct for this? I could use a Tilemap maybe? But I only have one image right now and that seems like a bulky object and a lot of extra coding to do something as simple as what I’m trying to do.

Thanks for your time!

:bust_in_silhouette: Reply From: kidscancode

I’m assuming you’re using Godot 2.1.4.

All Node2D derived nodes can have and set a position. The problem you’re having is the function is named set_pos(). See the docs here: Node2D.set_pos()

So you would need to say

wall.set_pos(get_global_pos() + wall_offset)

Ha! well, I guess it’s somewhat appropriate for my first question on here to be one that gets filed under duh. I knew that was that right function name, I just didn’t notice in the code or the error that I was using the full word instead! I’ll fix this tonight when I’m home. Thanks!

Sorry for forgetting to give my Godot version number as well, I’ll do my best to remember that for future questions.

cleeverz | 2018-01-08 17:22

There is a help button with all the class reference and search option, if you do not find something in the node you are using, look at one of the parent classes, in this case, Node2D (not all with description in 2.1 but all methods, signals and constants names are present).

eons | 2018-01-09 02:51