Jump to content

Something New-to-Me: Using "Wrapper" calls for llJsonSetValue() - for Watches, Events, etc.!


Love Zhaoying
 Share

You are about to reply to a thread that has been inactive for 622 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

 

Part of one LSL project I am working on, is a "generic text-driven gaming engine".

All of the data is being stored in Notecards as JSON data, loaded / interpreted also as JSON data.  I'm not using any Lists at all, except where needed as input to the llJson() calls for the "specifiers" parameters.

Anyway - I had a "big idea" that is working great - and that is to implement logic using "Watches" (checking for variable values / changes), "Events" (implementing specific use-case scenarios), and "Blocking" (to prevent logic from continuing past certain conditions, detected via "Watches", "Events", etc.).

The reason this turned out to be MUCH easier than I expected, as that I implemented a "wrapper" around the "llJsonSetValue()" function.  

For this post, I will refer to that "wrapper" as "lzJsonSetValue()".  It takes the same parameters as "llJsonSetValue()", but is "data-aware".

I will post examples of the "Watches", "Events", and "Blocks" later in the thread  (or a separate thread).

Currently, I am coding a method where I know that the llJsonSetValue() "wrapper" (lzJsonSetWrapper) needs to understand JSON Arrays: where logic is needed to check "inserts" and "deletes".  For these cases, the last parameter in the "specifiers" list is most often JSON_APPEND or JSON_DELETE. 

This example pseudocode shows how I am extending the existing the append / delete functionality for JSON_ARRAY objects.

In this example, I am checking if (depending on a flag in the source Json) I need to track "Counts" of items, just allow a "Single" item, etc.  

So, instead of just "Appending" to the JSON_ARRAY for JSON_APPEND, and "Deleting" for JSON_DELETE, I am tracking a "Count" of items with the same Key / Name.

string sJson; // The JSON I am modifying

string sNewJson; // The JSON I am inserting or deleting

Insert:  sJson = lzJsonSetValue(sJson, [specifer1, specifier2, ..JSON_APPEND], sNewJson);

Delete: sJson = lzJsonSetValue(sJson, [specifer1, specifier2, ..], JSON_DELETE);

In my code that needs this, I only calling my lzJsonSetValue() "wrapper" where needed.

Inside lzJsonSetValue(), I have logic:

if (llList2Integer(specifiers, -1)==JSON_APPEND) {   // JSON_APPEND constant is an integer

// Check sNewJson for a flag value meaning: "Single", "Count", or "no flag"

   string sFlag = llJsonGetValue(sNewJson, ["theflag"]);

   if (sFlag==JSON_INVALID) {  // No flag

// If no flag, just add to the array

   }

   else if (sFlag=="Single") { // Only allow a single entry

// if sFlag is "Single" Make sure I don't already have the value in the JSON_ARRAY.

// Find the entry matching the Key in the JSON_ARRAY and replace it

   }

   else if (sFlag=="Count") { // Allow 1 entry, but keep "Count"

// Find any matching entry, get ["Count"] value and increase it.

   }

}

else if (sValue==JSON_DELETE) {   // JSO)

// TODO - Will code this later
   

   }

}

 

The use-case for this is "Inventory" in the text game:  Add another "torch" by increasing the "Count", etc. instead of creating another Inventory entry.

The "Watch" / "Event" / "Block" part comes in, by adding checks for scenarios where either:

- You can only have one of the "thing"

- We want to tell the user "you added the thing, now you have X things"

- You can only have a Maximum count of the "thing".

- Adding the "thing" triggers another behavior.

All of this same logic would be for actions "Add/Take/Wear" (JSON_APPEND)  vs. "Drop/Give/Remove" (JSON_DELETE).

I would only call the "wrapper" lzJsonSetValue() for JSON_APPEND and JSON_DELETE instances where it is appropriate.

A "helper function" will search the JSON array for existing entries matching the "Key", where the "Key" is just the "Name" entry in sNewJson:

string sKey = llJsonGetValue(sNewJson, ["Name"]);

As always, any feedback or questions are more than welcome.

Thanks,

Love

 

 

Edited by Love Zhaoying
  • Thanks 1
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 622 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...