- Joined
- Jul 3, 2017
- Messages
- 251
That is basically my question. Let me explain what I want to do...
I want to create a "virtual bag" where I can drop all sorts of things: weapons, potions, armor, scrolls... whatever. Then I just do a "grab" and take a random item from it.
Is there a way a "List" can hold multiple classes instead of a single one?
So far, this is what I have, but I don't think it will work.
But I don't think this will work. I know, I can always make all classes 'extend' from a single one and use *that* in the list. But I want an "all purpose" solution, that I could drop on any project and use it, without this 'caveat'.
Is there a way to do this in Cerberus? Maybe if I replace T with "Object", would it work?
I want to create a "virtual bag" where I can drop all sorts of things: weapons, potions, armor, scrolls... whatever. Then I just do a "grab" and take a random item from it.
Is there a way a "List" can hold multiple classes instead of a single one?
So far, this is what I have, but I don't think it will work.
Code:
Class Bag<T>
Field list:List<T>
Method New()
list = New List<T>
End Method
Method add(value:T)
list.AddLast(value)
End Method
Method remove(value:T)
list.removeFirst(value)
End Method
Method clear()
list.Clear()
End Method
Method get:T()
Local c:Int = list.Count()-1
If c<=0 Then Return Null
Local item:T
Local r:Int = Rand(0, c)
Local i:Int = 0
For item = Eachin list
If i = r Then Return item
i+=1
Next
Return Null ' error?
End Method
End Class
But I don't think this will work. I know, I can always make all classes 'extend' from a single one and use *that* in the list. But I want an "all purpose" solution, that I could drop on any project and use it, without this 'caveat'.
Is there a way to do this in Cerberus? Maybe if I replace T with "Object", would it work?
Last edited: