Like the Skills code, these code snippets are independent codes that can provide functionality to various game projects (specifically designed for RPG's). Each one includes a demo as well. Rem out or delete the demo before using. These are all Open Source and available for anyone to use. I will be using this thread to post more code (instead of creating new threads for each snippet).
==============================================
Player Text Buffer: Control the speed of your output text to the player using these usefull functions. Includes a safety feature that prevents immediately duplicated messages from being entered into the buffer.
Rem *** Include File: Player_Text_Functions.dba ***
Rem Created: 5/18/2006 11:15:19 PM
`The purpose of these functions is to create a dynamic text buffer that can be
`used to send messages to the player at a reasonable pace and to help prevent
`accidental duplicate messages.
`======================================================
`=========================Demo=========================
`======================================================
`Rem the demo out before using.
Sync On:Sync Rate 60
Initialize_Player_Text()
Add_Player_Text("This demonstrates...")
Add_Player_Text("how the text buffer...")
Add_Player_Text("works. There should...")
`Notice this line is repeated? It will not be repeated on the output.
Add_Player_Text("how the text buffer...")
Add_Player_Text("how the text buffer...")
Add_Player_Text("how the text buffer...")
Add_Player_Text("be a slight pause...")
Add_Player_Text("between text printing.")
Add_Player_Text("======================")
Do
m$=Pull_Next_Player_Message()
if m$<>""
Print m$
Endif
Manage_Player_Messages()
`Attempt to add this message every loop. It does not get added for a short while.
Add_Player_Text("Testing input delay")
Sync
Loop
End
`======================================================
`=======================End Demo=======================
`======================================================
`======================================================
`=======================Functions======================
`======================================================
`Intiialize the Player_Text_Buffer
Function Initialize_Player_Text()
Dim Text_Buffer(-1) as TextBuffer
Endfunction
Type TextBuffer
Message as String
Printed as Word
Endtype
`Usefull constants
`Returns the Text_Buffer array size
#Constant Text_Buffer_Size Array Count(Text_Buffer())
`The delay in cycles before the next message is printed.
#Constant Text_Delay 60
`How many cycles to retain the last message.
#Constant Text_Retain 1000
`Use this function to add text to the buffer
Function Add_Player_Text(txt as string)
if txt="" Then Exitfunction
i=Find_Player_Text(txt)
if i=>0 Then Exitfunction
Array Insert At Bottom Text_Buffer()
Text_Buffer(Array Count(Text_Buffer())).message=txt
Endfunction
`This function is used by the previous fucntion to determine if the text is already in the list.
`The information is then used to prevent duplicating a message to the buffer.
Function Find_Player_Text(txt as string)
If Text_Buffer_Size <0 Then Exitfunction -1
index=-1
For i = 0 to Text_Buffer_Size
If Text_Buffer(i).message=txt
index=i
Exit
Endif
Next i
Endfunction index
`This function is used to pull the next message from the buffer (first in first out).
`It also set the Printed decay counter to 1, starting the message decay.
`It will not print the message until the alloted amount of cycles has passed.
Function Pull_Next_Player_Message()
If Text_Buffer_Size <0 Then Exitfunction ""
If Get_Last_Decay()>0 and Get_Last_Decay()< Text_Delay Then Exitfunction ""
index=-1
For i =0 to Text_Buffer_Size
If Text_Buffer(i).Printed=0
index=i
exit
Endif
Next i
if index<0 then Exitfunction ""
msg$=Text_Buffer(index).message
Text_Buffer(index).printed=1
Endfunction msg$
`This function decays messages in the buffer. Once a message has decayed enough, it is removed from the buffer.
`Message decay is a safety catch to prevent duplicate messages from entering buffer too quickly after the intial message.
`Message decay is also used to determine the timing before the next message is printed.
Function Manage_Player_Messages()
If Text_Buffer_Size <0 Then Exitfunction
For i = 0 to Text_Buffer_Size
If Text_Buffer(i).Printed>0
Inc Text_Buffer(i).Printed
Endif
If Text_Buffer(i).Printed> Text_Retain
Array Delete Element Text_Buffer(),i
Endif
Next i
Endfunction
`This function is used to determine the 'decay' since the last message.
Function Get_Last_Decay()
If Text_Buffer_Size <0 Then Exitfunction 0
max=1001
For i = 0 to Text_Buffer_Size
If Text_Buffer(i).Printed>0 and Text_Buffer(i).Printed<Max
max=Text_Buffer(i).Printed
Endif
Next i
Decay=max*(max<1001)
Endfunction Decay
==============================================
Object Buttons: Take the concept of 2D buttons and apply it to 3D objects and you have object buttons. This self contained system allows you to assign an object as a Button Object, giving it a unique index value, a specific "Return Value" (which is usefull for categorizing), a Name, and and activation range. This code manages when the object is in range, when the mouse is over a specific Object Button, or can return which Object Button the mouse is over. It also manages depth priority (closer objects have priority over distant objects). If an object is behind another button object, it will not 'register'. Or if the object is too far away, it will not register. The demo demonstrates all the functionality. Rem out or delete the demo before using the code.
Rem *** Include File: object_buttons.dba ***
Rem Created: 5/6/2006 11:47:24 PM
Rem *****************************************
Rem *******************DEMO******************
Rem *****************************************
Sync On:Sync Rate 60:Autocam Off
`Initialize the object_buttons (Step 1)
Initialize_Object_Buttons()
`Make the objects and assign them as object_buttons (Step 2)
For i = 1 to 20
Make Object Cube i,5
Position Object i,Rnd(100)-50,-2,Rnd(100)-25
Make_Object_Button(i,Int(i/5)+1,50,"Box"+Str$(i))
Next i
Do
`These are all the controls that are really needed
Control Camera Using Arrowkeys 0,.3,1
`Call the Active_Object_Button() function (Step 3)
Active=Active_Object_Button()
If Active>-1
If Active_Button(0).Click=1 Then Turn Object Right Active_Button(0).ObjectID,1
If Active_Button(0).Click=2 Then Turn Object Left Active_Button(0).ObjectID,1
Endif
`And this is extra, but not needed, to display information. You can rem this section out.
Set Cursor 0,0
if Active>-1
Print "Active Button"
Print "Return Value= ";Active_Button(0).Return_Value
Print "Index = ";Active_Button(0).Index
Print "Object = ";Active_Button(0).ObjectID
Print "Object Range= ";Int(Active_Button(0).Range)
Print "Object Name= ";Active_Button(0).Name
Print "Mouseclick= ";Active_Button(0).Click
Else
Print "Last Active Button"
Print "Return Value= ";Last_Active_Button(0).Return_Value
Print "Index = ";Last_Active_Button(0).Index
Print "Object = ";Last_Active_Button(0).ObjectID
Print "Object Range= ";Int(Last_Active_Button(0).Range)
Print "Object Name= ";Last_Active_Button(0).Name
Print "Mouseclick= ";Last_Active_Button(0).Click
Endif
imax=Array Count(Object_Button())
ink rgb(0,0,180),0
for i = 0 to imax
If Object In Screen(Object_Button(i).ObjectID)
Center Text Object Screen X(Object_Button(i).ObjectID),Object Screen Y(Object_Button(i).ObjectID)-10,Get_Object_Button_Name(i)
Endif
Next i
ink rgb(255,255,255),0
`Wrap up the loop.
Sync
Loop
End
Rem *****************************************
Rem ***************END DEMO******************
Rem *****************************************
`Object buttons are used to turn objects into clickable buttons that return a value.
`This is very usefull for allowing the player to click on an object and have something happen.
`The value returned is determined at the time the object button is created.
`Also, the distance the object is from the camera can be specified in order for the object button to be "active"
`Note: Using Intialize_Object_Buttons clears out any previously defined Object Buttons. No objects are deleted or harmed.
Function Initialize_Object_Buttons()
UnDim Object_Button()
Dim Object_Button(-1) as Object_Buttons
UnDim Object_Button_Name()
Dim Object_Button_Name(-1) as Object_Button_Names
Dim Active_Button(0) as ActiveButton
Dim Last_Active_Button(0) as ActiveButton
Null= Make Vector3(998)
EndFunction
Type Object_Buttons
ObjectID as Word
Return_Value as Integer
Range as Float
NameID as Word
EndType
Type Object_Button_Names
Name as String
ButtonID as Word
EndType
Type ActiveButton
Index as Word
ObjectID as Word
Return_Value as Integer
Range as Float
Name as String
Time as DWord
Active as Boolean
Click as Byte
Endtype
`Make_Object_Button assigns an object an Object_Button return value, a range, and a name
`If the ObjectID is not a valid existing Object Number, the function will fail and return a -1
`If the ObjectID is already assigned to an Object Button, the function will reassign that Object Button's values.
`Set the Range value to 0 or a negative value to allow for unlimited range.
`Setting the range can help speed up the code by reducing the number of object needed to be checked for.
`Naming objects is possible, but not always recommended as the text exchange may slow down the code considerably.
`However, the reference to the name (and vise-a-versa) use array index values to speed up the overall reference exchange.
`Use an empty string ("") to avoid assigning an Object Name.
`There is a limit of 1000 Object_Buttons available.
`The function returns the Object_Button_Index value, but it is not needed. However, using the index reference may increase
`the speed of the code since, without the index value, the index value needs to be looked up.
Function Make_Object_Button(ObjectID as Word,ReturnVal as Integer, Range as Float, Name as String)
index as Integer
index=-1
If Object_Is_Valid(ObjectID)=0 Then ExitFunction index
index = Array Count(Object_Button())+1
if index>999
index=-1
Exitfunction index
endif
flag=0
If index>0
i=Get_Object_Button_Index(ObjectID)
If i=-1
flag=0
Else
index=i
Endif
Endif
If Flag=0
Array Insert At Bottom Object_Button()
Endif
Object_Button(index).ObjectID=ObjectID
Object_Button(index).Return_Value=ReturnVal
Object_Button(index).Range=Range
If Len(name)>0
Array Insert At Bottom Object_Button_Name()
NameIndex=Array Count(Object_Button_Name())
Object_Button_Name(NameIndex).Name=Name
Object_Button_Name(NameIndex).ButtonID=index
Object_Button(index).NameID=NameIndex
Else
Object_Button(index).NameID=-1
Endif
Endfunction index
`Returns the Index of the Object, or -1 if the object is not an Object_Button
Function Get_Object_Button_Index(ObjectID as Word)
index as Integer
index=Array Count(Object_Button())
if index<0 then Exitfunction -1
flag=0
For i = 0 to index
if Object_Button(i).ObjectID=ObjectID
flag=1
index=i
exit
endif
Next i
if flag=0 then index=-1
Endfunction index
`Returns the Index of the first button assigned the name provided.
`This could be very slow if there are a lot of named Object Buttons.
Function Get_Object_Button_Index_By_Name(name as String)
index as Integer
index=Array Count(Object_Button_Name())
if index<0 then Exitfunction index
flag=0
For i = 0 to index
if Object_Button_Name(i).Name=Name
flag=1
index=i
exit
endif
Next i
if flag=0
index=-1
Exitfunction index
Endif
index=Object_Button_Name(i).ButtonID
Endfunction index
`This function returns the Name of the object by providing the Index
Function Get_Object_Button_Name(Index as integer)
If Button_Index_Valid(Index)=0 then Exitfunction ""
NameID=Object_Button(index).NameID
Button_Name$=""
If NameID>-1
Button_Name$=Object_Button_Name(NameID).Name
Endif
Endfunction Button_Name$
`This function returns the Name of the object by providing the ObjectID
Function Get_Object_Button_Name_By_Object(ObjectID as word)
index=Get_Object_Button_Index(ObjectID)
If Button_Index_Valid(Index)=0 then Exitfunction ""
NameID=Object_Button(index).NameID
Button_Name$=""
If NameID>-1
Button_Name$=Object_Button_Name(NameID).Name
Endif
Endfunction Button_Name$
`This function safely returns the Object of the Object Button by providing the Index
Function Get_Object_Object_Button(Index as Integer)
If Button_Index_Valid(Index)=0 then Exitfunction 0
ObjectID = Object_Button(Index).ObjectID
Endfunction ObjectID
`This function returns the Object of the Object Button by providing the Name.
`Caution: this could be slow if there are a lot of assigned Object Button names.
Function Get_Object_Object_Button_By_Name(Name as String)
Index=Get_Object_Button_Index_By_Name(Name)
If Button_Index_Valid(Index)=0 then Exitfunction 0
ObjectID = Object_Button(Index).ObjectID
Endfunction ObjectID
`Returns a 1 if the index is valid, otherwise a 0 is returned.
Function Button_Index_Valid(Index as Integer)
flag=(index=>0 and index<=Array Count(Object_Button()))
Endfunction flag
`Safely checks if an integer is a valid object number and that object exists. 1=Valid Object, 0=Not Valid.
Function Object_Is_Valid(ObjectID as Integer)
If ObjectID<1 Then Exitfunction 0
If Object Exist(ObjectID)=0 Then Exitfunction 0
Endfunction 1
`Returns the range of the object_button from the camera using the index.
Function Get_Button_Range(index)
Obj=Object_Button(index).ObjectID
r#=-1
If Obj=0 Then Exitfunction r#
If Object_Is_Valid(Obj)=0 Then Exitfunction r#
Set Vector3 998,Camera Position X()-Object Position X(Obj),Camera Position Y()-Object Position Y(Obj),Camera Position Z()-Object Position Z(Obj)
r#=Length Vector3(998)
Endfunction r#
`Returns the index of the closest Object Button in range currently under the mouse. Returns a -1 if no button is under the mouse.
`Also completes the array Active_Button(0) with the active button values. The Active_Button(0).Time is the timer value when
`the array is last set. This way you can determine how long ago the button was last active. Active_Button(0).Active is a flag that
`is set if the object_button stored in the array is currently active, otherwise it is set to 0.
Function Active_Object_Button()
imax=Array Count(Object_Button())
if imax<0 then Exitfunction -1
flag=-1
d#=99999
for i=0 to imax
If Object_Is_Valid(Object_Button(i).ObjectID)
If Object In Screen(Object_Button(i).ObjectID)
r#=Get_Button_Range(i)
if Object_Button(i).Range<=0 or Object_Button(i).Range>=r#
f=Pick Object(MouseX(),MouseY(),Object_Button(i).ObjectID,Object_Button(i).ObjectID)
If f>0 and r#<d#
flag=i
d#=r#
Endif
Endif
Endif
Endif
Print
Next i
If Flag=-1
`Store last button results
If Active_Button(0).Active=1
Last_Active_Button(0).Index=Active_Button(0).Index
Last_Active_Button(0).ObjectID=Active_Button(0).ObjectID
Last_Active_Button(0).Return_Value=Active_Button(0).Return_Value
Last_Active_Button(0).Range=Active_Button(0).Range
Last_Active_Button(0).Name=Active_Button(0).Name
Last_Active_Button(0).Time=Active_Button(0).Time
Last_Active_Button(0).Click=Active_Button(0).Click
Endif
Active_Button(0).Active=0
Else
`Store the Active Button Index
Active_Button(0).Index=Flag
`Store the active button Object Number
Active_Button(0).ObjectID=Object_Button(Flag).ObjectID
`Store the Active Object's Return Value
Active_Button(0).Return_Value=Object_Button(Flag).Return_Value
`Store the Active Object's Range
Active_Button(0).Range=d#
`Store the Active Object's Name
Active_Button(0).Name=Get_Object_Button_Name(Flag)
`These next values are useful for delayed reactions or storing data.
`Store the Active Object's most recent activation timestamp
Active_Button(0).Time=Timer()
`Set the Active flag to indicate the Object_Button stored in this array is currently active or not.
Active_Button(0).Active=1
`Store mouse button state when the button was active
Active_Button(0).Click=MouseClick()
Endif
Endfunction Flag
==============================================
Feedback is appreciated. Let's me know if I should post more.
Enjoy.
Open MMORPG: It's your game!