PDA

View Full Version : How to parse multiple optional parameters?



Michael Hartlef
17-03-2008, 16:50
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.




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

Petr Schreiber
17-03-2008, 17:10
Hi Mike,

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

The most appropriate one could be:


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


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 :D

Michael Hartlef
17-03-2008, 17:33
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 :)

Petr Schreiber
17-03-2008, 17:35
;D

Scary scene, I would not like to see that movie :D

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

Petr Schreiber
17-03-2008, 17:46
If it would not work,

you can always do it the old way:


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


thinBasic_CheckComma_Optional is the key in this case.


Bye,
Petr

Michael Hartlef
17-03-2008, 18:00
Thanks, first version works like a charm. :)

ErosOlmi
17-03-2008, 18:07
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" (http://images.google.com/images?hl=en&q=masterchief&um=1&ie=UTF-8) already reply ;D

Michael Hartlef
17-03-2008, 18:36
No boss, everything is under control. I'm on my track :D