Entity Tutorial 2: Simple Script: Difference between revisions

From SkyCorp Global
Jump to navigation Jump to search
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 7: Line 7:
Now for '''description''', we set the parameter '''programString''' to true, to indicate that there is a description() function in the lua code that can be called for the description.  It is a 'string' because that is the return type of '''description()'''.  Other parameters might be '''programBoolean''', '''programVoid''', etc.  See the reference docs for the return type.
Now for '''description''', we set the parameter '''programString''' to true, to indicate that there is a description() function in the lua code that can be called for the description.  It is a 'string' because that is the return type of '''description()'''.  Other parameters might be '''programBoolean''', '''programVoid''', etc.  See the reference docs for the return type.


There is one other new parameter in the JSON, something called '''luac'''.  That is the base64 version of the compiled lua program.  Let's generate that in the next step.
There is one other new parameter in the JSON, something called '''lua'''.  That is the json escaped version of the lua source code.  Let's generate that in the next step.


<syntaxhighlight lang="json">
<syntaxhighlight lang="json">
Line 20: Line 20:
"programString": true
"programString": true
},
},
"luac": "???"
"lua": "???"
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 44: Line 44:
</syntaxhighlight>
</syntaxhighlight>


What the game will do is see that description has a programString available, and when requesting description will call description() and use its return value for the description.
Now, let's put this into the JSON.  We will need to escape this source code so it can be pasted in the lua field.  Some advanced text editors will have a "JSON Escape" feature, or just use a website like [https://www.freeformatter.com/json-escape.html this].  


If we just try to run this code from the command line, it won't be overly interesting:
Doing so will result in a very long line of text like so:
> lua shinierGem.lua                                                       
Nothing happens.  This is because the game calls the description() function, whereas nothing is calling the description function for the command line version of lua.  We could quickly hack this just for quick testing purposes by adding a call to description():


<syntaxhighlight lang="lua" highlight="15-16">
<code>-- Description is called when an object's description is shown to the\r\n-- player or otherwise requested.  The object should return a string\r\n-- containing the description.\r\n\r\nfunction description()\r\n\r\n  -- Create a descriptive string\r\n  local descriptiveText = \"The gem shines programatically as you look at it.\";\r\n\r\n  -- Return it\r\n  return descriptiveText;\r\n\r\nend</code>
-- Description is called when an object's description is shown to the
-- player or otherwise requested.  The object should return a string
-- containing the description.
 
function description()
 
  -- Create a descriptive string
  local descriptiveText = "The gem shines programatically as you look at it.";
 
  -- Return it
  return descriptiveText;
 
end
 
-- NEW!  Output return value of description() for quick test:
io.write("Just for testing on the command line purposes, description returns: " .. description() .. "\n");
</syntaxhighlight>
 
Note the last added line, this will now output to the console if we run the LUA through the command line.
 
> lua shinierGem.lua
Just for testing on the command line purposes, description returns: The gem shines programatically as you look at it.
 
Running from the command line can be helpful for finding any LUA syntax or logic errors, if your program doesn't rely on any functions from the game.  However, since the command line version of LUA doesn't know about any game logic, most advanced programs can't use this trick.  Regardless, now that it's been tested, we should remove the debug statement and go back to our original program:
 
<syntaxhighlight lang="lua">
-- Description is called when an object's description is shown to the
-- player or otherwise requested.  The object should return a string
-- containing the description.
 
function description()
 
  -- Create a descriptive string
  local descriptiveText = "The gem shines programatically as you look at it.";
 
  -- Return it
  return descriptiveText;
 
end
</syntaxhighlight>
 
Now, let's compile the program.  We can use luac instead of lua to create lua bytecode instead of running the program.
 
> luac shinierGem.lua
 
There is now a luac.out file.  If you open it in a text editor, it'll look like gibberish as it's a binary file.  Since we need to put this luac into the JSON file from earlier, we'll use the base64 utility to convert the binary file into an ASCII representation.
 
> base64 luac.out
G0x1YVEAAQQEBAgAEAAAAEBzaGluaWVyR2VtLmx1YQAAAAAAAAAAAAAAAgIDAAAAJAAAAAcAAAAeAIAAAQAAAAQMAAAAZGVzY3JpcHRpb24AAQAAAAAAAAAFAAAADQAAAAAAAAIDAAAAAQAAAB4AAAEeAIAAAQAAAAQyAAAAVGhlIGdlbSBzaGluZXMgcHJvZ3JhbWF0aWNhbGx5IGFzIHlvdSBsb29rIGF0IGl0LgAAAAAAAwAAAAgAAAALAAAADQAAAAEAAAAQAAAAZGVzY3JpcHRpdmVUZXh0AAEAAAACAAAAAAAAAAMAAAANAAAABQAAAA0AAAAAAAAAAAAAAA==


Now, let's copy/paste that text into the luac json...
Now, let's copy/paste that text into the lua json...


<syntaxhighlight lang="json">
<syntaxhighlight lang="json">
Line 115: Line 63:
"programString": true
"programString": true
},
},
"luac": "G0x1YVEAAQQEBAgAEAAAAEBzaGluaWVyR2VtLmx1YQAAAAAAAAAAAAAAAgIDAAAAJAAAAAcAAAAeAIAAAQAAAAQMAAAAZGVzY3JpcHRpb24AAQAAAAAAAAAFAAAADQAAAAAAAAIDAAAAAQAAAB4AAAEeAIAAAQAAAAQyAAAAVGhlIGdlbSBzaGluZXMgcHJvZ3JhbWF0aWNhbGx5IGFzIHlvdSBsb29rIGF0IGl0LgAAAAAAAwAAAAgAAAALAAAADQAAAAEAAAAQAAAAZGVzY3JpcHRpdmVUZXh0AAEAAAACAAAAAAAAAAMAAAANAAAABQAAAA0AAAAAAAAAAAAAAA=="
"lua": "-- Description is called when an object's description is shown to the\r\n-- player or otherwise requested.  The object should return a string\r\n-- containing the description.\r\n\r\nfunction description()\r\n\r\n  -- Create a descriptive string\r\n  local descriptiveText = \"The gem shines programatically as you look at it.\";\r\n\r\n  -- Return it\r\n  return descriptiveText;\r\n\r\nend"
}
}
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 02:11, 14 July 2023

In this tutorial, we will upgrade the Shiny Gem object from Tutorial 1. Now instead of using a literal string for the description, the description will be generated by LUA code.

Generally, if only returning a static string, a string literal like in the previous tutorial should be used due to performance reasons. However, this will demonstrate the simplest possible LUA code before we expand upon it in the next tutorial.

Definition Upgrade

Now for description, we set the parameter programString to true, to indicate that there is a description() function in the lua code that can be called for the description. It is a 'string' because that is the return type of description(). Other parameters might be programBoolean, programVoid, etc. See the reference docs for the return type.

There is one other new parameter in the JSON, something called lua. That is the json escaped version of the lua source code. Let's generate that in the next step.

{
	"type": "ENTITY",
	"name":
	{
		"literalString" : "Shinier Gem"
	},
	"description":
	{
		"programString": true
	},
	"lua": "???"
}

Writing the LUA code

We create a simple LUA program in a folder on our HDD and name it shinierGem.lua:

-- Description is called when an object's description is shown to the
-- player or otherwise requested.  The object should return a string
-- containing the description.

function description()

   -- Create a descriptive string
   local descriptiveText = "The gem shines programatically as you look at it.";

   -- Return it
   return descriptiveText;

end

Now, let's put this into the JSON. We will need to escape this source code so it can be pasted in the lua field. Some advanced text editors will have a "JSON Escape" feature, or just use a website like this.

Doing so will result in a very long line of text like so:

-- Description is called when an object's description is shown to the\r\n-- player or otherwise requested.  The object should return a string\r\n-- containing the description.\r\n\r\nfunction description()\r\n\r\n   -- Create a descriptive string\r\n   local descriptiveText = \"The gem shines programatically as you look at it.\";\r\n\r\n   -- Return it\r\n   return descriptiveText;\r\n\r\nend

Now, let's copy/paste that text into the lua json...

{
	"type": "ENTITY",
	"name":
	{
		"literalString" : "Shinier Gem"
	},
	"description":
	{
		"programString": true
	},
	"lua": "-- Description is called when an object's description is shown to the\r\n-- player or otherwise requested.  The object should return a string\r\n-- containing the description.\r\n\r\nfunction description()\r\n\r\n   -- Create a descriptive string\r\n   local descriptiveText = \"The gem shines programatically as you look at it.\";\r\n\r\n   -- Return it\r\n   return descriptiveText;\r\n\r\nend"
}

Testing

That's the finished JSON, ready for testing in-game! Use the UGC Control Panel as in the last tutorial, and when you look at the object in-game, the description should be "The gem shines programatically as you look at it." You just made a LUA object!