0 votes

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")
related to an answer for: invalid get index "Lu" (on base 'array')
asked Nov 30, 2018 in Projects by Lynn_Len (41 points)
edited Nov 30, 2018 by Lynn_Len

2 Answers

0 votes
    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)
answered Nov 30, 2018 by Zylann (26,139 points)
 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')"

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.

0 votes

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
    
  2. Add some checks of player array size to write_component method, i.e.

    if player.empty():
        return
    
  3. 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.

answered Dec 1, 2018 by leonidboykov (56 points)
edited Dec 1, 2018 by leonidboykov
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.