Linked List style 'container' datablock?
by David Schwanke · in Torque Game Engine · 06/12/2004 (8:18 pm) · 8 replies
Howdy. Doing an object based inventory system and I wanted to setup a linked list inside of a 'container' datablock. (As if it were a class in C++)
Pseudocode:
Simple enough?
Cant find a single reference to referencing anywhere in the demo scripts or in the docs or forums.
I thought maybe I could do it just by assigning an object to FirstItem and NextItem but I cant seem to figure out how to 'define' this datablock and have it compile without already having an object to put there. Seems like every single datablock I have read has an assignment for each variable in its definition and I couldnt see any where they were refering to objects and not just other datablocks;
(Just a simple ContainerItem FirstItem; wouldnt compile.)
Any suggestions from you more learned scripters out there? Be kind of depressing if there isnt even a hack for linked lists in the scripting language. Not sure how else I could do a container class of objects in script and not be forced to go in to the source. :(
Thanks in advance.
Pseudocode:
datablock ItemData(ContainerItem) {
ContainerItem *FirstItem;
ContainerItem *NextItem;
}Simple enough?
Cant find a single reference to referencing anywhere in the demo scripts or in the docs or forums.
I thought maybe I could do it just by assigning an object to FirstItem and NextItem but I cant seem to figure out how to 'define' this datablock and have it compile without already having an object to put there. Seems like every single datablock I have read has an assignment for each variable in its definition and I couldnt see any where they were refering to objects and not just other datablocks;
(Just a simple ContainerItem FirstItem; wouldnt compile.)
Any suggestions from you more learned scripters out there? Be kind of depressing if there isnt even a hack for linked lists in the scripting language. Not sure how else I could do a container class of objects in script and not be forced to go in to the source. :(
Thanks in advance.
#2
I'm doing an object based inventory so then...
If I wanted to create this linked list with Item's, I could just write the function as Item::addItemToList as well as a set of support functions to flesh out the list usage.
Then all I would have to do is have a master Item in the player class as the base item (for the main linked list) and viola (sp?:) inventory. :)
Am I correct in understanding that since you dont declare variables like you do in C/C++ that all I have to do is make sure that the player creation code creates the master Item and that the item manipulation code takes care of adding the items to the inventory.
No worrying about having the list setup in advance?
I like heavily structured code systems, keeps my brain in check. :)
I think I finally have enough info to actually start coding. Thanks!
06/13/2004 (10:59 am)
I think I like your addItemToList scenario. Thank you!I'm doing an object based inventory so then...
If I wanted to create this linked list with Item's, I could just write the function as Item::addItemToList as well as a set of support functions to flesh out the list usage.
Then all I would have to do is have a master Item in the player class as the base item (for the main linked list) and viola (sp?:) inventory. :)
Am I correct in understanding that since you dont declare variables like you do in C/C++ that all I have to do is make sure that the player creation code creates the master Item and that the item manipulation code takes care of adding the items to the inventory.
No worrying about having the list setup in advance?
I like heavily structured code systems, keeps my brain in check. :)
I think I finally have enough info to actually start coding. Thanks!
#3
Plus they have functions to get the items out already.
06/13/2004 (11:29 am)
Why not create a simset and add items to that. Thats what theyre there for!Plus they have functions to get the items out already.
#4
Just when I think I can get down to actually coding.... :D
Thanks though, if you have any links ideas or suggestions I'm all about milking the people that know what they are doing for all their willing to donate. :D
** EDIT **
Wow, found some info on the first try. :D
Seems to be a little description of their usage in the SDK docs. :D
06/13/2004 (12:02 pm)
Err, cuz I'm not used to programming a code system that isnt heavily and uniformly documented? :D Guess I'll start researching simsets.Just when I think I can get down to actually coding.... :D
Thanks though, if you have any links ideas or suggestions I'm all about milking the people that know what they are doing for all their willing to donate. :D
** EDIT **
Wow, found some info on the first try. :D
Seems to be a little description of their usage in the SDK docs. :D
#5
I see SimSets are a child of SimObjects and that the Item class is a pseudo-distant child of SimObjects.
I didnt really get this far in C++ so I am not sure how to say if its possible. Is there a way to reclass the Item class to make it have the functionality of a SimSet as wel las its own functionality?
I want to be able to create Item's that are containers. So for example I want to be able to create 'ammo' which is a package of ammo and then I want to create an 'ammo box' which would be a collection of 'ammo'.
Sounds like if I could extend the functionality of SimSets into the Item class I could create a ItemContainer class.
I vaguely remember something like this being possible in C++ but what I dont remember is how?
Specifically I see that SimSet's are a sub class of SimObjects, so I guess I am looking to try to make a ItemSets that is a subclass of Item but re-use the code from SimSets.
Possible? Think I am going to dig around in my C++ books.
*** EDIT ***
Dug around enough to find Multiple Inheritance, though an experienced programmer friend of mine said: "Ive learned enough about multiple inheritance to know not to use it." :D Instead create a new class that has a variable/paramter 1 each of the two classes i want to combine and then extend the functions I want to work on those two items.
Think I might do that. Gotta first find out if I can even reuse the Item class or if its too 'simple' for a full mmorpg object system.
Thanks for you help. (And I'm being verbose incase this helps someone else. :D)
06/13/2004 (12:17 pm)
Alright, nifty C++ tricks time. :DI see SimSets are a child of SimObjects and that the Item class is a pseudo-distant child of SimObjects.
I didnt really get this far in C++ so I am not sure how to say if its possible. Is there a way to reclass the Item class to make it have the functionality of a SimSet as wel las its own functionality?
I want to be able to create Item's that are containers. So for example I want to be able to create 'ammo' which is a package of ammo and then I want to create an 'ammo box' which would be a collection of 'ammo'.
Sounds like if I could extend the functionality of SimSets into the Item class I could create a ItemContainer class.
I vaguely remember something like this being possible in C++ but what I dont remember is how?
Specifically I see that SimSet's are a sub class of SimObjects, so I guess I am looking to try to make a ItemSets that is a subclass of Item but re-use the code from SimSets.
Possible? Think I am going to dig around in my C++ books.
*** EDIT ***
Dug around enough to find Multiple Inheritance, though an experienced programmer friend of mine said: "Ive learned enough about multiple inheritance to know not to use it." :D Instead create a new class that has a variable/paramter 1 each of the two classes i want to combine and then extend the functions I want to work on those two items.
Think I might do that. Gotta first find out if I can even reuse the Item class or if its too 'simple' for a full mmorpg object system.
Thanks for you help. (And I'm being verbose incase this helps someone else. :D)
#6
06/13/2004 (6:39 pm)
Why don't you just put a SimSet inside the Item class, instead of trying to combine them? This is known as composition and it's a very useful technique for working with complex systems like Torque.
#7
06/13/2004 (7:26 pm)
Hehe i love doing that, reading through post " ah he could.." *scolls down one* "oh, Bens just suggested it".
#8
Only thing is we want to be able to have stackable items and I havnt found a neat trick yet for doing that with a SimSet. Maybe I just dont know them well enough yet, Id like to be able to get the reference for an object then get the referencs for all the other objects of the same class. Then get the next ungotten object. Etc..
That way we can list them in stacks.
I could do that easy with a linked list by makeing sure they get inserted next to other objects of the same class.
Still have to research SimSet's though, right now just working on the single extension of Item. (This is my first home brewed class definition in 7 years. :D)
06/13/2004 (8:35 pm)
Well thats basically what I was going to do. I have to extend Item anyway to handle all the extra code that the normal item doesnt care about so I might as well toss in a SimSet to boot. Only thing is we want to be able to have stackable items and I havnt found a neat trick yet for doing that with a SimSet. Maybe I just dont know them well enough yet, Id like to be able to get the reference for an object then get the referencs for all the other objects of the same class. Then get the next ungotten object. Etc..
That way we can list them in stacks.
I could do that easy with a linked list by makeing sure they get inserted next to other objects of the same class.
Still have to research SimSet's though, right now just working on the single extension of Item. (This is my first home brewed class definition in 7 years. :D)
Torque Owner Dylan Sale
Datablocks are not really the place to put a dynamic structure such as a linked list. You should never change the values of a datablock during a game as then you will change the values of all objects using that datablock (if it even works, I havnt tried it).
Datablocks are just that. Blocks of data that are used by different objects. ie, Players are given a datablock, the datablock then sets the attributes of the player object. Each instance of player can use the same datablock to set its attributes. Its a one to many relationship (one datablock for many players). I hope thats clear, and correct :/...
I would suggest a ScriptObject, they are generic objects that can be used for anything.
As a quick example...
function insertFirst(%nextObject, %item){ %first = new ScriptObject(){ next = %nextObject; //The link to the list element = %item; //The actual item we're storing }; return %first; }Just inserts a new ScriptObject at the start of the list.EDIT: Or you could even add a "next" field to the Item object. The great thing about TorqueScript is you can just add a variable at anytime.
Say you already have an item and a list...
function addItemToList(%item,%first){ %item.next = %first; //Make the item the first one in the list %first.prev = %item; //This one is doubly linked. return %item; //Returns the new first item in the list }