Entity Tutorial 3: Random Script: Difference between revisions

From SkyCorp Global
Jump to navigation Jump to search
No edit summary
No edit summary
Line 6: Line 6:
The code is extended as follows -- take a read through the comments.
The code is extended as follows -- take a read through the comments.


CODE
<syntaxhighlight lang="lua">
function description()
 
  -- Create a blank string to store the color in
  local colorString = "";
 
  -- Get a random integer in the range of 1-3
  local randomNumber = math.random(1,3);
 
  -- Set color string to a different color based on the random number
  if randomNumber == 1 then
      colorString = "red"
  elseif randomNumber == 2 then
      colorString = "green"
  elseif randomNumber == 3 then
      colorString = "blue"
  else
      colorString = "unknown" -- As the number should be 1, 2, or 3, should not reach here
  end
 
  -- Return a string with (possibly) a different color each time
  return "The gem shines brightly, reflecting " .. colorString .. " into your eyes.";
 
end
 
-- The below lines are commented out.  Uncommenting them can be helpful for
-- testing the program in lua command line.  Comment or delete them before
-- compiling the program for in-game usage, though!
 
--for i=1,10 do
-- io.write(description() .. "\n");
--end
</syntaxhighlight>
 
Try running it through command line lua to check for syntax & logic errors.
 
After luac'ing and base64'ing the program, you'll have some base64 luac code for the new json definition.


== JSON Definition ==
== JSON Definition ==
After luac'ing and base64'ing the program, we get some base64 luac code which the JSON can be updated with:
The new lua code can be inserted:


JSON
<syntaxhighlight lang="json">
{
"type": "ENTITY",
"name":
{
"literalString" : "Shiniest Gem"
},
"description":
{
"programString": true
},
"luac": "G0x1YVEAAQQEBAgAEQAAAEBzaGluaWVzdEdlbS5sdWEAAAAAAAAAAAAAAAICAwAAACQAAAAHAAAAHgCAAAEAAAAEDAAAAGRlc2NyaXB0aW9uAAEAAAAAAAAAAQAAABcAAAAAAAAFGQAAAAEAAABFQAAARoDAAIHAAADBAAEAXICAARfAwAAWQACAAUABABYAAoAXgMEAFkAAgAHAAQAWAAGAFwDBABZAAIABAAIAFgAAgAFAAgCBgAIAwAAAAAHBAgCVAAEBngAAAR4AgAAMAAAABAEAAAAABAUAAABtYXRoAAQHAAAAcmFuZG9tAAMAAAAAAADwPwMAAAAAAAAIQAQEAAAAcmVkAAMAAAAAAAAAQAQGAAAAZ3JlZW4ABAUAAABibHVlAAQIAAAAdW5rbm93bgAEJQAAAFRoZSBnZW0gc2hpbmVzIGJyaWdodGx5LCByZWZsZWN0aW5nIAAEEQAAACBpbnRvIHlvdXIgZXllcy4AAAAAABkAAAAEAAAABwAAAAcAAAAHAAAABwAAAAcAAAAKAAAACgAAAAsAAAALAAAADAAAAAwAAAANAAAADQAAAA4AAAAOAAAADwAAAA8AAAARAAAAFQAAABUAAAAVAAAAFQAAABUAAAAXAAAAAgAAAAwAAABjb2xvclN0cmluZwABAAAAGAAAAA0AAAByYW5kb21OdW1iZXIABgAAABgAAAAAAAAAAwAAABcAAAABAAAAFwAAAAAAAAAAAAAA"
}
</syntaxhighlight>


== Running ==
== Running ==
Now each time the gem is looked at, a different color is returned (well, up to three colors anyway).
Now each time the gem is looked at, a different color is returned (well, up to three colors anyway).

Revision as of 05:17, 21 December 2017

Previously we created a gem that would display a description using LUA. Since we're using LUA to power the description, this allows us to do more than just return the same thing every time.

In this tutorial, we'll extend our test gem to reflect a different color of light each time the user looks at it.

LUA Code

The code is extended as follows -- take a read through the comments.

function description()

   -- Create a blank string to store the color in
   local colorString = "";

   -- Get a random integer in the range of 1-3
   local randomNumber = math.random(1,3);

   -- Set color string to a different color based on the random number
   if randomNumber == 1 then
      colorString = "red"
   elseif randomNumber == 2 then
      colorString = "green"
   elseif randomNumber == 3 then
      colorString = "blue"
   else
      colorString = "unknown" -- As the number should be 1, 2, or 3, should not reach here
   end

   -- Return a string with (possibly) a different color each time
   return "The gem shines brightly, reflecting " .. colorString .. " into your eyes.";

end

-- The below lines are commented out.  Uncommenting them can be helpful for
-- testing the program in lua command line.  Comment or delete them before
-- compiling the program for in-game usage, though!

--for i=1,10 do
-- io.write(description() .. "\n");
--end

Try running it through command line lua to check for syntax & logic errors.

After luac'ing and base64'ing the program, you'll have some base64 luac code for the new json definition.

JSON Definition

The new lua code can be inserted:

{
	"type": "ENTITY",
	"name":
	{
		"literalString" : "Shiniest Gem"
	},
	"description":
	{
		"programString": true
	},
	"luac": "G0x1YVEAAQQEBAgAEQAAAEBzaGluaWVzdEdlbS5sdWEAAAAAAAAAAAAAAAICAwAAACQAAAAHAAAAHgCAAAEAAAAEDAAAAGRlc2NyaXB0aW9uAAEAAAAAAAAAAQAAABcAAAAAAAAFGQAAAAEAAABFQAAARoDAAIHAAADBAAEAXICAARfAwAAWQACAAUABABYAAoAXgMEAFkAAgAHAAQAWAAGAFwDBABZAAIABAAIAFgAAgAFAAgCBgAIAwAAAAAHBAgCVAAEBngAAAR4AgAAMAAAABAEAAAAABAUAAABtYXRoAAQHAAAAcmFuZG9tAAMAAAAAAADwPwMAAAAAAAAIQAQEAAAAcmVkAAMAAAAAAAAAQAQGAAAAZ3JlZW4ABAUAAABibHVlAAQIAAAAdW5rbm93bgAEJQAAAFRoZSBnZW0gc2hpbmVzIGJyaWdodGx5LCByZWZsZWN0aW5nIAAEEQAAACBpbnRvIHlvdXIgZXllcy4AAAAAABkAAAAEAAAABwAAAAcAAAAHAAAABwAAAAcAAAAKAAAACgAAAAsAAAALAAAADAAAAAwAAAANAAAADQAAAA4AAAAOAAAADwAAAA8AAAARAAAAFQAAABUAAAAVAAAAFQAAABUAAAAXAAAAAgAAAAwAAABjb2xvclN0cmluZwABAAAAGAAAAA0AAAByYW5kb21OdW1iZXIABgAAABgAAAAAAAAAAwAAABcAAAABAAAAFwAAAAAAAAAAAAAA"
}

Running

Now each time the gem is looked at, a different color is returned (well, up to three colors anyway).