Entity Tutorial 3: Random Script: Difference between revisions

From SkyCorp Global
Jump to navigation Jump to search
(Created page with ".")
 
(new lua steps)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
.
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.
 
<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
</syntaxhighlight>
 
Try running it through command line lua to check for syntax & logic errors.
 
After escaping the program, you'll have some lua code to paste into the new json definition.
 
== JSON Definition ==
The new lua code can be inserted:
 
<syntaxhighlight lang="json">
{
"type": "ENTITY",
"name":
{
"literalString" : "Shiniest Gem"
},
"description":
{
"programString": true
},
"lua": "function description()\r\n\r\n  -- Create a blank string to store the color in\r\n  local colorString = \"\";\r\n\r\n  -- Get a random integer in the range of 1-3\r\n  local randomNumber = math.random(1,3);\r\n\r\n  -- Set color string to a different color based on the random number\r\n  if randomNumber == 1 then\r\n      colorString = \"red\"\r\n  elseif randomNumber == 2 then\r\n      colorString = \"green\"\r\n  elseif randomNumber == 3 then\r\n      colorString = \"blue\"\r\n  else\r\n      colorString = \"unknown\" -- As the number should be 1, 2, or 3, should not reach here\r\n  end\r\n\r\n  -- Return a string with (possibly) a different color each time\r\n  return \"The gem shines brightly, reflecting \" .. colorString .. \" into your eyes.\";\r\n\r\nend\r\n\r\n-- The below lines are commented out.  Uncommenting them can be helpful for\r\n-- testing the program in lua command line.  Comment or delete them before\r\n-- compiling the program for in-game usage, though!\r\n\r\n--for i=1,10 do\r\n-- io.write(description() .. \"\\n\");\r\n--end"
}
</syntaxhighlight>
 
== Running ==
Now each time the gem is looked at, a different color is returned (well, up to three colors anyway).

Latest revision as of 02:01, 14 July 2023

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

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

After escaping the program, you'll have some lua code to paste into the new json definition.

JSON Definition

The new lua code can be inserted:

{
	"type": "ENTITY",
	"name":
	{
		"literalString" : "Shiniest Gem"
	},
	"description":
	{
		"programString": true
	},
	"lua": "function description()\r\n\r\n   -- Create a blank string to store the color in\r\n   local colorString = \"\";\r\n\r\n   -- Get a random integer in the range of 1-3\r\n   local randomNumber = math.random(1,3);\r\n\r\n   -- Set color string to a different color based on the random number\r\n   if randomNumber == 1 then\r\n      colorString = \"red\"\r\n   elseif randomNumber == 2 then\r\n      colorString = \"green\"\r\n   elseif randomNumber == 3 then\r\n      colorString = \"blue\"\r\n   else\r\n      colorString = \"unknown\" -- As the number should be 1, 2, or 3, should not reach here\r\n   end\r\n\r\n   -- Return a string with (possibly) a different color each time\r\n   return \"The gem shines brightly, reflecting \" .. colorString .. \" into your eyes.\";\r\n\r\nend\r\n\r\n-- The below lines are commented out.  Uncommenting them can be helpful for\r\n-- testing the program in lua command line.  Comment or delete them before\r\n-- compiling the program for in-game usage, though!\r\n\r\n--for i=1,10 do\r\n-- io.write(description() .. \"\\n\");\r\n--end"
}

Running

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