Author Topic: Making and reading a list easily  (Read 811 times)

How would I go about making a list of values that could be continuously added on to and read?

For example, I could type

/Set Treynolds416 dog
/Set (Other player) cat
/Set (Other player) mouse
/Set (Other player) human

then type

/WhatIs Treynolds416

And have the script return "dog"



Is there an easier way to do this without using FileIO?

script objects.

Code: [Select]
new scriptObject(whatIs)
{
  value["cow"] = "moo";
};
function serverCmdWhatIs(%client , %1, %2, %3, %4, %5)
{
  if(whatIs.value[trim(%1 SPC %2 SPC %3 SPC %4 SPC %5)] !$= "")
    messageClient(%client,'',"\c6"@trim(%1 SPC %2 SPC %3 SPC %4 SPC %5)@"\c3 is \c6"@whatIs.value[trim(%1 SPC %2 SPC %3 SPC %4 SPC %5)]);
}

function serverCmdSet(%client, %pl, %set)
{
  %pl = strReplace(%pl, "_", " ");
  if(%set !$= "")
    whatIs.value[%pl] = %set;
    messageClient(%client,'',"\c3You have set \c6"@%pl@"\c3 to \c6"@%set@"\c3.");
}

untested, may not work

if you did /whatIs cow it would message you

cow is moo

edit:

if you want this list to save, you would have to use FileI/O.
« Last Edit: April 10, 2011, 12:55:59 PM by otto-san »

if you want this list to save, you would have to use FileI/O.
You are able to save objects and their values:
%obj.save(%filePath);


But if the any of the value names have spaces or special characters it would give syntax errors when re executing. So if that is the case you would need to use file objects and write the values to a text file manually.

You are able to save objects and their values:
%obj.save(%filePath);


But if the any of the value names have spaces or special characters it would give syntax errors when re executing. So if that is the case you would need to use file objects and write the values to a text file manually.
oh, right. I forgot about that.

As you can probably infer, I haven't done any script objects to date. I will try to understand otto's script line by line:

Code: [Select]
new scriptObject(whatIs)   //Creates new script object under name of "whatIs"
{
==============  value["cow"] = "moo";   //*For value "cow", set as "moo"*
};
function serverCmdWhatIs(%client , %1, %2, %3, %4, %5)   //Sets up function and command with the client and 5 text arguments
{
  if(whatIs.value[trim(%1 SPC %2 SPC %3 SPC %4 SPC %5)] !$= "")   //If the stuff you just typed in isn't blank...
    messageClient(%client,'',"\c6"@trim(%1 SPC %2 SPC %3 SPC %4 SPC %5)@"\c3 is \c6"@whatIs.value[trim(%1 SPC %2 SPC %3 SPC %4 SPC %5)]);   //Send "(person) is (value)" to client
}

function serverCmdSet(%client, %pl, %set)   //Sets up function with arguments for client, player, and value
{
  %pl = strReplace(%pl, "_", " ");   //Replace all underscores with regular spaces for player (Not sure why this is necessary but ok)
  if(%set !$= "")   //If you actually put something for the %set argument...
==============    whatIs.value[%pl] = %set;   //*Adds another value to the SO list*
    messageClient(%client,'',"\c3You have set \c6"@%pl@"\c3 to \c6"@%set@"\c3.");   //More messaging feedback
}
All of the lines I don't fully understand have a bunch of equal signs next to them

value["cow"] = "moo"; You're defining a field in the scriptObject "whatIs" called value[cow]. "whatIs.value[cow]" equals "moo".

whatIs.value[%pl] = %set; You're changing "whatIs.value[%pl]" to the value of %set. Let's say that %pl is "rat". That would mean that "whatIs.value[%pl]" is really "whatIs.value[rat]"

As you can probably infer, I haven't done any script objects to date. I will try to understand otto's script line by line:

Code: [Select]
new scriptObject(whatIs)   //Creates new script object under name of "whatIs"
{
==============  value["cow"] = "moo";   //*For value "cow", set as "moo"*
};
function serverCmdWhatIs(%client , %1, %2, %3, %4, %5)   //Sets up function and command with the client and 5 text arguments
{
  if(whatIs.value[trim(%1 SPC %2 SPC %3 SPC %4 SPC %5)] !$= "")   //If the stuff you just typed in isn't blank...
    messageClient(%client,'',"\c6"@trim(%1 SPC %2 SPC %3 SPC %4 SPC %5)@"\c3 is \c6"@whatIs.value[trim(%1 SPC %2 SPC %3 SPC %4 SPC %5)]);   //Send "(person) is (value)" to client
}

function serverCmdSet(%client, %pl, %set)   //Sets up function with arguments for client, player, and value
{
  %pl = strReplace(%pl, "_", " ");   //Replace all underscores with regular spaces for player (Not sure why this is necessary but ok)
  if(%set !$= "")   //If you actually put something for the %set argument...
==============    whatIs.value[%pl] = %set;   //*Adds another value to the SO list*
    messageClient(%client,'',"\c3You have set \c6"@%pl@"\c3 to \c6"@%set@"\c3.");   //More messaging feedback
}
All of the lines I don't fully understand have a bunch of equal signs next to them

i'll explain then
-value["cow"] = "moo"; --When the script object is made, a variable by default exists inside of it. In the SO, variables are stored in 'arrays' but they aren't really arrays at all.

VARIABLE[NUMBER/STRING] = VALUE;

This is used to be able to access the variables by another variable like so:

whatIs.value[%pl] = %set;

In this, whatever the user defined (%pl) is in "NUMBER/STRING" and "VALUE" is the user defined value (%set).

Basically, it works this way:

the script object stores data in "value" and whatever is in the brackets works as a label for the value of it. if "cow" is in the brackets, and "moo" is the value, then if you try to get the value of "value["cow"]" it will tell you it's "moo". if you try to get the value of "value" it will do nothing. if you try to get the value of "value["moo"]" it will do nothing because nothing with the label "moo" has been defined.

This is as best as I can explain it.

Thanks, that makes a lot of sense.

I wont lock the topic, but I've got all of the information I need