Itemmanager

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

Itemmanager

Postby mrminecrafttnt » Nov 11, 2019 9:34

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: 1726
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Itemmanager

Postby Imortis » Nov 13, 2019 0:51

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: 1123
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Itemmanager

Postby SARG » Nov 13, 2019 8:55

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: 553
Joined: Mar 10, 2007 15:44
Location: Ohio, USA
Contact:

Re: Itemmanager

Postby bcohio2001 » Nov 13, 2019 15:44

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

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 1 guest