Register    Login    Forum    FAQ    PinSimDB.org

Board index » Resources » Tutorials, how-to's, bug report and docs




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: [script] Usefull util classes (List, Map, GenericTimer)
 Post Posted: Tue Jan 18, 2011 6:37 am 
Offline
User avatar

Joined: Fri Jul 02, 2010 4:49 am
Posts: 481
Location: Lyon, France
Some years ago, I started a script engine for FP, but it's unfinished so I've decided to release some parts as standalone scripts.

:arrow: Summary

Class List: a dynamic array for storing object of any kind. Very useful as you don't care of the init size, and you can insert and remove at any index.

Class Map : a key/value pair dictionnary

Class GenericTimer : a universal timer, base on only one FP Timer Object, that allow to schedule events and code. It needs class List above.

edit : now at pinsimdb : http://www.pinsimdb.org/pinres/entry-21 ... nerictimer)

_________________
PinSimDB.org projects : PinSimDB.org - FPM Editor - FPWine - Speed Devils table


Top 
 Profile  
 
 Post subject: Re: [script] Usefull util classes (List, Map, GenericTimer)
 Post Posted: Tue Jan 18, 2011 6:38 am 
Offline
User avatar

Joined: Fri Jul 02, 2010 4:49 am
Posts: 481
Location: Lyon, France
:arrow: Class List : A ordered dynamic list of objects

File : util/List.vbs

Unit test file : test/ListTest.vbs

Methods (index is integer, value must be a object or struct) :
- index = addValue(value)
- set value = getValue(index)
- replaceValue index, value
- insertValue index, value
- set value = removeValue(index)
- set value = removeFirstValue()
- set value = removeLastValue()
- size = getSize()

Sample code (for a complete sample, see ListTest.vbs) :

Code:
ExecuteGlobal LoadExternalScript ("util\List.vbs")

class struct
   public text
end class

sub test()
   dim objList
   dim tmp
   dim i

   set objList = new List
   for i=1 to 3
      set tmp=new struct
      tmp.text="elem " & i
      objList.addValue(tmp)
   next
end sub


Tips :
- You can use the List as a LIFO or FIFO stack using methods addValue(), removeFirstValue(), removeLastValue()

_________________
PinSimDB.org projects : PinSimDB.org - FPM Editor - FPWine - Speed Devils table


Top 
 Profile  
 
 Post subject: Re: [script] Usefull util classes (List, Map, GenericTimer)
 Post Posted: Tue Jan 18, 2011 6:38 am 
Offline
User avatar

Joined: Fri Jul 02, 2010 4:49 am
Posts: 481
Location: Lyon, France
:arrow: Class Map : A key (String) / value (Object) dictionary

File : util/Map.vbs

Unit test file : test/MapTest.vbs

Methods (key is string, value must be a object or struct) :

- index = putValue key, value
- set value = getValue(key)
- set value = removeValue(key)
- exists = isValueExist(key)
- index = getIndex(key)
- size = getSize()

faster methods by using index (so act also like a List):

- set value = getValueByIndex(index)
- set value = removeValueByIndex(index)
- replaceValueByIndex index, value
- key = getKeyByIndex(index)


Sample code (for a complete sample, see MapTest.vbs) :

Code:
ExecuteGlobal LoadExternalScript ("util\Map.vbs")

class struct
   public text
end class

sub test()
   dim objMap
   dim tmp
   dim i

   set objMap = new Map

   for i=0 to 2
      set tmp=new struct
      tmp.text="elem " & i
      objMap.putValue "key"&i, tmp
   next

   AddDebugText "EXIST = " & objMap.isValueExist("key1")
end sub


Tips :
- Map must be reserved for complex needs, but are very powerfull. You can access it like a List when you have known indexes.

_________________
PinSimDB.org projects : PinSimDB.org - FPM Editor - FPWine - Speed Devils table


Top 
 Profile  
 
 Post subject: Re: [script] Usefull util classes (List, Map, GenericTimer)
 Post Posted: Tue Jan 18, 2011 6:38 am 
Offline
User avatar

Joined: Fri Jul 02, 2010 4:49 am
Posts: 481
Location: Lyon, France
:arrow: Class GenericTimer : A single timer for generic purpose

File : util/GenericTimer.vbs

Unit test file : test/GenericTimerTest.vbs

Methods list:

- init(timerObjectName)
- addEvent(code, delay)
- expired()

Usage :

Code:
dim objGenericTimer
set objGenericTimer = new GenericTimer
objGenericTimer.init(YourTimer)
Sub YourTimer_Expired()
   objGenericTimer.expired()
End Sub



Sample code (for a complete sample, see GenericTimerTest.vbs) :

Code:
ExecuteGlobal LoadExternalScript ("util\List.vbs")
ExecuteGlobal LoadExternalScript ("util\GenericTimer.vbs")

dim objGenericTimer
dim start

sub test()
   set objGenericTimer = new GenericTimer
   objGenericTimer.init(TestTimer)

   start = Timer() * 1000
   objGenericTimer.addEvent "test1(0)", 0
   objGenericTimer.addEvent "test1(1000)", 1000
   objGenericTimer.addEvent "test1(2000)", 2000
end sub


Tips :
- Use only one Time FP object. I made it the most precise vbs could be.
- You can schedule any type of code, but calling a subroutine with argument is I think the best way to go.
- A good side effect is you can launch a code after exiting the current sub FP is running by setting a delay to 0 :
Code:
Sub MyKicker_Hit

  ' do some stuff, light, etc ...
 
  ' after handling the kicker event, start a mode
  objGenericTimer.addEvent "startMultiBallMode", 0
 
  ' Add some score
  addScore(5000)
  dmdUpdate()
  ' ....'
 
End Sub

This way the startMultiBallMode subroutine will be call actually after MyKicker_Hit is completeley finished. It could avoid some conflict to delay a code after a subroutine end.

_________________
PinSimDB.org projects : PinSimDB.org - FPM Editor - FPWine - Speed Devils table


Top 
 Profile  
 
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 4 posts ] 

Board index » Resources » Tutorials, how-to's, bug report and docs


Who is online

Users browsing this forum: No registered users and 1 guest

 
 

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to: