Anyone interested in making a text adventure as a group? - Printable Version +- Qbasicnews.com (http://qbasicnews.com/newforum) +-- Forum: QbasicNews.Com (http://qbasicnews.com/newforum/forum-3.html) +--- Forum: Challenges (http://qbasicnews.com/newforum/forum-10.html) +--- Thread: Anyone interested in making a text adventure as a group? (/thread-8166.html) Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Anyone interested in making a text adventure as a group? - Liquid Snake - 10-29-2005 Quote:The only thing non QB that he did is use pointers. With CAllocate, he allocated memory on the free store. Then, using pointer arithmetic, gave them a value. It's difficult to understand at first. You should read rdc's Introduction to Pointers. Here is the link: http://www.freebasic.net/wiki/wikka.php?wakka=TutPointers I don't really understand what you can do with them....Points to data...well what kind of data? And where are you bringing it in from? Anyone interested in making a text adventure as a group? - Oz - 10-29-2005 I never really understood pointers until I started using them i'll try my best to explain them, though basically, everything in the computer memory has an address of some sort, yes? Well, a pointer is used to find that address. So, instead of moving a chunk of memory from place to place, we can just send the address, and any variable can use this address for example Code:`DIM example AS INTEGER` Ah-ha....we have a variable....not comes the fun part: Code:`example = 123456`now we have data in the variable.......instead of tranporting the 123456 value, we can transfere the address: Code:`PRINT example, @example` The '@' symbol retrieves the pointer address of the variable....so note that example and @example are two completely different numbers How is this helpful? you can use this for a few different things: copying a variable Code:```DIM example2 AS INTEGER example2 = @example ' Now, when example changes, example2 will change, because it uses the same memory``` this comes in handy, severly, when you have huge amounts of data in a UDT (user defined type) Code:```TYPE some_example a as string * 50 b as long c(100) as integer END TYPE DIM example3 as some_example dim cnt as integer example3.a = string\$(50, chr\$(int(rnd * 255))) example3.b = 329465084765447 for cnt = 1 to 100   example3.c(cnt) = int(rnd * 100) next '(...) SUB test(exptr as some_example ptr)   print exptr->a   print exptr->b   print exptr->c END SUB``` If you are dealing with pointers of a UDT, instead of using a period to use a sub-variable, use the -> sign (minus + greater-than) Pointers are mainly used because with addresses, you can do some 'fun' things when you really get a handle on them ( cptr().... :rotfl: ) hope that helps the explaination oz~ Anyone interested in making a text adventure as a group? - speedlemon - 10-30-2005 Thanks for that information Alex. I don't much about pointers yet either. I was wondering, what kind of fun stuff can you do with it that you mentioned? :???: Anyone interested in making a text adventure as a group? - Liquid Snake - 10-30-2005 What is it like....GOTO or GOSUB? The data it retrieves is from within the program right? Sorry, I still don't see the full picture. Anyone interested in making a text adventure as a group? - Oz - 10-31-2005 a pointer only points to a memory address. when two or more variables use the same memory, they are both the same. there isn't much more I can really do to explain pointers.... i don't know any good tuts on them...particularily for FB (but any language would do)..... best bet is to google "pointer tutorials, c", which will give yo probably lots of fun stuff oz~ Anyone interested in making a text adventure as a group? - Torahteen - 10-31-2005 Pointers also allow you to use memory on the Free Store. Think of your normal Dimmed variables as cubbyholes. When you DIM a variable, you put a label on a cubbyhole. So I have cubbyhole with a label on it now. You can do anything using that label. The Free Store is a bunch of cubbyholes already made by the computer. However, you can not label these. Instead, you can access it through its address. Take this as an example. Code:```Dim myptr As Integer Ptr        'A pointer to an Integer. myptr = Allocate(len(Integer))          'Allocates 4 cubbyholes on the                                                          'Free Store, then puts the                                                          'Address in myptr.``` Now you really can't do anything with myptr. It's just a Pointer to a cubbyhole on the Free Store. However, now you can put anything you want in that cubbyhole using your pointer. Things done directly on the free store tend to be faster than regular variables. The way to put the number 256 in the cubbyhole, is to use the DeReference operator (*). Like this: Code:`*myptr = 256` Notice the asterisk (*). This is very important. The asterisk means "The cubbyhole at". So this would be read as "The cubbyhole at myptr gets 256". You see? This is only one of the many uses of pointers. They don't seem very useful at first (and truthfully I haven't used them much either) but they are one of those tricks of the trade, that you will learn as you go along.[/code] Anyone interested in making a text adventure as a group? - Liquid Snake - 10-31-2005 OK, it's starting to make sense, but until I see them in action( to see and understand), I probably won't be able to use them. Anyone interested in making a text adventure as a group? - Torahteen - 11-01-2005 Another VERY useful thing they are used for is linked lists. Linked lists are pretty much dynamic arrays. But they allow more functionality, and easier to sort. Anyone interested in making a text adventure as a group? - stylin - 11-04-2005 Linked lists have a few advantages over an array, and also a few disadvantages. Arrays: Arrays are (in most implementations) contiguous areas of memory. That means all of the elements of the array from the first to the last are stored in memory sequentially, side-by-side, one after the other. This is how you can have O(1) random access to any element, iow, it doesn't matter how many elements are in the array, the time it takes to retrieve one of those elements will be the same (because we can simply do some pointer arithmetic). This contiguous storage brings up a problem when we need to insert or remove an element in the array, because all of the elements after it will have to be moved accordingly - that is, when inserting, the elements following the insertion point must be moved further in memory (to the right) to make space for the new element. Similarly, when deleting, the elements following the deletion point must be moved earlier in memory (to the left) to fill in the space caused by the deleted element. As you can imagine, the larger the array is, the more time it will take to perform these operations. An alternative is to use a linked list, which itself has an O(1) insertion/deletion complexity, but suffers from its own drawbacks as we'll see in a moment. Linked Lists: Linked Lists are containers similar to arrays, in the fact that they contain elements. Linked lists implement this storage differently than arrays, however. A linked list is composed of one or more nodes. These nodes are objects which contain an element, and a pointer to another node. In a singly-linked list, these nodes point to the next node in the list, thus, one can only iterate forward in such a list. In a doubly-linked list, each node points to the next and previous node, making forward and reverse iteration possible. You may be able to guess how we can have constant insertion/deletion into a linked list - and the answer is with pointers. Since each node (or element) merely points another node, these nodes don't have to be contiguous, ie. they can be stored in memory wherever the compiler sees fit to put them. This means that it's not necessary to move elements when inserting or deleting. All we do is change the pointer! Although linked lists can be efficient if you need to add and remove alot of elements from the middle of a container, they are incredibly inefficient when it comes to random-access. Since elements are stored at arbitrary locations in memory, the only way to access an element via an index is to start from the beginning of the list and make your way forward (via the nodes pointers). As you can see, this is the same problem with insertion/deletion for arrays, where on the average, half the elements must be dealt with. Summary: Arrays are good when you need efficient random access (like a screen buffer) and when you only need to insert/remove elements from the ends of the array. Linked lists are very efficient in insertion/removal from the middle of the list (like a task list for a kernel), but horribly slow with random access. Conclusion For basic object storage, prefer arrays, and use linked lists when you specifically need to take advantage of its features, and are not concerned with its downfalls. For any programmer not using C++, making your own linked list can be fun, and it is a good way to get introduced to working with pointers (C++ has a built-in linked list, as well as numerous other container types). Anyone interested in making a text adventure as a group? - Liquid Snake - 11-04-2005 OK, lets use them then. Should we start making rooms and stuff? Or do you think we should start with pointers?