Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: collechess on November 18, 2011, 05:56:49 pm

Title: Inventory System
Post by: collechess on November 18, 2011, 05:56:49 pm
How would one design an inventory system in axe?  I want to be able to easily add and change items and I want each item to have a name, description, type, and 8x8 sprite.  I don't want to just use a lot of if loops.
Title: Re: Inventory System
Post by: ben_g on November 18, 2011, 06:49:21 pm
Inventories aren't that hard to make. You just start with a location to store the data to. Here, I'll use L1, but you can store it to any location you like.

Now, We decide how many spaces we want. Here, I'll use an inventory 4 items wide (32 pixels) and 6 items tall (48 pixels). This will give us an inventory which can carry 24 items, So we'll need 24 bytes to store them (or 12, if a maximum of 16 items is enough for you).

An inventory doesn't contains any information about the items themselves, only the 'ID' of the item, So now we store the items themselves:
This is the way I'd use to store the items: (there might and probably is a more optimised way to do this)

Code: [Select]
.the sprite
[FF00FF00FF00FF00] -> GDB0 .only do the -> GDB0 in the first item
[00] .type of item
[Str000] .pointer to the string containing the name of the item
[Str001] .pointer to string containing the description

.repeat for other items
Now we have the data of the items, and a place to store which items are carried. Now we just need some code to make it useful:

Code: [Select]
.Here we'll make the inventory ready to use
for(T,0,23)
0->{T+L1}
End
.this made the inventory empty, so we won't end up with random items when the game starts

Code: [Select]
.Now we add an item,of which the ID is stored in A, to the inventory
0->T
while(T<24)
if({T+L1}=0
A -> {T+L1}
0->A
24->T
End
T++
End
.I made it so that when the item could be placed in the inventory, A is set to 0. If it couldn't store it becouse the inventory was full, A would still have the original value.

Code: [Select]
.What can also be useful is checking if the inventory contains an item. The ID of the item should be stored to A
0->T
While(T<24)
if({T+L1}=A)
...
.remove those 3 dots and those below the code if you want to delete the item when it's found
0->{T+L1}
...
0->A
24->T
End
T++
End
.A will be 0 when the item was found and will still contian it's original value when it couldn't find the item.

Code: [Select]
.Unless your game is entirely text-based, a way to draw the inventory is also useful
For(T,0,5)
For(U,0,3)
If(4*T+U) .If there is an item here
Pt_On(U*8+X,T*8+Y,4*T+U*13+L1) .you can use any sprite drawing routine you want
End
End
End
.Guess what X and Y are
.Correct: They are the X and Y coordinates on which the inventory should be drawn.

I hope this helped. Sorry, but none of the code is tested.
Title: Re: Inventory System
Post by: collechess on November 21, 2011, 10:14:40 am
Thanks. I don't really understand the drawing part though.  What is the Ux13 for?
Title: Re: Inventory System
Post by: ben_g on November 21, 2011, 02:51:43 pm
Thanks. I don't really understand the drawing part though.  What is the Ux13 for?
the U*13 part (which is actually (4*T+U)*13) is because every item is 13 bytes long.

BTW: the pointer to the name is ID*13+9 and to the description is ID*13+11

If there is still anything that I didn't explain well, just ask
Title: Re: Inventory System
Post by: collechess on November 21, 2011, 03:38:39 pm
I seem to be something wrong, as it won't compile.  An invalid token error.  Here is the source.
Code: [Select]
.INVNTRY
"SWORD"->Str00
"10 DAMAGE"->Str10
"POTION"->Str01
"+20 HP"->Str11
"PEAR"->Str02
"+20 MP"->Str12

[03050A94E870F0D8]->GDB0
[01]
[Str00]
[Str10]
[181824244242423C]
[00]
[Str01]
[Str11]
[0C1028284482827C]
[00]
[Str02]
[Str12]

For(T,0,23)
1ü{T+L‚}
End
Repeat getKey(15)
sub(DRAW
DispGra
End
Lbl ADD
0->r6
While (r6<24)
!If {r6+L2}
r1->{r6+L2}
24->T
End
T++
End
Return
Lbl DRAW
For(T,0,5)
For(U,0,3)
If ({4*T+U+L2})
Pt-On(U*8,T*8,(4*T+U+{L2})*13-13+GDB0
End
End
End
Return

Edit:Nevermind, I have it working now.
Title: Re: Inventory System
Post by: collechess on November 22, 2011, 09:58:48 am
I understand how to do the picture inventory now, but it doesn't really suit my game.   How would you create an inventory that just showed the names of items and scrolled, and when an item is selected, it shows more info?  Also, how can you create a maximum unique item count?
Title: Re: Inventory System
Post by: ben_g on November 24, 2011, 03:43:50 pm
I understand how to do the picture inventory now, but it doesn't really suit my game.   How would you create an inventory that just showed the names of items and scrolled, and when an item is selected, it shows more info?  Also, how can you create a maximum unique item count?
that's even easyer: you just create a list again which is the same sixe as the (maximum) number of items. Then you make everything 0 again at the start of the game. To get the name of an item, just do ID*13+9 and to get the description is ID*13+11 (those are the pointers). You can easily check before drawing the string if you reached the end.
Title: Re: Inventory System
Post by: Jonius7 on November 24, 2011, 06:37:58 pm
I could probably do this in TI-basic and Casio Basic. And I'd better get used to Axe cuz Im still kinda a beginner.