error but this time it's for the "-1"

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

i get the same invalid error for “-1”, when i remove it i again get the same error but for “1”

     extends Control 

onready var bg_rect = get_node("Media_Sys/BG_Rect")
onready var char_sprite = get_node("Media_Sys/Sprite")
onready var text_box = get_node("Dialog_Sys/Dialog_Text")
onready var text_nam = get_node("Dialog_Sys/Label/Name")
onready var click = get_node("ClickManager")

var player = []

var lu = "Lu"
var ciel = "Ciel"

func _ready():
    click.request_ready()


func write(char_name_str, text_str=null):
    if text_str == null:
        text_str = char_name_str
        char_name_str = ""
    player.push_front({"type": "dialogue","name": char_name_str, "text": text_str})
    print("!!!")
func write_component():
    text_nam.clear()
    text_box.clear()
    text_nam.add_text(player[player[player.size() - 1]["name"]])
    text_box.add_text(player[player[player.size() - 1]["text"]])
    player.pop_back()

func _input(event):

    if event is InputEventMouseButton:
        write_component()

extends Node

onready var g = get_parent()

var lu = "Lu"
var ciel = "Ciel"


func _ready():
    g.write("Lu", "hello")
:bust_in_silhouette: Reply From: Zylann
    text_nam.add_text(player[player[player.size() - 1]["name"]])

Didn’t you mean this instead?

    text_nam.add_text(player[player.size() - 1]["name"])

Or shorter:

    text_nam.add_text(player[-1].name)
 text_nam.add_text(player[player[player.size() - 1]["name"]])

I still get the invalid when i do this.

text_nam.add_text(player[-1].name)

I get a different invalid error “Invalid get index ‘-1’ (on base: ‘int’)”

Lynn_Len | 2018-11-30 20:42

That second error you get is very confusing. It basically means player is an integer, but you declared it as an array, so… Oo
I don’t understand what’s going on. It’s like some other place in your code sets player to be an integer.

Zylann | 2018-11-30 21:30

:bust_in_silhouette: Reply From: leonidboykov

I’ve tried your code in Godot.

Seems, your problem is related to input handling. if event is InputEventMouseButton fires write_component() twice: on mouse press and on mouse release. You getting an error because you’re deleting an entry from player array.

Try to do the following things

  1. Add event type checks to _input method, i.e.
    if event is InputEventMouseButton and !event.pressed:
        # code on ANY mouse button release
        # remove `!` to handle a mouse press event
  1. Add some checks of player array size to write_component method, i.e.
    if player.empty():
        return
  1. You still have to access your data without using player twice.

P.S.: print() method is really handy to debug input events. And you should never access an array by index before checking its length.