A complete introduction to scripting for games using the versatile Lua programming language and the Core game development platform. Add an array of game coding skills to your toolbox and take your game development to new heights!
Up until this lesson, you have learned how to use Lua, a versatile language with many different applications. To use Lua in Core, we will also need a set of objects and functions created for the platform, which are all part of the Core API
API stands for Application Programming Interface, and is a set of useful objects and functions that allow you to operate a tool. In this case, the tool that you are driving is the Core platform.
The makers of Core created a game development system, and then created the functions that you could use to make your game.
You can still create your own variables and functions, but these are designed to specifically work with the kinds of things you find in Core, like players, 3D objects, and sound and visual effects.
Start by going to https://docs.coregames.com/core_api/ to see the complete list of all the object types in Core, and what you can do with each of them.
When you are deciding how to approach a programming solution, follow these steps.
Everything object Core is a CoreObject, and therefore has all the properties and functions in this section of the API documentation. They may also be more specific things, like Players, Abilities, or Cameras.
Each entry in the API Documentation has three sections: Event, Function, and Property. We will discuss events in the next lesson, so for now we will focus on these second two.
Properties are the attributes that you can access about an object using
The Return Type column will tell you the type of properties.
One of the property return types that you will see is an enum. This is a type of data that assigns a number to a meaning. Like a menu, “Press 1 for more information, press 2 to start an order …”
For example, Visibility has only 3 values: Inherit From Parent, Force On, and Force Off.
In code, what this means is:
print(Visibility.FORCE_OFF) -- will print "2"
Not every property of an object is something that you should be able to change, but you may still need to look up what they are. These are Read-Only values.
A player’s ID, for example, is consistent for them across all Core games. No one game should be able to just change it. However, if you want to give a particular player special priviledges, you will need to be able to read that property.
Properties that you can change are Read-Write, meaning that you can both find out what they are, and change it.
Core API functions are behavior that Core Objects can either do, or can be done to them.
You can identify functions because they start with uppercase letter, and you call them using a
local positionVariable = myObjectName:GetPosition()
You will almost always use
: with the functions you find in the API.
You can call a CoreObject function with a
., but the first parameter on all these functions is the object itself. The
:puts the object in as the first argument for you. If you get an error that says “Incorrect number or arguments”, it could just be that you used a
.instead of a
You need to supply an input for every parameter listed in the function. The
AttachToPlayer(Player, string socketName) for example, requires two inputs. A Player-type object and a string that matches one of the player sockets, like
Events are notifications that Core sends to your code when certain expected behaviors happen are your Core Objects. We will discuss them in more depth in the next chapter.