Need help with Lists (code sample included)

About Monkey 2 Forums Monkey 2 Programming Help Need help with Lists (code sample included)

Tagged: 

This topic contains 14 replies, has 3 voices, and was last updated by  jondecker76 2 years, 9 months ago.

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #1457

    jondecker76
    Participant

    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

    #1458

    jondecker76
    Participant

    Well 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

    #1459

    jondecker76
    Participant

    Looks like I also need help pasting code?  I’ve now copied from Ted2 and Gedit with the same result of newlines being ignored…

    #1460

    jondecker76
    Participant

    </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>

    #1461

    Jesse
    Participant

    use the “<>” above.

    #1462

    jondecker76
    Participant

    That’s what I’m using.  I paste in the code, then when it appears in the textbox all newlines are removed

    #1463

    Jesse
    Participant

    are 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.

    #1464

    jondecker76
    Participant

    No, just the code portion of the post.  very strange

    #1465

    jondecker76
    Participant

    attached 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!

    #1468

    Jesse
    Participant

    uncheck the inline box.

    #1469

    jondecker76
    Participant

    Aaah, 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

    #1470

    Jesse
    Participant

    I 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.

    #1473

    jondecker76
    Participant

    Ok. 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…

    #1474

    ImmutableOctet
    Participant

    For 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.

    #1478

    jondecker76
    Participant

    Ok, 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!

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic.