Results 1 to 7 of 7

Thread: If Item Is Taken

  1. #1
    Hyperactive Member
    Join Date
    Dec 11
    Posts
    346

    If Item Is Taken

    So im working on a text based rpg engine because I wanted to see how it would all work and learn. So I have items you can take, but ive come to the problem these items are stored as txt files and so are the areas. So the area might say it has a Gold Coin in it. And I can take that. But I can just keep taking it how ever many times I want. I could erase Gold Coin from the area but that would directly edit the text file. And if That happend youd have to go in and reset it. Any ideas on how I could tell just for that game that its gone?

  2. #2
    Addicted Member
    Join Date
    Sep 04
    Posts
    129

    Re: If Item Is Taken

    Text File:
    Code:
    "You look around the cave and see <xITEMx>."
    Seudocode
    Code:
    textVariable = text from file
    
    If CoinHasBeenTaken = true and RopeHasBeenTaken = true then
    replace <xITEMx> in textVariable with "nothing".
    
    Else If CoinHasBeenTaken = true and RopeHasBeenTaken = false 
    replace <xITEMx> in textVariable with "a rope".
    
    Else If CoinHasBeenTaken = false and RopeHasBeenTaken = true
    replace <xITEMx> in textVariable with "a Gold Coin".
    
    Else 
    replace <xITEMx> in textVariable with "a Gold Coin and a rope".
    End If
    
    Display textVariable
    EDIT: When I first read your post I thought you were asking how to display the differences without changing the text files. I reread your post and it sounds like you may be trying to figure out how to remember the games current state as to what has been taken or not. There are several ways to do that. One is with variables as I used above. You could also use a multidimensional array of items with a name and a state flag. Another option is a long string of bits that represent the state of various game items. I have used all three methods for various things.
    Last edited by Maverickz; Aug 20th, 2012 at 07:51 PM.

  3. #3
    Hyperactive Member
    Join Date
    Dec 11
    Posts
    346

    Re: If Item Is Taken

    Yea I was trying to figure out to remember if it was taken. I could use a true false but would that have to be done in the txt file? If it was I couldint use it because that would require changing the state back within the text file.

  4. #4
    Addicted Member
    Join Date
    Sep 04
    Posts
    129

    Re: If Item Is Taken

    Quote Originally Posted by sherlockturtle View Post
    Yea I was trying to figure out to remember if it was taken. I could use a true false but would that have to be done in the txt file? If it was I couldint use it because that would require changing the state back within the text file.
    You can save the state in many ways. You could save it to separate text or XML file, save it to a DB, or if you used the bit method you could save it to the registry.

    Personally I would use bits and save it to the registry, using a field name that is not obvious. This would make it less obvious how to cheat and take up less memory than initializing a separate boolean variable for every item.

    Why do I suggest the bit method? Imagine that you have 10 items to track if they had been picked up or not. You could indeed create 10 variables to track true or false if they had been picked up but what if you had 2000 items to track? You would then have to create 2000 variables. With the bit method you really only need 2 variables. One to hold all of the bits and one to hold the value of the current bit you need to know about. Each bit would represent one item and if the bit is a 0 it's false and 1 is true.

    So if you had 10 items and nothing had been picked up the bit value would be 0000000000. If you pick up the gold coin set one bit to a 1 and then when you pick up a rope the next bit changes to a one, if you drop the coin change it back to a 0, etc. If you wanted you could technically handle this as a string while in memory if you know how to work with strings more than bits, but then you could (optionally) convert it to Hexidecimal to store in the registry or DB. This is a neat way to track a bunch of boolean values but it can get hard to track which bit is which if you have a lot.

    The Seudocode (using as a string) for this would basically be:
    Code:
    dim ItemBits as String
    dim curentItem as Integer
    ItemBits = <get value from registry>
    currentItem = <get appropriate bit from ItemBits>
    If currentItem = 0 then
    <do code if the item is on the ground>
    Else
    <do code if the item is picked up>
    End If
    --------------------------------------------------------------------------------------------------

    Another good way if you want to use a DB, is have a separate record for each item and just set true/false in the DB for each item. Then you just need one variable to read the state of the current item you are interested in.
    Last edited by Maverickz; Aug 20th, 2012 at 10:01 PM.

  5. #5
    Hyperactive Member
    Join Date
    Dec 11
    Posts
    346

    Re: If Item Is Taken

    Ok sounds good. Two things though. First how does gold coin know its bit 0 or which 0 it is? And so for each item id just add a zero correct?

    Edit: So how would do the part that assigns ItemsBits and currentItem? Just some pointers would help.
    Last edited by sherlockturtle; Aug 21st, 2012 at 08:22 AM.

  6. #6
    Addicted Member
    Join Date
    Sep 04
    Posts
    129

    Re: If Item Is Taken

    Quote Originally Posted by sherlockturtle View Post
    Ok sounds good. Two things though. First how does gold coin know its bit 0 or which 0 it is? And so for each item id just add a zero correct?

    Edit: So how would do the part that assigns ItemsBits and currentItem? Just some pointers would help.
    You just have to know which bit to parse out for that particular item. If you know the Coin is the first bit and you just want to know about the coin, then read just the first bit. If the rope is the second and you want to know about that then just read the second bit.

    You can save it to the registry and read it back by importing Microsoft.Win32

    Imports Microsoft.Win32 'Needed to edit the registry.

    To Save (this saves the value of the ItemsBits variable to the Key "ItemsBits" at the location "HKEY_CURRENT_USER\Software\YourAppName")
    Call Registry.SetValue("HKEY_CURRENT_USER\Software\YourAppName", "ItemsBits", ItemsBits)

    To Read (This reads the value of the key "ItemsBits" at location "HKEY_CURRENT_USER\Software\YourAppName" and stores it in the ItemBits variable. If no value is available then it uses the default value "0000000000")
    ItemsBits = Registry.GetValue("HKEY_CURRENT_USER\Software\YourAppName", "ItemsBits", "0000000000")

  7. #7
    Hyperactive Member
    Join Date
    Dec 11
    Posts
    346

    Re: If Item Is Taken

    What youve given me I can do and thanks. But I had an idea. So currently all the rooms and items n stuff all stored in a folder. So what if when the player clicks new game it duplicates the original folder and they play off of the new folder? So, if I did that it might be good to save it as its own file type. So instead of looking like a duplicate folder it could be a file. So the extension might be .something If I did that could I open it in some way?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •