NEWBIE - How to do SQL-like stuff in C++?
Ok, guys, I am a big ole'' newbie, so please don''t flame me for idiocy. I have some C++ books, and I am learning as I go, but the best way for me personally to learn is to just do it.
Here''s the deal. For my first game (heck, my first C++ program!), I am creating an Inform-style text adventure. I have a Room class, a Player class, and so on. There is a Room.ID property, and a Player.Location property, which would equal the room ID of the room where the player is. I have a nice little function to move the player (it just sets the Player.Location property). Here''s my problem:
Let''s say I move the player to room 1. How do I take the Player.Location, check to find the room object where Player.Location is equal to Room.ID, and get the Room.Desc (description) property for that room? What I really need to figure out conceptually is how C++ does an SQL-like lookup:
Select Room.Desc where Room.ID = Player.Location. <-- This is how I''d do it in SQL.
Does that make sense? The only way I can think of is to create an array for the rooms, and then use the Room.ID/Player.Location as the index to the array, but that seems like too much overhead. Is there a simpler/better way?
Oh, and if the answer has anything to do with linked lists, I''ll go do some more reading. I have no idea what they are, but everybody here seems to love ''em. :-)
Thanks in advance,
- gollumgollum
You can get Room.Desc, but where do you want to put it? Or are you returning it? I''m not sure what you are getting at, but here''s some code that might help:
if(Room.ID == Player.Location)
a = Room.Desc;
-Where a is what you want to put Room.Desc.
Or, if you want to return Room.Desc:
if(Room.ID == Player.Location)
return Room.Desc;
-Note that == is for comparison, and = is for assignment.
You may have to research using that array some more; use a linked list only if the number of rooms changes at runtime.
I''ve never used SQL, so don''t flame me if I''m not following you right .
"Science is a tool. If the tool works, we use it. If it''s true, that''s great, but if it isn''t, it doesn''t matter" -- ("Desert Fox" from GameDev.net)''s physics teacher
if(Room.ID == Player.Location)
a = Room.Desc;
-Where a is what you want to put Room.Desc.
Or, if you want to return Room.Desc:
if(Room.ID == Player.Location)
return Room.Desc;
-Note that == is for comparison, and = is for assignment.
You may have to research using that array some more; use a linked list only if the number of rooms changes at runtime.
I''ve never used SQL, so don''t flame me if I''m not following you right .
"Science is a tool. If the tool works, we use it. If it''s true, that''s great, but if it isn''t, it doesn''t matter" -- ("Desert Fox" from GameDev.net)''s physics teacher
"If a man does not keep pace with his companions, perhaps it is because he hears a different drummer. Let him step to the music he hears, however measured or far away"--Henry David Thoreau
Oh, the way I want to use it is fairly simple.
Let''s say that the player types ''l" or "look". In my game, we would first reference the location property of the player object:
Player.Location
That property would be a number equal to a room object''s id;
Room.ID
I want to use that to look up the correct room object, get the description, and output it to the display.
I would use the same idea for text descriptions of objects the player can carry.
- gollumgollum
Let''s say that the player types ''l" or "look". In my game, we would first reference the location property of the player object:
Player.Location
That property would be a number equal to a room object''s id;
Room.ID
I want to use that to look up the correct room object, get the description, and output it to the display.
I would use the same idea for text descriptions of objects the player can carry.
- gollumgollum
I don''t see the overhead when using an array for the rooms. Just make sure that you have the Room.id as index. Then you could simply have code that looks like this.
No overhead at all.
|
No overhead at all.
You could create tables and use SQL to access them.
Otherwise I would say what you are missing is adding an index or pointer to the structure when you load it. As an example if you have a list of exits for each room that list the room being exited from and where that exit leads as a pair of text room IDs then when you load it basically replace the text room id with the index into the array of rooms. The Player.Location is then an index into that array and the description is Rooms[Player.Location].Desc. You can also use pointers in which case it would be Player.Location->Desc. Rather than searching throughout your program search once when you load.
Otherwise I would say what you are missing is adding an index or pointer to the structure when you load it. As an example if you have a list of exits for each room that list the room being exited from and where that exit leads as a pair of text room IDs then when you load it basically replace the text room id with the index into the array of rooms. The Player.Location is then an index into that array and the description is Rooms[Player.Location].Desc. You can also use pointers in which case it would be Player.Location->Desc. Rather than searching throughout your program search once when you load.
Keys to success: Ability, ambition and opportunity.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement