0 votes

I'm making a card game, that requires the player to pick a card, confirm, and change the position. I was using the get_viewport().get_mouse_position() to find the mouse position, and comparing it to the card position, if it was >= the card position, and <= the card size, this card was selected, but I don't know how to discover the card position at the scene, I tried to use the rect_position property, but isn't working.

Does someone here know how to discover if the mouse was clicked inside the card area?

Main Scene script:

extends Control

onready var BlackCardText = $GamePlace/BlackCard/RichTextLabel
onready var BlackCardPick = $GamePlace/BlackCard/Escolha/RichTextLabel
onready var Hand = $GamePlace/Hand/GridContainer
onready var WhiteCard = preload("res://WhiteCard.tscn")

var BlackListDictionaire = {
    1:["se", 1],
    2:["mata", 1],
    3:["barreto", 1],
    4:["veras", 2],
    5:["pprt", 2],
}
var WhiteList = ["se","mata","barreto","veras","pprt",]
var rng = RandomNumberGenerator.new()

func _ready():
    set_black_card()
    set_hand(3)


func _input(event):
    if Input.is_action_just_pressed("testblack"):
        set_black_card()
    if Input.is_action_just_pressed("testwhite"):
        set_hand(1)

func set_black_card():
    rng.randomize()
    BlackCardText.text = BlackListDictionaire[rng.randi_range(1,5)][0]
    BlackCardPick.text = str("Escolha ", str(BlackListDictionaire[rng.randi_range(1,5)][1]))

func set_hand(value):
    rng.randomize()
    for i in range(value):
        var Whitecard = WhiteCard.instance()
        Hand.add_child(Whitecard)
        Whitecard.set_text(WhiteList[rng.randi_range(0,4)])

Card scene script:

extends Control

func set_text(text):
    $RichTextLabel.text = text

func _input(event):
#   print(self.rect_position)
    if Input.is_action_just_pressed("mouse1"):
        var mouse_position = get_viewport().get_mouse_position()
        if mouse_position.x >= self.get_position().x && mouse_position.x <= (self.get_position().x + self.rect_size.x):
            if mouse_position.y >= self.get_position().y && mouse_position.y <= (self.get_position().y + self.rect_size.y):
                print("OI")

Main Scene: http://i.prntscr.com/n-NlcB2ZQV24YnbuKOjhuQ.png
Card Scene: http://i.prntscr.com/TRY66Rv6SaG6fSo6Is2JvQ.png

Godot version 3.2.3.stable
in Engine by (20 points)

1 Answer

0 votes

You can use Area2D node built in signals called mouse_entered() and mouse_exited() to do that.

by (430 points)
edited by

I tried to use the area 2d as a child of my control card node, but it seems that is not responding

responding to what?

I pass the mouse trough the area 2d, but the signal is not activated

you can see if you hover over the signal that it requires the area node pickable option active and at least one collision layer bit to be set

I have the collision layer, but I need to activate this pickable option ?

yes you can see in the screenshot I took
https://imgur.com/a/RZJaane

so, i need to write a function inside the area2d func set_pickable(true) ?

you can do it in the inspector under the collision layers there's the option for Pickable
https://imgur.com/a/cWm7knE

It's set true, but isn't working, maybe is because i'm using an area2d inside of a control node ?

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.