About Monkey 2 › Forums › Monkey 2 Programming Help › Need help with Lists (code sample included)
Tagged: List
This topic contains 14 replies, has 3 voices, and was last updated by
jondecker76
2 years, 9 months ago.
-
AuthorPosts
-
July 1, 2016 at 1:51 am #1457
Trying to get a start with Monkey2 by converting a BlitzMax module that I use in a lot of my projects. It’s just a simple sequencing module. I’m just now working on the skeleton of the class, but for the life of me I can’t figure out how to get the topmost <SequenceStep> from the top of the list for reading or writing. I’m just building as a normal file for now as apposed to a real module just to see if I can get past compiler errors, so of course I expect to get an error about there being no Main() etc – just trying to get the Monkey2 syntax down.
Any tips?
thanks!
#rem monkeydoc \##Simple Sequencing Engine This module provides a simple sequencing engine implementation. Each sequence is built by any number of “steps” which can be handled in the order they were created. Each step has convenience methods to assign/track integer step identifiers, store and retrieve a data object, expire steps as well as track/set initialization of each sequence step. Steps operate on a simple FIFO stack, and the functions and methods provided primarily work on the currently active step (which is on top of the stack). #end ‘Namespace jmd.sequence #Rem monkeydoc Sequence class This is the main type you will be dealing with #end #Import “<std>” Using std.. Class Sequence ‘List of sequence steps Field steps:List<SequenceStep> = New List<SequenceStep> #Rem monkeydoc Create a new sequence alternative for CreateSequence() #end Method New() steps.Clear() End #Rem monkeydoc alternative for #SequenceAdd() #end Method Add(stepNum:Int,thisDuration:Int=0,data:Object=Null) ‘If Not list Then list=CreateList() local a:= New SequenceStep() a.info=stepNum a.init=False a.data=data a.timestamp=Millisecs() a.duration=thisDuration steps.AddLast(a) End Method #Rem monkeydoc alternative for #SequenceClear() #End Method Clear:void() If (Not steps=Null) steps.Clear() endif End #Rem monkeydoc alternative for #SequenceGet() #End Method Get:Int() ‘Local a:SequenceStep ‘If Not steps then Return 0 ‘return steps.First().info Local a:SequenceStep=steps.First() Return a.info ‘Return steps.First()<SequenceStep>.info End #Rem monkeydoc alternative for #SequenceData() #End Method Data:Object() If Not steps Return Null ‘Return steps.First().data Local a:=steps.First() Return a.data End Method #Rem monkeydoc Move to the next sequence. #end Method DoNext() steps.RemoveFirst() End Method #Rem monkeydoc bbdoc: alternative for #SequenceInit() #End Method Init:Bool() If Not steps Return 0 Local a:=steps.First() Local b:Bool = a.init a.init=True Return b End Method #Rem monkeydoc alternative for #SequenceReInit() #End Method ReInit() steps.First().init=False End Method #Rem monkeydoc alternative for #SequenceCount() #End Method Count:Int() Return steps.Count() End Method #Rem monkeydoc alternative for #SequenceAutoExpire() #End Method AutoExpire() If Self.Expire() Then Self.DoNext() End Method #Rem monkeydoc alternative for #SequenceExpire() #End Method Expire:Int() Local a:SequenceStep Local deltaT:Int=Millisecs()-steps.First().timestamp Local dur:Int = steps.First().duration If deltaT > dur And dur>0 Return True Else Return False EndIf End Method #Rem monkeydoc alternative for #CreateSequence() Function Create:Sequence() Local a:Sequence a = New Sequence a.list=CreateList() Return a End Function #end End #rem monkeydoc SequenceStep class This type is used behind the scenes by the #TSequence type to store information about each step in the sequence. You should never have to deal with this class directly. #end Class SequenceStep ‘Initialization tracking Field init:Bool ‘Step info (normally step number) Field info:Int ‘Additional data to store with step Field data:Object ‘Autotimeout, True/False Field autotimeout:Bool ‘Millisecs timestamp at creation of step Field timestamp:Int ‘Duration of this step, in Millisecs. ‘<1 is infinite duration Field duration:Int Method New() end End
July 1, 2016 at 1:52 am #1458Well that didn’t work well.. Let me try that code block again
#rem monkeydoc \##Simple Sequencing Engine This module provides a simple sequencing engine implementation. Each sequence is built by any number of “steps” which can be handled in the order they were created. Each step has convenience methods to assign/track integer step identifiers, store and retrieve a data object, expire steps as well as track/set initialization of each sequence step. Steps operate on a simple FIFO stack, and the functions and methods provided primarily work on the currently active step (which is on top of the stack). #end ‘Namespace jmd.sequence #Rem monkeydoc Sequence class This is the main type you will be dealing with #end #Import “<std>” Using std.. Class Sequence ‘List of sequence steps Field steps:List<SequenceStep> = New List<SequenceStep> #Rem monkeydoc Create a new sequence alternative for CreateSequence() #end Method New() steps.Clear() End #Rem monkeydoc alternative for #SequenceAdd() #end Method Add(stepNum:Int,thisDuration:Int=0,data:Object=Null) ‘If Not list Then list=CreateList() local a:= New SequenceStep() a.info=stepNum a.init=False a.data=data a.timestamp=Millisecs() a.duration=thisDuration steps.AddLast(a) End Method #Rem monkeydoc alternative for #SequenceClear() #End Method Clear:void() If (Not steps=Null) steps.Clear() endif End #Rem monkeydoc alternative for #SequenceGet() #End Method Get:Int() ‘Local a:SequenceStep ‘If Not steps then Return 0 ‘return steps.First().info Local a:SequenceStep=steps.First() Return a.info ‘Return steps.First()<SequenceStep>.info End #Rem monkeydoc alternative for #SequenceData() #End Method Data:Object() If Not steps Return Null ‘Return steps.First().data Local a:=steps.First() Return a.data End Method #Rem monkeydoc Move to the next sequence. #end Method DoNext() steps.RemoveFirst() End Method #Rem monkeydoc bbdoc: alternative for #SequenceInit() #End Method Init:Bool() If Not steps Return 0 Local a:=steps.First() Local b:Bool = a.init a.init=True Return b End Method #Rem monkeydoc alternative for #SequenceReInit() #End Method ReInit() steps.First().init=False End Method #Rem monkeydoc alternative for #SequenceCount() #End Method Count:Int() Return steps.Count() End Method #Rem monkeydoc alternative for #SequenceAutoExpire() #End Method AutoExpire() If Self.Expire() Then Self.DoNext() End Method #Rem monkeydoc alternative for #SequenceExpire() #End Method Expire:Int() Local a:SequenceStep Local deltaT:Int=Millisecs()-steps.First().timestamp Local dur:Int = steps.First().duration If deltaT > dur And dur>0 Return True Else Return False EndIf End Method #Rem monkeydoc alternative for #CreateSequence() Function Create:Sequence() Local a:Sequence a = New Sequence a.list=CreateList() Return a End Function #end End #rem monkeydoc SequenceStep class This type is used behind the scenes by the #TSequence type to store information about each step in the sequence. You should never have to deal with this class directly. #end Class SequenceStep ‘Initialization tracking Field init:Bool ‘Step info (normally step number) Field info:Int ‘Additional data to store with step Field data:Object ‘Autotimeout, True/False Field autotimeout:Bool ‘Millisecs timestamp at creation of step Field timestamp:Int ‘Duration of this step, in Millisecs. ‘<1 is infinite duration Field duration:Int Method New() end End
July 1, 2016 at 1:53 am #1459Looks like I also need help pasting code? I’ve now copied from Ted2 and Gedit with the same result of newlines being ignored…
July 1, 2016 at 1:53 am #1460</p><p>#rem monkeydoc<br />\##Simple Sequencing Engine<br />This module provides a simple sequencing engine implementation. Each sequence is<br />built by any number of "steps" which can be handled in the order they were created.<br />Each step has convenience methods to assign/track integer step identifiers, store and retrieve a data object,<br />expire steps as well as track/set initialization of each sequence step. Steps operate on a simple<br />FIFO stack, and the functions and methods provided primarily work on the currently active step<br />(which is on top of the stack).<br />#end<br />'Namespace jmd.sequence</p><p>#Rem monkeydoc Sequence class<br />This is the main type you will be dealing with<br />#end<br />#Import "<std>"</p><p>Using std..</p><p>Class Sequence<br />'List of sequence steps<br />Field steps:List<SequenceStep> = New List<SequenceStep></p><p>#Rem monkeydoc<br />Create a new sequence<br />alternative for CreateSequence()<br />#end<br />Method New()<br />steps.Clear()<br />End</p><p>#Rem monkeydoc<br />alternative for #SequenceAdd()<br />#end<br />Method Add(stepNum:Int,thisDuration:Int=0,data:Object=Null)<br />'If Not list Then list=CreateList()<br />local a:= New SequenceStep()<br />a.info=stepNum<br />a.init=False<br />a.data=data<br />a.timestamp=Millisecs()<br />a.duration=thisDuration<br />steps.AddLast(a)<br />End Method</p><p>#Rem monkeydoc<br />alternative for #SequenceClear()<br />#End<br />Method Clear:void()<br />If (Not steps=Null)<br />steps.Clear()<br />endif<br />End</p><p>#Rem monkeydoc<br />alternative for #SequenceGet()<br />#End<br />Method Get:Int()<br />'Local a:SequenceStep<br />'If Not steps then Return 0<br />'return steps.First().info<br />Local a:SequenceStep=steps.First()<br />Return a.info<br />'Return steps.First()<SequenceStep>.info<br />End</p><p>#Rem monkeydoc<br />alternative for #SequenceData()<br />#End<br />Method Data:Object()<br />If Not steps Return Null<br />'Return steps.First().data<br />Local a:=steps.First()<br />Return a.data<br />End Method</p><p>#Rem monkeydoc<br />Move to the next sequence.<br />#end<br />Method DoNext()<br />steps.RemoveFirst()<br />End Method</p><p>#Rem monkeydoc<br />bbdoc: alternative for #SequenceInit()<br />#End<br />Method Init:Bool()<br />If Not steps Return 0<br />Local a:=steps.First()<br />Local b:Bool = a.init<br />a.init=True<br />Return b<br />End Method</p><p>#Rem monkeydoc<br />alternative for #SequenceReInit()<br />#End<br />Method ReInit()<br />steps.First().init=False<br />End Method</p><p>#Rem monkeydoc<br />alternative for #SequenceCount()<br />#End<br />Method Count:Int()<br />Return steps.Count()<br />End Method</p><p>#Rem monkeydoc<br />alternative for #SequenceAutoExpire()<br />#End<br />Method AutoExpire()<br />If Self.Expire() Then Self.DoNext()<br />End Method</p><p>#Rem monkeydoc<br />alternative for #SequenceExpire()<br />#End<br />Method Expire:Int()<br />Local a:SequenceStep<br />Local deltaT:Int=Millisecs()-steps.First().timestamp<br />Local dur:Int = steps.First().duration</p><p>If deltaT > dur And dur>0<br />Return True<br />Else<br />Return False<br />EndIf<br />End Method</p><p>#Rem monkeydoc<br />alternative for #CreateSequence()</p><p>Function Create:Sequence()<br />Local a:Sequence<br />a = New Sequence<br />a.list=CreateList()<br />Return a<br />End Function<br />#end</p><p>End</p><p>#rem monkeydoc SequenceStep class<br />This type is used behind the scenes by the #TSequence type to store information about<br />each step in the sequence. You should never have to deal with this class directly.<br />#end<br />Class SequenceStep<br />'Initialization tracking<br />Field init:Bool<br />'Step info (normally step number)<br />Field info:Int<br />'Additional data to store with step<br />Field data:Object<br />'Autotimeout, True/False<br />Field autotimeout:Bool<br />'Millisecs timestamp at creation of step<br />Field timestamp:Int<br />'Duration of this step, in Millisecs.<br />'<1 is infinite duration<br />Field duration:Int</p><p>Method New()</p><p>end<br />End</p><p>
July 1, 2016 at 1:53 am #1461use the “<>” above.
July 1, 2016 at 1:55 am #1462That’s what I’m using. I paste in the code, then when it appears in the textbox all newlines are removed
July 1, 2016 at 1:56 am #1463are you using the hyperlink above where you enter your comment?
when you click on it a window pops open and you can paste your code in the code box.
July 1, 2016 at 1:59 am #1464No, just the code portion of the post. very strange
July 1, 2016 at 2:04 am #1465attached images.. This shows my newlines and whitespace showing correctly using the “<>” link above.
After adding it, you can see in the next picture that all formatting disappears!
July 1, 2016 at 2:09 am #1468uncheck the inline box.
July 1, 2016 at 2:12 am #1469Aaah, thanks! I must have misunderstood the intent of “inline”
Anyways, pretty simple class I’m trying to convert… I just can’t for the life of me figure out how to do anything with an object on the top of the list. I keep getting the following error:
error: Type ‘SequenceStep’ cannot be invoked
I get my first error on line 61
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167#rem monkeydoc\##Simple Sequencing EngineThis module provides a simple sequencing engine implementation. Each sequence isbuilt by any number of "steps" which can be handled in the order they were created.Each step has convenience methods to assign/track integer step identifiers, store and retrieve a data object,expire steps as well as track/set initialization of each sequence step. Steps operate on a simpleFIFO stack, and the functions and methods provided primarily work on the currently active step(which is on top of the stack).#end'Namespace jmd.sequence#Rem monkeydoc Sequence classThis is the main type you will be dealing with#end#Import "<std>"Using std..Class Sequence'List of sequence stepsField steps:List<SequenceStep> = New List<SequenceStep>#Rem monkeydocCreate a new sequencealternative for CreateSequence()#endMethod New()steps.Clear()End#Rem monkeydocalternative for #SequenceAdd()#endMethod Add(stepNum:Int,thisDuration:Int=0,data:Object=Null)'If Not list Then list=CreateList()local a:= New SequenceStep()a.info=stepNuma.init=Falsea.data=dataa.timestamp=Millisecs()a.duration=thisDurationsteps.AddLast(a)End Method#Rem monkeydocalternative for #SequenceClear()#EndMethod Clear:void()If (Not steps=Null)steps.Clear()endifEnd#Rem monkeydocalternative for #SequenceGet()#EndMethod Get:Int()'Local a:SequenceStep'If Not steps then Return 0'return steps.First().infoLocal a:SequenceStep=Cast<SequenceStep>(steps.First())Return a.info'Return steps.First()<SequenceStep>.infoEnd#Rem monkeydocalternative for #SequenceData()#EndMethod Data:Object()If Not steps Return Null'Return steps.First().dataLocal a:=steps.First()Return a.dataEnd Method#Rem monkeydocMove to the next sequence.#endMethod DoNext()steps.RemoveFirst()End Method#Rem monkeydocbbdoc: alternative for #SequenceInit()#EndMethod Init:Bool()If Not steps Return 0Local a:=steps.First()Local b:Bool = a.inita.init=TrueReturn bEnd Method#Rem monkeydocalternative for #SequenceReInit()#EndMethod ReInit()steps.First().init=FalseEnd Method#Rem monkeydocalternative for #SequenceCount()#EndMethod Count:Int()Return steps.Count()End Method#Rem monkeydocalternative for #SequenceAutoExpire()#EndMethod AutoExpire()If Self.Expire() Then Self.DoNext()End Method#Rem monkeydocalternative for #SequenceExpire()#EndMethod Expire:Int()Local a:SequenceStepLocal deltaT:Int=Millisecs()-steps.First().timestampLocal dur:Int = steps.First().durationIf deltaT > dur And dur>0Return TrueElseReturn FalseEndIfEnd Method#Rem monkeydocalternative for #CreateSequence()Function Create:Sequence()Local a:Sequencea = New Sequencea.list=CreateList()Return aEnd Function#endEnd#rem monkeydoc SequenceStep classThis type is used behind the scenes by the #TSequence type to store information abouteach step in the sequence. You should never have to deal with this class directly.#endClass SequenceStep'Initialization trackingField init:Bool'Step info (normally step number)Field info:Int'Additional data to store with stepField data:Object'Autotimeout, True/FalseField autotimeout:Bool'Millisecs timestamp at creation of stepField timestamp:Int'Duration of this step, in Millisecs.'<1 is infinite durationField duration:IntMethod New()endEndJuly 1, 2016 at 2:16 am #1470I posted a bug in the development section:
http://monkey2.monkey-x.com/forums/topic/collectionslist-monkey2-error-in-module/#post-1440
fix it and it should take care of most of your errors.
you are going to have to recompile the modules. I think.
July 1, 2016 at 2:20 am #1473Ok. changed the bad #endif and recompiled.
I just realized that Last is implemented as a property instead of a method (kind of strage)! That’s my main problem! Going to see if I can get it to compile now…
July 1, 2016 at 2:25 am #1474For the record, you should only need to rebuild ‘std’ if you changed it, which can be done by simply opening “std.monkey” and building it in Ted or Ted2.
July 1, 2016 at 2:58 am #1478Ok, got it to compile and did a quick test. Looks like it’s working.
It’s going to take a bit of time to get used to the new syntax!
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171#rem monkeydoc\##Simple Sequencing EngineThis module provides a simple sequencing engine implementation. Each sequence isbuilt by any number of "steps" which can be handled in the order they were created.Each step has convenience methods to assign/track integer step identifiers, store and retrieve a data object,expire steps as well as track/set initialization of each sequence step. Steps operate on a simpleFIFO stack, and the functions and methods provided primarily work on the currently active step(which is on top of the stack).#end'Namespace jmd.sequence#Rem monkeydoc Sequence classThis is the main type you will be dealing with#end#Import "<std>"Using std..Class Sequence'List of sequence stepsField steps:List<SequenceStep> '= New List<SequenceStep>#Rem monkeydocCreate a new sequencealternative for CseateSequence()#endMethod New()steps = New List<SequenceStep>End#Rem monkeydocalternative for #SequenceAdd()#endMethod Add(stepNum:Int,thisDuration:Int=0,data:Object=Null)'If Not list Then list=CreateList()local a:= New SequenceStep(stepNum)a.data=dataa.duration=thisDurationsteps.AddLast(a)End Method#Rem monkeydocalternative for #SequenceClear()#EndMethod Clear:void()If (Not steps=Null)steps.Clear()endifEnd#Rem monkeydocalternative for #SequenceGet()#EndMethod Get:Int()'Local a:SequenceStep'If Not steps then Return 0'return steps.First().info'Local a:SequenceStep=Cast<SequenceStep>(steps.First())'Return a.infoReturn steps.First.infoEnd#Rem monkeydocalternative for #SequenceData()#EndMethod Data:Object()Return steps.First.dataEnd Method#Rem monkeydocMove to the next sequence.#endMethod DoNext()steps.RemoveFirst()End Method#Rem monkeydocbbdoc: alternative for #SequenceInit()#EndMethod Init:Bool()If Not steps Return 0If(steps.First.init=True)Return FalseElsesteps.First.init=TrueReturn TrueEndifReturn trueEnd Method#Rem monkeydocalternative for #SequenceReInit()#EndMethod ReInit()steps.First.init=FalseEnd Method#Rem monkeydocalternative for #SequenceCount()#EndMethod Count:Int()Return steps.Count()End Method#Rem monkeydocalternative for #SequenceAutoExpire()#EndMethod AutoExpire()If Self.Expire() Then Self.DoNext()End Method#Rem monkeydocalternative for #SequenceExpire()#EndMethod Expire:Int()Local a:SequenceStepLocal deltaT:Int=Millisecs()-steps.First.timestampLocal dur:Int = steps.First.durationIf deltaT > dur And dur>0Return TrueElseReturn FalseEndifReturn TrueEnd MethodEnd#rem monkeydoc SequenceStep classThis type is used behind the scenes by the #TSequence type to store information abouteach step in the sequence. You should never have to deal with this class directly.#endClass SequenceStep'Initialization tracking'Will return true the first time it is called only (unles it is reinitialized with ReInit())Field init:Bool'Step info (normally step number)Field info:Int'Additional data to store with stepField data:Object'Autotimeout, True/FalseField autotimeout:Bool'Millisecs timestamp at creation of stepField timestamp:Int'Duration of this step, in Millisecs.'<1 is infinite durationField duration:IntMethod New(_info:int)init=Falseinfo=_infodata=Nullautotimeout=Falsetimestamp=Millisecs()endEndFunction Main:Void()Local seq:=New Sequence()seq.Add(1)seq.Add(2)seq.Add(3)Print "Count: " + seq.Count()Print "Current: " + seq.Get()seq.DoNext()Print "New current: " + seq.Get()End -
AuthorPosts
You must be logged in to reply to this topic.

