• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by shoffing · Nov 20, 2014 at 08:35 PM · c#programmingmodding

Best way to limit the functionality of Lua scripting?

I'm working on a game project where players will build and code their own spaceships to do battle with other players' spaceships. Think CodeCombat / FightCode, but in space with cool 2D physics and stuff. Here's a demo video of what I have working so far.

I've chosen to implement the ship scripting with Lua, as it's a good scripting language that is relatively easy to implement. I'm using NLua (specifically Unity3D-NLua) as an interface between my C# code and the Lua scripts, and everything is going swimmingly. It was super easy to link my MonoBehaviour scripts to a Lua instance - it goes something like this:

 using NLua;
 
 Lua lua = new Lua();
 lua["thrusterLeft"] = thrusterObject.GetComponent<Part>();

Where thrusterObject is a GameObject containing a Component that is a child class of Part (e.g. PartThruster, PartTurret, PartRanger, etc). Then, in Lua, the player's script can simply do:

 thrusterLeft:SetThrust(0.8)

And so it will be. Easy, right? But there's a problem! Because there are no restrictions on what the player can do with my GameObject in the Lua script, the player is able to use functions that should be out of bounds. For example, they can manually set properties like thrusterLeft.transform.position and thrusterLeft.rigidbody2D.velocity. This is obviously not intended behaviour. So I'm faced with this problem - how can I prevent the Lua scripts from having too much power?

I can think of a few potential solutions, but they all will require quite a bit of work to implement. If anybody's done something like this before, I would be interesting to know how you approached it.

If you need more details about how my code is set up, you can check out the project repository on GitHub. Some files you might want to check out are:

  • Scripts/Core/ShipLoader.cs - Loads a ship from its JSON file, instantiates the parts and ShipContainer

  • Scripts/Ship/ShipContainer.cs - Contains the parts and the Lua script instance/functionality

  • Scripts/Ship/Parts/Part.cs - Parent class for every ship part

  • Scripts/Ship/Parts/PartThruster.cs - Example of what a ship part class looks like, extends Part

  • Ships/ship5.lua - Example of what a ship script might look like, this is the script for the ship featured in the demo video I linked in the first paragraph

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Oct 21, 2015 at 06:26 PM 1
Share

404 content not found for project repository. Is it private?

Ah, looks like someone bumped an old post.

avatar image Bunny83 Statement · Oct 21, 2015 at 06:44 PM 0
Share

Found the post and published it since it's exactly what i wanted to write ^^.

avatar image oraknabo Statement · Oct 21, 2015 at 07:16 PM 0
Share

When I posted an answer I didn't even bother looking at the original's date either.

avatar image shoffing Statement · Oct 21, 2015 at 07:28 PM 0
Share

Yes sorry, since I made this post a year ago I've made the repo private.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by oraknabo · Oct 21, 2015 at 06:42 PM

You could try making a "wrapper" class that holds the component you're passing to Lua, then only expose the methods you want users to have access to:

 public class ThrusterWrapper{
   private Part thruster;
   
   public ThrusterWrapper(Part thruster){
     this.thruster = thruster;
   }
   
   public void setThrust(float power){
     thruster.SetThrust(power);
   }
 }

Then create an instance of ThrusterWrapper and pass that to Lua:

  Lua lua = new Lua();
  lua["thrusterLeft"] = new ThrusterWrapper (thrusterObject.GetComponent<Part>());
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shoffing · Oct 21, 2015 at 07:30 PM 0
Share

Yes, this is exactly what I ended up doing! I have two types of Part classes - Part and LPart.

So PartThruster will hold all the stuff that makes the thruster work, and LPartThruster is just a wrapper that calls PartThruster's methods. It works well!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Getting nullreference error while using sendmessage 1 Answer

Constructor giving nullreference error 1 Answer

IOS touch vs OnMouseDown & error messages 0 Answers

Question on C# Interface Method Parameters 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges