Itemmanager

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

Itemmanager

Post by mrminecrafttnt »

Here is a simple itemmanager that can save and load Variables like a database.

Syntax:
Databasename.set_item ("Name",Value) 'Sets an Entry if not existent so will an new entry be created
Databasename.get_item ("Name") 'Get an Entry when existent.

Code: Select all

type inventory
    obj_name as string
    obj_amount as integer
end type

type inventory_manager
    declare sub set_item(item_name as string,amount as integer)
    declare function get_item(obj_name as string) as integer
    dim as Inventory Items(any)
end type

sub inventory_manager.set_item(item_name as string,amount as integer)
    dim as integer id = -1
    for i as integer = lbound(Items) to ubound(Items)
        if Items(i).obj_name = item_name then id = i : exit for
    next
    if id = -1 then
        redim preserve Items(ubound(Items)+1)
        Items(ubound(Items)).obj_name = item_name
        Items(ubound(Items)).obj_amount = amount
        exit sub
    else
        with(items(id))
        .obj_name = item_name
        .obj_amount = amount
        end with
    end if
    
end sub

function inventory_manager.get_item(obj_name as string) as integer
    dim as integer id = -1
    for i as integer = lbound(Items) to ubound(Items)
        if Items(i).obj_name = obj_name then id = i : exit for
    next
    if id = -1 then print OBJ_NAME ;" NOT FOUND" : EXIT FUNCTION
    Return Items(id).obj_amount
end function
´
dim as inventory_manager test

test.set_item ("HEALTH",100)
test.set_item ("MANA",50)

print test.get_item("HEALTH")
print test.get_item("MANA")
print test.get_item("BUG")
sleep

Imortis
Moderator
Posts: 1923
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Itemmanager

Post by Imortis »

Very Interesting. Do you have plans to add a file read/write feature for this? It would make things like settings files or save files rather simple to work with.
SARG
Posts: 1755
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Itemmanager

Post by SARG »

Hi,

Just 2 remarks :
- in the "set" function it's useless to set again the name if the item already exists as it's the key to access to the data ....
- probably better, to optimize, to increase the size of the array by 10 or 100 depending the possible number of inputs.
bcohio2001
Posts: 556
Joined: Mar 10, 2007 15:44
Location: Ohio, USA
Contact:

Re: Itemmanager

Post by bcohio2001 »

My mods ….

Code: Select all

Type inventory
	obj_name As String
	obj_amount As Long
End Type

Type inventory_manager
	Declare Sub set_item(item_name As String, amount As Long)
	Declare Function get_item(item_name As String) As Long
	Declare Sub mod_item(item_name As String, amount As Long)
	'
	'internal
	As Inventory Items(Any)
	As Long Count
	Declare Function item_idx(item_name As String) As Long
End Type

Sub inventory_manager.set_item(item_name As String, amount As Long)
	Dim As Long id = item_idx(item_name)
	If id = -1 Then
		ReDim Preserve Items(Count)
		Items(Count).obj_name = item_name
		Items(Count).obj_amount = amount
		Count += 1
	Else
		Items(id).obj_amount = amount
	EndIf
End Sub

Function inventory_manager.get_item(item_name As String) As Long
	Dim As Long id = item_idx(item_name)
	If id = -1 Then Return id
	Return Items(id).obj_amount
End Function

Function inventory_manager.item_idx(item_name As String) As Long
	Dim As Long idx
	While idx < Count
		If Items(idx).obj_name = item_name Then
			'found
			Exit While
		EndIf
		idx += 1
	Wend
	If idx = Count Then Return -1
	Return idx
End Function

Sub inventory_manager.mod_item(item_name As String, amount As Long)
	Dim As Long id = item_idx(item_name)
	If id > -1 Then
		Items(id).obj_amount += amount
	EndIf
End Sub

Dim As inventory_manager test

test.set_item ("HEALTH",100)
test.set_item ("MANA",50)

Print test.get_item("HEALTH")
Print test.get_item("MANA")
'Error should be handled by program. Such as incorrect input of item name.
Print test.get_item("BUG")

'took a hit
Print
test.mod_item("HEALTH", -10)
Print test.get_item("HEALTH")
GetKey
Post Reply