0 votes

I have 2 ItemList nodes under a general UI node,and they both have an array on them, activecards and inventorycards

On both nodes I have a signal to emit (item_activated) to remove 1 element from the current item list and array,and append it to the other,however godot keeps appending the item removed to both arrays

I have initialiased both arrays with the following format

activecards : Array = Array()
invcards : Array = Array()

on the corresponding node

the code is as follows

onready var Inv = get_parent().get_node("Inventory")

func _on_Inventory_item_activated(index):
    activecards.append(Inv.InvCards[index])
    add_card(activecards[index+1])
    Inv.remove_card(index)
    Inv.InvCards.remove(index)

func add_card(i):
    add_item(i,null,true)

func remove_card(i):
    remove_item(i)

and vice versa on the other one

Am I doing something wrong? I've been stuck on this for 3 days,and append keeps appending to both invcards and activecards,and I've added prints to see if both items are activated with item_activated (cause they are in the same parent) and that's not the case

in Engine by (16 points)
edited by

Hi,
both lists have the same script with the "onInventoryitemactivated"?

no,one has "oninventoryitemactivated" and the other one "onactiveitemactivcated",the signals are different

i think this is an array problem as mentioned in another thread,but even when i initialise the arrays,the .append adds it to both

nah, i dont think so ... i think its a logical error.

You have to provide more code. To realy know whats going on you have to post the code of parent and both itemlists.

As mentioned, more code or a small example project would be needed for someone else to debug it.

This is just speculation, but from the description of your side effects, it sounds like somewhere you may of tried to copy the values of one array to another using activecards = invcards, the equal operator in this case will change the array pointer (its reference to the contents), not copy its values.

Consider the following code, and you'll see how the reference is what changes in the second set of prints. The two variables will both be pointing to the same array in memory.

extends Node2D

func _ready() -> void:

    var arr1 = []
    var arr2 = []

    arr1.append(1)
    arr2.append(2)

    print(arr1)
    print(arr2)

    arr1 = [] # reset values
    arr2 = arr1 # arr2 now points to the same array as arr1

    arr1.append(1)
    arr2.append(2)

    print(arr1)
    print(arr2)

If this is the case and you want to copy values, the API for Arrays has a method to do this: duplicate(bool deep = false)

https://docs.godotengine.org/en/stable/classes/class_array.html#class-array-method-duplicate

this is activecards.gd

extends ItemList

onready var world = get_tree().current_scene

onready var Inv = get_parent().get_node("Inventory")

onready var activecards : Array = Array()

func set_deck():

    for i in activecards :
        add_item(i, null , true)

func add_card(i):
    add_item(i,null,true)

func remove_card(i):
    remove_item(i)

func _on_Inventory_item_activated(index):
    var tempcard = Inv.InvCards[index]
    activecards.append(tempcard)
    add_card(activecards[index+1])
    Inv.remove_card(index)
    Inv.InvCards.remove(index)

this is inventory.gd

extends ItemList

onready var world = get_tree().current_scene

onready var Act = get_parent().get_node("ActiveCards")

onready var InvCards : Array = Array()


func set_deck():
    for i in InvCards :
        print('set"')
        add_item(i, null , true)

func add_card(i):
    add_item(i,null,true)

func remove_card(i):
    remove_item(i)


func _on_ActiveCards_item_activated(index):
    var tempcard = Act.activecards[index]
    InvCards.append(tempcard)
    add_card(InvCards[index+1])
    Act.remove_card(index)
    Act.activecards.remove(index)

im not sure what im doing wrong,this seems good to me

It's probably not where the issue is. It's not clear to me what is being stored in these arrays, so I wonder why you even need them. The items in the ItemList have the option to store metadata, and you could probably use IDs, keys, or references to objects there.

There is no code showing how these arrays initially are initially populated, and you're also using a style of directly modifying properties of your other classes, so I assume there is other code elsewhere that looks like.

Act.activecards = [x, y, z]
Act.set_deck()

Again, I would speculate you're setting both arrays to the same array at some point.

i didn't know about adding meta-data,i think this solves my problem

the arrays were supposed to have the cards inside them,and the player to be able to switch the cards between the "inventory" and the "active set",there is a getcard in player.gd that requests a random card from arrayActive, so i thought i needed to attach the array to the itemlist,instead of treating item_list itself as the array with meta-data

you solved my problem,but not the problem presented with the arrays

like i said that's all the code to the arrays there is,and i tried it with a more distilled problem and i still get the same results

thank you very much,will mark this as answered even thought i don't feel so comfortable doing it

That is ok, I agree, I wouldn't count that as an answer.

Glad you found another approach.

Please log in or register to answer this question.

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.