MachineCraft

MachineCraft

Not enough ratings
Introduction to Lua Scripting
By pringding
Introduction to Lua Programming in MachineCraft
   
Award
Favorite
Favorited
Unfavorite
1
Lua scripting in this game is quite a hidden feature. There are AI bots in the _samples folder that show what Lua scripting can do, but they don't have guides yet. If you've ever seen a player in-game with a green name, that means that they're using a Lua script to control their bot automatically.
2
If you're completely new to the Lua programming language (or to programming in general) I recommend you watch the first 10 minutes of this video: https://www.youtube.com/watch?v=iMacxZQMPXs
It goes over some of the basic things you'll need to know when making your own scripts.
3
The best place to start is with the AI machines in the _samples folder, the most basic is "AI_Mortar". Try out the bot in Test mode (don't press any buttons to control it, let it work on it's own). There should be some white text in the top-right corner, this is from the Print function in the script, you can use this to display debug information so that if something goes wrong in your script, you can see what went wrong. The game will also tell you about any errors in your script by displaying a yellow message in the top-left corner.
4
After you've tried out the AI bot, go to the _scripts folder (should be located here: C:\Program Files (x86)\Steam\steamapps\common\MachineCraft\UserData\_scripts). This is where all of the default scripts are and it is where you should put the scripts that you make. Open the file named "mortar.txt" (best program to use is Notepad++, but Notepad should do) and look at the code past "function_update()". This is where you should put most of your code to control your machine.
5
In my next guide I will explain how you can use the different functions avaliable to control your machine. There is a document which containing explanations of the code (unfinished) here: https://docs.google.com/document/d/1k0a744BAum2PlR5GVfO8Fg6ZNIakNhxZNutSeMlNrjY/edit?usp=sharing
If you have any questions just ask them in the comments.

*I haven't written a guide like this in months so sorry if it isn't great
9 Comments
MJRAHOLE 17 Apr, 2017 @ 7:31pm 
@Turret problems

Workshop Items lack an AI Script, because we are builders, not AI designers.
Turret problems 17 Sep, 2016 @ 11:36am 
where are the scripts workshop items use?
RA2lover 21 Aug, 2016 @ 3:37am 
That document is also outdated. Luaスクリプト<kanji i've never managed to learn>.txt now makes mentions of drawing-related functions:
[list]
[*]SetLogicalScreenHeight(float height)
[*]DrawLine2D(Color color [, Vector2 vtx1, Vector2 vtx2])
[*]DrawLine2D(Vector2 vtx1, Vector2 vtx2)
[*]MakePattern2D(string name)
[*]DrawPattern2D(string name, Color color, Vector2 pos [, Vector2 scale])
[*]DrawGraph(string name [, float val , int num]?, Color color, Vector2 pos, Vector2 scale
[*]DrawLine3D(Color color [, Vector3 vtx1, Vector3 vtx2])
[*]DrawLine3D(Vector3 vtx1, Vector3 vtx2)
[*]MakePattern3D(string name)
[*]DrawPattern3D(string name, Color color, Vector3 pos [, Vector3 scale])
[/list]

Can't really translate them because the file has mojibake issues, although it seems most of these seem to be straightforward to use.
Turret problems 24 Jun, 2016 @ 7:41pm 
nvm
Turret problems 24 Jun, 2016 @ 7:39pm 
how do i use the scripts?
PanTurtle 4 May, 2016 @ 10:46pm 
nv i realized how to make my machine how to use scripts
PanTurtle 4 May, 2016 @ 10:33pm 
how do i make sure in the script that it is controling it in the txt? All i really want is to take the autofire script there and put it on my Aircraft Carrier