How to remove object?

Podge

New Member
Joined
Nov 10, 2019
In this example code I am creating some cards both red and black.
For the life of me I cannot figure out how to remove, lets say all red cards or all black cards.

I've mucked around with things like RemoveFirst and RemoveEach, with no luck at all. Can anyone help me with this?

Strict
Import mojo

Global card_count:Int

Class Game Extends App

Field cards := New Stack<Card>'all cards

Method OnCreate:Int()
SetUpdateRate(60)
For card_count = 0 Until 10'85'create all cards
cards.Push New Card
Next

Return 0
End Method

Method OnUpdate:Int()
Return 0
End Method

Method OnRender:Int()
Cls()
Local y:Int = 0
For Local card := Eachin cards'cycle through all cards
DrawText(card.name, 0, y)
y = y + 10
Next

Return 0
End Method

End

Class Card'class for all cards

Field name:String'name
Method New()'create full deck, 84 cards
If card_count >= 0 And card_count <= 5
name = "Red"
End
If card_count > 4
name = "Black"
End
End Method'end method - New Card

End Class'class card

Function Main:Int()
New Game()
Return 0
End
 

Holzchopf

Moderator
3rd Party Module Dev
Joined
Jul 31, 2017
Location
Bern, Switzerland
Please use code blocks for pasting code (insert, code). Makes it easier to read and doesn't mess up indentation.

Anyways: Stack.RemoveFirst etc. expect a value of type T - in your case T is Card. So you have to pass it the exact card you want to remove, e.g:
Cerberus X:
For card := Eachin cards
    ' this card is named red
    If card.name = "Red" Then
        ' remove its first occurence in cards
        cards.RemoveFirst(card)
    Endif
Next
 

Podge

New Member
Joined
Nov 10, 2019
Hello Holzchopf,

That's it, thanks a lot for that, you have no idea how long I spent trying to figure this out.
I really appreciate the help and I'll be sure to use indentation next time. God knows my code is hard to read at the best of time.

Thanks again,

Take care.
 
Top Bottom