0 votes

I have a Grid, which extends TileMap, and I'm trying to spawn panels in. Panels extend Area2D.

The hierarchy:
Main scene -> player grid scene -> grid scene -> [player scene, x number of panel scenes (added via spawn_panels function)].

Please note that these are all the above are added in the 2D editor, and only the panels are added via script.

To spawn the panels, I have this function ran in _ready in Grid:

func spawn_panels(charType):
  var panelInit = null
  var panel = null
  var childPanel = null

  if charType == PLAYER:
      panelInit = AllyPanel
  elif charType == ENEMY:
      panelInit = EnemyPanel

  for x in grid_s_x:
      for y in grid_s_y:
          panel = panelInit.new(x, y)
          self.add_child(panel)
          panel.set_owner(self)
          panel.set_name("panel" + str(x) + str(y))
          panel.position = map_to_world(Vector2(x, y)) + cell_size / 2

However, the panels are not visible when the game is ran. I have printed the tree and can see that they are added properly, and also verified that they are not added out of bounds as I've hard coded their position to 0, 0 and the player position (player is visible) but it is still not visible.

Any help is appreciated, thanks!

in Engine by (21 points)

what does the panelInit.new() function do?

it makes a new instance of it I believe.

1 Answer

0 votes
Best answer

Turns out adding a .new() instance doesn't work, so you must either preload or load your scene, call .instance() in order for it to show up when you add it.

by (21 points)
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.