A few noob questions regarding my card game (just started Godot)
Asked Answered
R

8

0

Hi, all, just started with Godot and even though i have some Unity experience (yeah, i know...) there were a few things I couldn't figure out:

  1. I made my Main node/scene which has canvas_layer, a colorRect background and hbox for card slots (each slot is basically a colorRect with a button to draw a card). I assumed (correct me if I'm wrong) that the best node for a simple card is ColorRect which i created on a separate node. When i try to draw/instantiate the colorRect node, i do not see it. I played around with the ordering but nothing. I have the same problem when i create a card as label, sprite2D, normal/neutral node, control node. It worked fine when I made my card out of canvas_layer node with colorRect as a child. However, I do not understand why?

The script is attached to my Main node and I use this very simple instantiate function i found online:

extends Node2D

var myCard = preload("res://layer_and_node.tscn")

func spawn():
var instance = myCard.instantiate()
add_child(instance)

func _on_button_pressed():
spawn()

  1. Regarding the function, what is "add_child(instance)" for?

  2. I tried to spawn/instantiate my card at the place of my slot by creating a default position which i link to the position of the slot but I couldn't make it work.

Any tips are appreciated!

Retral answered 7/10, 2023 at 10:8 Comment(0)
F
0

in the future if you get an error message please include it in your original post. Also including the line number were the error happens would be useful. I assume it is var slot_position = $CanvasLayer/HBoxContainer/ColorRect.position. This happens because at the time when the node is initialised, the node is not part of the scene tree yet. An attempt to access other nodes (ColorRect in this case) will fail. You can fix this by prefixing the line with a @onready annotation (assuming you are using Godot 4).

btw. you should consider using a PanelContainer instead for the background of the card.

Floating answered 7/10, 2023 at 11:30 Comment(0)
F
0

add_child adds the newly created node or scene to the scene tree. This is necessary for having the node/scene in the game. This is also necessary to avoid a resource leak.

You don't show any code for positing your card, so it is impossible to tell what is going wrong.

Floating answered 7/10, 2023 at 11:3 Comment(0)
R
0

Floating I see, thank you! Here is what i tried to do:

extends Node2D

var mySprite = preload("res://layer_and_node.tscn")

var slot_position = $CanvasLayer/HBoxContainer/ColorRect.position 

func spawn(pos):
	var instance = mySprite.instantiate()
	add_child(instance)
	instance.position = pos
	

func _on_button_pressed():
	print("hi")
	spawn(slot_position)

And i get 1 Error: "Invalid get index 'position' (on base: 'null instance'). "

Retral answered 7/10, 2023 at 11:18 Comment(0)
F
0

in the future if you get an error message please include it in your original post. Also including the line number were the error happens would be useful. I assume it is var slot_position = $CanvasLayer/HBoxContainer/ColorRect.position. This happens because at the time when the node is initialised, the node is not part of the scene tree yet. An attempt to access other nodes (ColorRect in this case) will fail. You can fix this by prefixing the line with a @onready annotation (assuming you are using Godot 4).

btw. you should consider using a PanelContainer instead for the background of the card.

Floating answered 7/10, 2023 at 11:30 Comment(0)
R
0

Floating Thank you, I will do that. There was no number for the error line but i think you are correct, after your edit i get different error in the following line ( I assume) : "instance.position = pos"
"Invalid set index 'position' (on base: 'CanvasLayer') with value of type 'Vector2'."

Retral answered 7/10, 2023 at 12:16 Comment(0)
M
0

The line that causes the error should be highlighted in the editor. Identifying that line in your post is helpful in determining the problem. The number is not helpful unless the lines of code that you post are numbered.

Also, if you place ~~~ on the lines before and after your posted code, the code will be indented properly.

Mitchum answered 7/10, 2023 at 12:23 Comment(0)
R
0

Mitchum Thank you!

Retral answered 7/10, 2023 at 12:37 Comment(0)
F
0

Is the root node of res://layer_and_node.tscn a CanvasLayer? That can not work. CanvasLayer doesn't have a position. Also, you probably shouldn't have CanvasLayers everywhere. One CanvasLayer or at most one CanvasLayer for a related group of Controls is enough.

Floating answered 7/10, 2023 at 12:40 Comment(0)
R
0

Floating I will go back to the tutorials to figure it out as it seems that i've approached the whole project in a pretty erroneous way. I appreciate your input and your help!

Retral answered 7/10, 2023 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.