#Using nested classes

29 messages · Page 1 of 1 (latest)

merry fjord
#

What is the error, and can you share your script file ?

woeful karma
#

Card.gd

extends Resource

class_name Card

export var index = 0
export var title = ""

Deck.gd

extends Resource

class_name Deck

export var cards = []

func add_card(card):
    cards.append(card)

func shuffle():
    cards = cards.shuffle()

Up to this point it works

func _ready():
...
  deck.add_card(card)
  card = Card.new()
  #deck.shuffle()
  var output = "Card index: %d - %s" % [deck.cards[i].index, deck.cards[i].title]
  output += "\n"
  output += "Value i: %d Deck total %d" % [i, deck.cards.size()]
  print(output)

#

But when I remove the comment that shuffle function it doesn't work

merry fjord
#

what error are you getting ?

#

or is it just not working as intended ?

woeful karma
#

It crashes when I remove the comment, but as of now, working I still get this

merry fjord
#

try reloading your project in the editor

#

Project -> Reload Current Project

woeful karma
#

I must add that I did change the names of the files

#

I closed and opened the editor but I still have the issue

merry fjord
#

where is the _ready function located ?

woeful karma
#

It said Invalid get index '0'

merry fjord
#

ah

#

your deck might be empty, try doing it in this order instead

var card = Card.new()
deck.add_card(card) 
woeful karma
#

Yeah I do, I declared the card in the top, before the _ready function but I need to remove the data to get a new card

merry fjord
#

oh, this code is inside a for loop ?

woeful karma
#

The deck is also a class that I declared before the ready function and it's in a while loop

#

It's just to text the waters, make a deck so that I can work with the functionality first before I can add the cards and visuals

merry fjord
#

ah i missed this, you're assigning the return value of cards.shuffle() to cards

#

however, the shuffle of Array is a function that doesn't return anything

woeful karma
#

You mean deck.shuffle()?

merry fjord
#

you only need to call cards.shuffle() and that's it

#

no, the shuffle function you're using on the cards Array

woeful karma
#

Or I can work with the cards property within the deck class

#

And just ignore the function I made in Deck class

merry fjord
#

yep you'd have to call deck.cards.shuffle() instead of deck.shuffle() in that case

#

personally i'd keep the deck shuffle() function

#

does it fix the problem ?