1
\$\begingroup\$

I'm looking to create a game like screeps, in which the user controls their units using javascript. I am planning on using lua as the scripting language that controls the units in my game, as it is supposedly easily embeddable. After doing some research, I decided that I should code the game in C++ and allow the player to access the objects (or the C++ equivalent of Lua's tables) in C++ that represent the units in the game through lua.

So far I have been able to send C++ functions to a Lua environment using LuaBridge. However, I'm not sure how to expose unit objects to the player through lua. The player should only be able to change certain attributes of the object (for example, they shouldn't be allowed to change the object's position to anywhere on the map, but they should be able to view the unit's position). The user should also be allowed to run functions like unit1.forward that can change the units position.

What is the best way to implement this behavior?

\$\endgroup\$
  • \$\begingroup\$ Hi, and welcome to GDSE! I have removed the part that asks for other languages as they're off topic here. \$\endgroup\$ – Vaillancourt Nov 20 '17 at 12:01
1
\$\begingroup\$

There is never a best way to do something in software development, only the way which works best for you.

But what I would do is not expose the whole Unit object to the Lua engine. I would expose only a façade which exposes only those functions which are supposed to be available to the scripting engine and also does internal plausibility and permission checks before forwarding it to the actual Unit object wrapped by it.

I would also make a difference between those units the player controls and those they do not control. For example, if a player-controlled unit has a function like getNearbyEnemies(), it would return an array table of units, but the enemy units would be represented by a different façade which only exposes those methods the player-unit should be able to call (getPosition() but not selfDestruct())

That façade on the C++ side should also create and cache the lua table for its own representation on the Lua side. Rebuilding the lua table from an object whenever you call the scripting engine is an expensive operation, so you should keep the table for each unit in C++ memory (if you properly encapsulate your classes, it will only contain pointers to member functions, so it will never change during the unit lifetime). This means you actually have three representations of a unit on the C++ layer: The actual unit, the façade and the lua table. This might seem as if the façade C++ class is redundant, because it can be represented by the table alone. But I would still keep the façade, because it gives you a clean separation of the scripting interface from the internal representation of your Unit. This will become especially useful if you ever decide to switch the scripting backend.

\$\endgroup\$
  • \$\begingroup\$ I was initially going to use C++ for speed, but I now know that C++ and node js are relatively similar in speed. Would it be simpler to just make the whole game in javascript and expose certain parts through a library as you describe? \$\endgroup\$ – Vityou Nov 21 '17 at 1:38
  • \$\begingroup\$ @Vityou node.js is not remotely close to C++ as far as speed is concerned (and I'd be curious to see the reference for your statement). But that's not your primary concern: are you more comfortable working with C++ or JS? Do you prefer an all-out dynamic language or one with more features? Make your choice based on your use of the language, and worry about speed later. You can still implement parts of your game in C++ later if JS proves not to be fast enough. \$\endgroup\$ – Quentin Nov 21 '17 at 9:23
  • \$\begingroup\$ @Vityou I'm sorry, but we generally don't give technology recommendations here, because they are too much based on personal preference and specific project requirements. \$\endgroup\$ – Philipp Nov 21 '17 at 9:48
  • \$\begingroup\$ @Quentin stackoverflow.com/questions/12195841/… \$\endgroup\$ – Vityou Nov 21 '17 at 18:00
  • \$\begingroup\$ @Philipp I was wondering more if the same thing can be implemented as a js library, or does the language have to be embedded to achieve that level of control. If I made it as a js library could the user modify any part of the game that he wants, or is there a way to restrict the users permissions? \$\endgroup\$ – Vityou Nov 21 '17 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.