Results 1 to 8 of 8

Thread: How to parse multiple optional parameters?

  1. #1

    How to parse multiple optional parameters?

    Hi Eros, Roberto and Petr.

    before I spend several hours via trial and error, I wanna ask the masters of module creation first.

    I'm working on a new module to manage game events. I have a function that will have a row of optional
    parameters. How do I parse it? Here is the code so far. Please look at the commented line under the function header.
    This is the format of the command.

    [code=thinbasic]

    '----------------------------------------------------------------------------
    Function EV_AddEvent() As Ext
    'Add ( functionName [,startTime [, eventType [,eventGroup [, activeFlag ]]]]) As eventID
    '----------------------------------------------------------------------------
    Local fname As String
    Local evtype As Ext

    If thinBasic_CheckOpenParens() Then
    thinBasic_ParseString fname
    If thinBasic_CheckComma() Then
    thinBasic_ParseNumber evtype
    If thinBasic_CheckCloseParens() Then

    newevent = memalloc(SizeOf(tEvent))

    eventid = eventid + 1

    @newevent.eventID = eventid
    @newevent.eventGroup = 0
    @newevent.objectID = 0
    @newevent.eventType = evType
    @newevent.startTime = 0
    @newevent.intervalCount = 0
    @newevent.intervalTime = 0
    @newevent.endTime = 0
    @newevent.repeatflag = 0
    @newevent.activeflag = 0
    @newevent.funcName = fname
    @newevent.pUDT = 0
    @newevent.preEvent = lastevent
    @newevent.postEvent = 0

    currevent = newevent
    If eventcount = 0 Then
    firstevent = newevent
    Else
    @lastevent.postevent = newevent
    End If
    eventcount = eventcount + 1
    lastevent = newevent
    'MsgBox (Trim$(@newevent.funcName))

    Function = eventID
    End If
    End If
    End If
    End Function


    [/code]

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: How to parse multiple optional parameters?

    Hi Mike,

    I am definitely still not master of thinBASIC SDK, but there are multiple ways.

    The most appropriate one could be:
    [code=thinbasic]
    Function EV_AddEvent() As Ext
    'Add ( functionName [,startTime [, eventType [,eventGroup [, activeFlag ]]]]) As eventID

    LOCAL fname As STRING
    LOCAL nParsed AS LONG
    LOCAL startTime, eventType, eventGroup, activeFlag AS EXT

    ' -- Default values in case any of them not parsed from script
    startTime = 1
    eventType = 2
    eventGroup = 3
    activeFlag = 4
    nParsed = thinBasic_Parse1StringXNumbers(0, 4, fname, startTime, eventType, eventGroup, activeFlag)

    ' <- code here ->

    function = eventID

    end function
    [/code]

    nParsed returns how many optional parameters were parsed, that can be handy sometimes.
    The thinBasic_Parse1StringXNumbers handles the open parents/close parents automatically.

    I did not tried it, but should work.


    Bye,
    Petr

    EDITED, wrong order of parameters and wrong datatypes
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3

    Re: How to parse multiple optional parameters?

    Thanks Petr. Am I right, the 0 and the 4 means 0 to 4 parameters?

    I am definitely still not master of thinBASIC SDK, but there are multiple ways

    [Imagine scene]
    Scenery: a cold room, tiles on the walls, a big mirror on one, with a table, and two chairs. On the table, a tape recorder and a microphone.
    Peope: Two unknown guys, wearing black suits and black glasses, one standing in a corner, one sitting opposite to Petr, asking him questions:

    Petr: I am definitely still not master of thinBASIC SDK, but there are multiple ways.
    Guy: Riiiiiight, so who is the mastermind behind TBGL. Is it Eros, the italian? Did he told you everything? WE HAVE TO KNOW!!!!!
    Petr: I can't tell you, it's me alone. Eros has nothing to do with it.
    Guy: okeydokey. So you say your created TBGL, but you don't master the SDK. Doesn't it sound fishy to yourself?
    Petr: No! And why should it?
    Guy: Well Petr.... you know, I'm a nice guy. I'm your friend.... you can tell me.
    Petr: Tell what? I told you everything.
    Guy explodes: ENOUGHT!!! If you don't wanna cooporate, then we will......
    Petr: What will you do?
    Guy: Well, there are many nasty things we can do you to your computer.... (big grin on his face)
    [Scene off]

    Thanks Petr

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: How to parse multiple optional parameters?

    ;D

    Scary scene, I would not like to see that movie

    Yes - 0, 4 should mean 0, 1, 2, 3 or 4 parsed numeric parameters; the string ( fName ) is parsed in any case.
    I am not sure about if zero will be accepted, will check.


    Thanks,
    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  5. #5
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: How to parse multiple optional parameters?

    If it would not work,

    you can always do it the old way:
    [code=thinbasic]
    Function EV_AddEvent() As Ext
    'Add ( functionName [,startTime [, eventType [,eventGroup [, activeFlag ]]]]) As eventID

    LOCAL fname AS STRING
    LOCAL startTime, eventType, eventGroup, activeFlag AS EXT

    ' -- Default values in case any of them not parsed from script
    startTime = 1
    eventType = 2
    eventGroup = 3
    activeFlag = 4

    IF thinBasic_CheckOpenParens_Mandatory THEN
    thinBasic_ParseString fname
    IF thinBasic_CheckComma_Optional THEN
    thinBasic_ParseNumber startTime
    IF thinBasic_CheckComma_Optional THEN
    thinBasic_ParseNumber eventType
    IF thinBasic_CheckComma_Optional THEN
    thinBasic_ParseNumber eventGroup

    IF thinBasic_CheckComma_Optional THEN thinBasic_ParseNumber activeFlag

    END IF
    END IF
    END IF

    thinBasic_CheckCloseParens_Mandatory
    ' <- Code here ->

    FUNCTION = ...

    END IF

    END FUNCTION
    [/code]

    thinBasic_CheckComma_Optional is the key in this case.


    Bye,
    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  6. #6

    Re: How to parse multiple optional parameters?

    Thanks, first version works like a charm.

  7. #7
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: How to parse multiple optional parameters?

    Did I miss something?

    I'm very busy at work. I will give you more info later this night even if I saw "master chief" already reply ;D
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  8. #8

    Re: How to parse multiple optional parameters?

    No boss, everything is under control. I'm on my track

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •