Hello I have this issue where I have 8 of a prop (one for each player in the game basically) and I want it so that someone can take it and use it then when they’re done it returns back to the selection if that makes sense. So if someone walked up they would take the one in the 0 spot then someone else could come take the one in the 1 spot then the first guy could bring back 0 making it 0,2,3,4,5,6,7 the first one available in the list would always be the first used I hope I’m making any sense thank you!
This looks like a stack.
You’d Pop the topmost prop, and Push to the stack when a player returns a Prop.
I haven’t tested this, but here are some ways you could implement a stack depending on your preference.
With an inheritable base class.
# Base class for a stack for any type that you might want to implement.
stack(t:type) := class<abstract>:
Push(Item:t):void
Pop()<decides>:t
# A stack of creative_props. Easy to use since it supports internal mutability of the items.
prop_stack := class(stack(creative_prop)):
var Items:[]creative_prop = array{}
Push<override>(Item:creative_prop):void=
set Items += array{Item}
Pop<override>()<decides><transacts>:creative_prop=
Ret:creative_prop = Items[Items.Length - 1]
set Items = Items.RemoveElement[Items.Length - 1]
Ret
# Usage
var MyPropStack:prop_stack = prop_stack{}
MyPropStack.Push(Prop)
if (PopProp := MyPropStack.Pop[]):
Print("Popped prop")
With extension methods applied to any array type.
# Push extension method for arrays.
(Input:[]t where t:type).Push(Item:t):[]t=
Input + array{Item}
# Pop extension method for arrays.
(Input:[]t where t:type).Pop()<decides><transacts>:tuple([]t, t)=
Ret:t = Input[Input.Length - 1]
(Input.RemoveElement[Input.Length - 1], Ret)
# Use it like so:
var ExtArray:[]int = array{1, 2, 3}
set ExtArray = ExtArray.Push(4)
if:
ExtArrayPop := ExtArray.Pop[]
then:
set ExtArray = ExtArrayPop(0)
Value := ExtArrayPop(1)
With a generic stack class.
# Generic stack that supports any type.
generic_stack(t:type) := class():
Items:[]t = array{}
Push(Item:t):generic_stack(t)=
generic_stack(t){ Items := Items + array{Item} }
Pop()<decides><transacts>:tuple(generic_stack(t), t)=
Ret:t = Items[Items.Length - 1]
(generic_stack(t){ Items := Items.RemoveElement[Items.Length - 1] }, Ret)
# Use it like so:
var MyStack:generic_stack(creative_prop) = generic_stack(creative_prop){}
set MyStack = MyStack.Push(Prop)
if (StackPop := MyStack.Pop[]):
set MyStack = StackPop(0)
Prop = StackPop(1)
You’d call the Pop/Push depending on your setup, like how you reference the props etc.
Keep in mind this Pops/Pushes from the end of the array, so you’d want to build it in reverse order → 5, 4, 3, 2, 1, 0
5 Likes
Thank you so much for such an in depth answer I really appreciate it!!
2 Likes
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.