|
complex Apprentice
Joined: 12 Apr 2004 Posts: 134 Location: USA
|
Posted: Sun Sep 12, 2010 1:28 am
Database Retrivel Question |
So I have a database with many many members. In this data base each member has a name and interaction command, I set it up this way rather then have hundreds of aliases, and I have other information stored on each member. What I am having trouble with is matching and retrieval. Lets say one of the members is
Name: "A banker is here" and his interaction command is
IntCmd: "Deposit Gold"
What I am trying to set up is a trigger so that when I enter a room, the database will be queried and the value that would be returned to the client is the interaction command. Currently when I run my trigger and enter the room I can #QUERY the database, but get the whole record as a return. How can I either isolate the desired portion of returned string, and pass it to the client, or get the specific data by it self? |
|
_________________ <Clever text here> |
|
|
|
MattLofton GURU
Joined: 23 Dec 2000 Posts: 4834 Location: USA
|
Posted: Sun Sep 12, 2010 1:55 am |
Use %query(). This will return the record number, and from there you can use the @record.field syntax to refer to just that field.
|
|
_________________ EDIT: I didn't like my old signature |
|
|
|
complex Apprentice
Joined: 12 Apr 2004 Posts: 134 Location: USA
|
Posted: Sun Sep 12, 2010 5:37 am |
Perfect, thank you.
|
|
_________________ <Clever text here> |
|
|
|
complex Apprentice
Joined: 12 Apr 2004 Posts: 134 Location: USA
|
Posted: Wed Sep 15, 2010 6:58 pm |
Is there anyway I could get a generic syntax example, I am still having trouble retrieving just field and having it posted to the mud.
A Banker is standing here.
should result in
deposit 1000 gold. (the second member of that record)
But instead I get
141 A Banker is standing here. deposit 1000 gold. town name, zone name, ect.... |
|
_________________ <Clever text here> |
|
|
|
MattLofton GURU
Joined: 23 Dec 2000 Posts: 4834 Location: USA
|
Posted: Wed Sep 15, 2010 10:23 pm |
@141##.field
## = the first two letters of the name of the database file (for example, if your file were areas.db then it'd be 141ar)
field = name of the column you want to refer to |
|
_________________ EDIT: I didn't like my old signature |
|
|
|
complex Apprentice
Joined: 12 Apr 2004 Posts: 134 Location: USA
|
Posted: Thu Sep 16, 2010 1:05 am |
Thank you Matt, but that does not quite do it. Ok let me get more specific, I want all of this to be triggered when I walk into a room. Before when I used zmud I had a massive list of npc's and their interactions. like so
Trigger Class: MatchNPC
pattern: *({@npcList})*
#var npc %lower({%1}){_nodef}{Interact};#-t MatchNPC
Trigger Class: Interact
#t+ MatchNPC
#if (@npcList) {interact @npc}
So when I walked into a room if an npc matched the one only my master npcList, the npc would be added to another temp list npc, then I had a list of alias that had for example
interact A Banker is standing here.
deposit 1000 gold.
Since for some reason or another my scripts did not want to transfer from zmud to cmud I thought I would updated the whole thing, and make it more compact and smooth. So I created a database with all of my NPC's in it, and their interaction commands, but I am having trouble getting my triggers to interact with the database.
I figured I could #query the database when I enter a room to see if any of the NPCs match the ones on my list, like the structure above
#if (#query databse for matching name)
@(recordNumber)np.interaction
also now that I am only using one trigger to do all of the work, what do you think would be the best way to toggle it on and off? |
|
_________________ <Clever text here> |
|
|
|
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: Thu Sep 16, 2010 12:49 pm |
Matt has already given you the tools you need. %query() will give you a list of record numbers that match your input. If it returns a single value, you can grab the value of the interaction field from that record, and execute it. I personnally would use %db() to grab the record rather than the syntax Matt suggested, because it avoids creating the awkward ###ar format. But that is a matter of choice. I assume the interactions are commands to be sent to the mud, rather than Cmud aliases.
I haven't played much with the internal Cmud database, so I may not get this quite right. There are numerous ways to grab the right record. It would look something like this:
Code: |
$keys = %query(&Name=%1)
#IF (%numitems($keys)=1) {
#DBGET $keys
$interaction = %db(%rec,interaction)
#SEND $interaction
} {
// I'm not sure what you want to do if there are multiple matches on the name, or no matches
}
|
If you need more help, you'll have to give us more details on the actual name and format of your table, and whether you have other tables as well.
As for toggling it on and off, you'll have to explain a bit more. Under what conditions do you want this turned on or off? Is it just this single trigger you want turned on and off, or a set of triggers and/or variables and other stuff? |
|
|
|
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: Thu Sep 16, 2010 12:53 pm |
Actually, I just realized, using #DBGET, it's even easier:
Code: |
$keys = %query(&Name=%1)
#IF (%numitems($keys)=1) {
#DBGET $keys
#SEND &interaction
} {
// I'm not sure what you want to do if there are multiple matches on the name, or no matches
}
|
This code will only work if the npc table is the current table. |
|
|
|
complex Apprentice
Joined: 12 Apr 2004 Posts: 134 Location: USA
|
Posted: Thu Sep 16, 2010 2:36 pm |
So I have gotten it to work, sort of. The above code results in the interaction command for the first member of the database to be sent for every match. I have tried to take a look at the script editor, but it does not let out everything that is going on.
** I don't know if this affects things but I noticed that $keys withing the if statement is not highlighted blue, does that mean it is no longer a variable within score?
Code: |
$keys = %query(&Name=%1);
#IF (%numitems($keys)>0)
{
#DBGET $keys;
#SEND &Interaction;
} |
|
|
_________________ <Clever text here> |
|
|
|
complex Apprentice
Joined: 12 Apr 2004 Posts: 134 Location: USA
|
Posted: Fri Sep 17, 2010 1:42 pm |
After playing with the code and doing a lot of testing yesterday which included moving around members of both the database and the match list I discovered that regardless of what match is made from the trigger only the first member of the database is ever sent to the mud. This is the abridged version of what I am working with.
variable
Name: npcList
A banker is here counting his money
Merlin is here
A shop keeper is here
Trigger:
Pattern *({@npcList})*
$keys = %query(&Name=%1);
#IF (%numitems($keys)>0)
{
#DBGET $keys;
#SEND &Interaction;
}
And the data base is
Name Interaction
A banker is here counting his money deposit 100 gold
Merlin is here list merlin
A shop keeper is here list keeper
If I move the shop keeper to the top of the database list, then when there is a match for any npc in the npcList, then the interaction for a shop keeper is sent, ie list keeper. If the bank is at the top, then all matches send deposit 100 gold. I have moved the names around in the npcList as well, but that has no affect. In the debug screen %1 is always a correct match. I also tried the code
$keys = %query(&Name=%1)
#IF (%numitems($keys)=1) {
#DBGET $keys
$interaction = %db(%rec,interaction)
#SEND $interaction
}
but the result is the same. |
|
_________________ <Clever text here> |
|
|
|
Moo Apprentice
Joined: 10 Apr 2009 Posts: 145
|
Posted: Fri Sep 17, 2010 8:59 pm |
Try without the *s in your pattern, and with "repeat within line" enabled.
|
|
|
|
complex Apprentice
Joined: 12 Apr 2004 Posts: 134 Location: USA
|
Posted: Sat Sep 18, 2010 8:52 pm |
The pattern still matches without the *'s in the there, and I am not sure what "repeat within line" did, but nothing changed as far as the results still being the first member of the database. If there is not a match, then nothing is sent, but regardless of what the match is if there is one, the first interaction command is sent. So even if there is a match for record 10 lets say, the interaction command for record 0 will be sent. I know this to be the case because I have moved the records around in the database, and its always record 0's interaction command that is sent.
|
|
_________________ <Clever text here> |
|
|
|
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: Mon Sep 20, 2010 6:43 pm |
Hm. The documentation says that #DBGET should set the %rec value, but it sounds as if it is not being set in your test. I haven't actually used #DBGET or similar commands, so I'm afraid I can't help much more. Hopefully someone else can?
|
|
|
|
complex Apprentice
Joined: 12 Apr 2004 Posts: 134 Location: USA
|
Posted: Tue Sep 21, 2010 3:56 am |
I just upgraded to the beta because I have a little bit of sql experience, so I am hoping to figure this out that way. Thank you for the time you put into it Rahab.
|
|
_________________ <Clever text here> |
|
|
|
|
|