UnderwareDESIGN

PlayBASIC => Beginners => Topic started by: stevmjon on January 26, 2012, 01:05:41 AM

Title: combine two words to make an array?
Post by: stevmjon on January 26, 2012, 01:05:41 AM
call me dumbo, but i forgot how to do this.

been searching the forums, but can't find the example program i saw a while ago with this in it.

eg. need "anim" + "01" to become an array anim01(x,x)

  thanks for any help,  stevmjon
Title: Re: combine two words to make an array?
Post by: kevin on January 26, 2012, 04:59:23 AM
 You can't do that.

What are you trying to do ?

 

 Here's a bit of example that creates a manager of sorts that uses NAMES (Strings) rather than an numeric reference like an INDEX or pointer.

[pbcode]

   Type tAnimationSystem
         Name$
         NameLC$
         NameHash
         Handle
         FrameCount
         CurrentFrame
   EndType

   Dim Animations(0) as tAnimationSystem


   // -----------------------------------------------------
   // Make some junk empty animations
   // -----------------------------------------------------

   for lp=0 to 10
      Anim_Tommy      =NewAnimation("Tommy"+str$(lp))
   next

   // -----------------------------------------------------
   // Make animation and fill with of some frames
   // -----------------------------------------------------
   Anim_Bill      =NewAnimation("Bill")
   For angle =0 to 180 step 15
         Cls
         Circle 50,50,50*sin(angle),true
         ThisIMage=getFreeImage()
         GetImage ThisImage,0,0,100,100
         AddAnimationFrame("BILL",ThisImage)
   next


   // -----------------------------------------------------
   //   Run a bit if a demo
   // -----------------------------------------------------

   Setfps 30
   Do
      Cls $102030

      DrawAnimationFrame("Bill",MouseX(),MouseY())
      
      print Fps()
      Sync       
   loop



   


// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
//                               >> New Animation <<
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------


Function NewAnimation(Name$)

   ; check if this animation already exists ? but anyway
   ; it if does,it just dumps an error to the debug console  

   Index=FindAnimationNameIndex(Name$)
   if Index=0   

      ; Get a free posiiotn in the array for this resource
      index =GetFreeCell(Animations())

      ; make the lower case version of the Name ID$      
      NameLC$=Lower$(Name$)

      ; Store the name as is/ in lower case and make a search hash out of it
      Animations(Index).name$         =Name$
      Animations(Index).nameLC$      =NameLC$
      Animations(index).NameHash      =zMakeNameHash(NameLC$)

      ; create a 1D integer array to hold the frames in this animation
      Animations(index).Handle       =zMakeFrameArray()   

      ; the number of frames in this animation
      Animations(Index).frameCount   =0
      
      ; current frame in animation set
      Animations(Index).Currentframe=0

   else
      #print "Error - Animation Name ["+Name$+"] ALready Exists "
   endif

EndFunction Index




// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
//                               >> Draw Animation Frame <<
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------

Function DrawAnimationFrame(Name$,Xpos,Ypos)
      Index=FindAnimationNameIndex(Name$)
      if Index      
         FrameCount=Animations(index).frameCount
         
         if FrameCount

            CurrentFrame=Animations(index).CurrentFRame

            MakeArray LocalFrames()
            LocalFrames()=Animations(index).Handle

            DrawImage LocalFrames(CurrentFrame),Xpos,Ypos,True
            
            Animations(index).CurrentFRame=Mod(CurrentFRame+1,FrameCount)


         endif

      endif
EndFunction



// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
//                               >> Add Animation Frame <<
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------

Function AddAnimationFrame(Name$,ImageIndex)
      Index=FindAnimationNameIndex(Name$)
      if Index      

            MakeArray LocalFrames()
            LocalFrames()=Animations(index).Handle

            FrameCount=Animations(index).frameCount
            
            if FrameCount+1=>GetArrayElements(localFrames())
               redim localFrames(FrameCount+128)      
            endif

            localFrames(FrameCount)=IMageIndex
            Animations(index).frameCount=FrameCount+1

      else
            ; Error this animation doesn't exiust
            #print "Error - Animation Name ["+Name$+"] Doesn't exist"
      endif
      
EndFunction



// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
//                          >> Find Animation Name Index <<
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
//   This Function scans the animation array looking for the Index of the
//  animation name the user wants to access.


Psub FindAnimationNameIndex(Name$)
   Index=0
   NameLC$=Lower$(Name$)
   NameHash=zMakeNameHash(NameLC$)
   for lp =1 to GetArrayElements(Animations())
      if Animations(lp)
         if Animations(lp).NameHash=NameHash
            if Animations(lp).NameLC$=NameLC$
               Index =lp
               exitfor
            endif
         endif         
      endif
   next
EndPsub Index


// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
//                               >> Make Name Hash <<
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------


Psub zMakeNameHash(NameLC$)
      Size=len(NameLC$)      
      MidPosition=Size/2
      Chr1=mid(NameLC$,1)
      Chr2=mid(NameLC$,MidPosition)
      Chr3=mid(NameLC$,Size)
      Hash=ARgb(Size,Chr1,Chr2,Chr3)
EndPsub Hash


// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
//                               >> Make Frame Array <<
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------

Psub zMakeFrameArray()   
   Dim Frames(256)
EndPsub Frames()

[/pbcode]

  The trouble with such an approach, is that looking up the "NAME" and constantly converting back to a reference can be costly when put under heavy stress.  There are ways around that, but really bypassing the bottle neck is better if you can.

  A simple modification would be to offer ways of accessing the material via Name or via it's reference, so you'd look up where the material is initially by name,  then access it directly.    Using names is very useful though, in particular when externalizing our data from the program.    So a character description might refer to an animation, or a movement path by name.    Which is uses in the Thesius XIII demo that comes with PB..  

  This one is a slight tweak to remove the Name to ID look ups
[pbcode]


   Type tAnimationSystem
         Name$
         NameLC$
         NameHash
         Handle
         FrameCount
         CurrentFrame
   EndType

   Dim Animations(0) as tAnimationSystem


   // -----------------------------------------------------
   // Make some junk empty animations
   // -----------------------------------------------------

   for lp=0 to 10
      Anim_Tommy      =NewAnimation("Tommy"+str$(lp))
   next

   // -----------------------------------------------------
   // Make animation and fill with of some frames
   // -----------------------------------------------------
   Anim_Bill      =NewAnimation("Bill")
   For angle =0 to 180 step 15
         Cls
         Circle 50,50,50*sin(angle),true
         ThisIMage=getFreeImage()
         GetImage ThisImage,0,0,100,100
         AddAnimationFrame(Anim_Bill,ThisImage)
   next


   // -----------------------------------------------------
   //   Run a bit if a demo
   // -----------------------------------------------------

   Setfps 30
   Do
      Cls $102030

      DrawAnimationFrame(Anim_Bill,MouseX(),MouseY())
      
      print Fps()
      Sync       
   loop



   


// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
//                               >> New Animation <<
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------


Function NewAnimation(Name$)

   ; check if this animation already exists ? but anyway
   ; it if does,it just dumps an error to the debug console  

   Index=FindAnimationNameIndex(Name$)
   if Index=0   

      ; Get a free posiiotn in the array for this resource
      index =GetFreeCell(Animations())

      ; make the lower case version of the Name ID$      
      NameLC$=Lower$(Name$)

      ; Store the name as is/ in lower case and make a search hash out of it
      Animations(Index).name$         =Name$
      Animations(Index).nameLC$      =NameLC$
      Animations(index).NameHash      =zMakeNameHash(NameLC$)

      ; create a 1D integer array to hold the frames in this animation
      Animations(index).Handle       =zMakeFrameArray()   

      ; the number of frames in this animation
      Animations(Index).frameCount   =0
      
      ; current frame in animation set
      Animations(Index).Currentframe=0

   else
      #print "Error - Animation Name ["+Name$+"] ALready Exists "
   endif

EndFunction Index




// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
//                               >> Draw Animation Frame <<
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------

Function DrawAnimationFrame(Index,Xpos,Ypos)
      if LegalAnimationIndex(index)
         FrameCount=Animations(index).frameCount
         
         if FrameCount

            CurrentFrame=Animations(index).CurrentFRame

            MakeArray LocalFrames()
            LocalFrames()=Animations(index).Handle

            DrawImage LocalFrames(CurrentFrame),Xpos,Ypos,True
            
            Animations(index).CurrentFRame=Mod(CurrentFRame+1,FrameCount)


         endif

      endif
EndFunction



// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
//                               >> Add Animation Frame <<
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------

Function AddAnimationFrame(Index,ImageIndex)
      if LegalAnimationIndex(index)

            MakeArray LocalFrames()
            LocalFrames()=Animations(index).Handle

            FrameCount=Animations(index).frameCount
            
            if FrameCount+1=>GetArrayElements(localFrames())
               redim localFrames(FrameCount+128)      
            endif

            localFrames(FrameCount)=IMageIndex
            Animations(index).frameCount=FrameCount+1

      else
            ; Error this animation doesn't exiust
            #print "Error - Animation Name ["+Name$+"] Doesn't exist"
      endif
      
EndFunction



// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
//                          >> Find Animation Name Index <<
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
//   This Function scans the animation array looking for the Index of the
//  animation name the user wants to access.


Psub FindAnimationNameIndex(Name$)
   Index=0
   NameLC$=Lower$(Name$)
   NameHash=zMakeNameHash(NameLC$)
   for lp =1 to GetArrayElements(Animations())
      if Animations(lp)
         if Animations(lp).NameHash=NameHash
            if Animations(lp).NameLC$=NameLC$
               Index =lp
               exitfor
            endif
         endif         
      endif
   next
EndPsub Index



Psub LegalAnimationIndex(index)
   Status=0
   if Index>=1 and  Index<=GetArrayElements(Animations())
      if Animations(index) then Status=1
   endif
EndPsub Status

// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
//                               >> Make Name Hash <<
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------


Psub zMakeNameHash(NameLC$)
      Size=len(NameLC$)      
      MidPosition=Size/2
      Chr1=mid(NameLC$,1)
      Chr2=mid(NameLC$,MidPosition)
      Chr3=mid(NameLC$,Size)
      Hash=ARgb(Size,Chr1,Chr2,Chr3)
EndPsub Hash


// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
//                               >> Make Frame Array <<
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------

Psub zMakeFrameArray()   
   Dim Frames(256)
EndPsub Frames()

[/pbcode]


 or you can do something like this, which i suspect is what you're thinking of..   Which is method of using an array stub to access data from another array.  Here we're declaring a bunch of arrays and then processing them all via a for/next loop.  

[pbcode]

   Dim Anim1(10)
   Dim Anim2(20)
   Dim Anim3(30)
   Dim Anim4(40)
   Dim Anim5(50)

   For lp=GetArray(Anim1()) to GetArray(Anim5())
   
      MakeArray ReferenceArray()
      SetArray ReferenceArray(),lp      
   
      print GetarrayElements(ReferenceArray())
   
   next
   
   Sync
   waitkey


[/pbcode]





Title: Re: combine two words to make an array?
Post by: stevmjon on January 26, 2012, 07:51:23 PM
QuoteWhat are you trying to do ?

basically, i want to create seperate arrays automatically for storing sets of image numbers.
i want to keep the 'anim_' but change the '01' , '02' , '03' , '04' etc as i need them.

> beetle walk :   anim_01(20)
> beetle turn :    anim_02(6)
> mine throw :   anim_03(10)
> explosion    :   anim_04(30)

i have been doing this manually at the moment. i don't want to do this. i want the program to automatically create each array as it needs it, so i don't have to keep adding them manually in the code here and there in the correct place.

i used to do this automatically in my older code using a big array. i allowed max of 30 frames, and set an array to store all of it. eg. anim_frames(x,30). i kept the frame numbers in the 2nd dimension, and just re-dimmed the 1st dimension to add more characters. but, this wasted a lot of array space. eg. some animations would use 10 elements out of possible 30 elements (so 20 elements will never be used/filled). i could use this again, but i thought it wasted unused space in the big array.

thanks for your reply, i will read through your examples.

 stevmjon
Title: Re: combine two words to make an array?
Post by: kevin on January 27, 2012, 01:43:01 AM
 I'd probably use something similar to the STRING NAME approach posted above.   So the animation data can live externally to the program code and more importantly characters can refer to animations by name in their external declarations.   None of this stuff really needs to be built into the program per-say.

So the animation declarations might just be a text file, set out in the structure with some keywords.  Text is ideal for this as it lets us add/edit animation data, without changing a single line of program code.  

Ie.


; --------------------------------------------
; Animation Declarations
; --------------------------------------------

beetle walk
{
  FileName="Beetle Walk"
  FileType="png"
  Frames=16
  Speed = 45
}




Now the game engine loads the text into memory, runs through it grabbing the data initializing the material in the internal animation array.   It's generally best to keep the data in such structures as minimal as possible.  So the program, constructs say the actual file name of the material for example.  

Characters can  be defined much the same way, but for animations or perhaps sounds etc, we use the material name.  This way if items share a material, we don't need to change the items if a change to that materials is required, we change the material.    Anyway, so the character loader reads the data in, then looks up the materials it uses by name.  Beyond this point, we refer to the material by it's media index/pointer rather than name.  So the naming stuff is purely for human convenience at design time.


Example,  Entity Example (Loading Game Levels) (http://www.underwaredesign.com/forums/index.php?topic=3680.0)

Title: Re: combine two words to make an array?
Post by: stevmjon on January 27, 2012, 04:57:35 PM
thanks kev.  i haven't done a lot with loading & saving files. now i have more options.

  stevmjon
Title: Re: combine two words to make an array?
Post by: kevin on January 31, 2012, 03:42:19 PM

well, rather than store this data inside the program...  save yourself some heartache and write a little no thrills editor to set the properties of your character data say.   Then just include the  loader routine in your game.