I see a few times on the Forum people are struggling with the new scripting language but it can be really simple and im just going to share a quick tip i use:
First up NotePad++, i find this very useful for scripting in LUA as it has full syntax highlighting support and automatically saving as .lua,
can get it from here:
http://notepad-plus-plus.org/
so open up NotePad++ and open a script that you would like to reference to while you learn, for example i will just put here my character conditions script (hunger and thirst basically)
Note there is a much more heavily commented one in the forums if you would prefer to use that.
-- Script By AuShadow
-- Player Conditions script, 1 script for all my player conditions including cash
-- Character Aspects
CharLvl = 1
CharExp = 0
-- Survival Aspects
Appetite = 1010
Thirst = 1045
-- Cash aspects
Cash = 0
local TimeStamp1 = 0
local TimePassed = 0
local Time1 = 0
local Time2 = 0
function Player_Conditions_init(e)
end
function Player_Conditions_main(e)
if TimeStamp1 == 0 then
Time1 = g_Time
TimeStamp1 = 1
end
Time2 = g_Time;
TimePassed = Time2 - Time1;
if TimePassed > 1000 then
Appetite = Appetite - 1;
Thirst = Thirst - 1;
if Thirst < 1000 then
Prompt ("I'm Dehydrated");
HurtPlayer(e,1);
end
if Appetite < 1000 then
Prompt ("I'm Starving");
HurtPlayer(e,1);
end
TimeStamp1 = 0;
end
if Appetite >= 5001 then
Appetite = 5000;
end
if Thirst >= 3001 then
Thirst = 3000;
end
if Appetite > 2000 and Appetite < 3000 then
Prompt ("Getting Hungry");
end -- AuShadow
if Appetite > 1000 and Appetite < 2000 then
Prompt ("Getting Really Hungry");
end
if Thirst > 1000 and Thirst < 2000 then
Prompt ("I'm getting Thirsty");
end -- AuShadow
end
That script has a timer example, some declared globals and locals, and the basics functions_init and such i will cover this in a minute.
Once this is copied into NotePad++ you may wonder where the syntax highlighting is well, In the top left you will see a dropdown called Language, clcik that, scroll down to L, then select LUA, and see all the text come to colour and life, just jokes but it does make it easier to read. Now go to File New, again select the LUA language for the new page.
Now to make it easier to reference to the other script you have open you will see the 2 page tabs near the top left, right click on the one your working with and click Move to Other View, and bam you now have side by side views within the program and now you can start your script with a reference, i still do this with my cheat sheet type file of which here is an early version:
--Lua Cheat Sheet
-- Must have in script
function scriptname_init(e)
--init Codes here ????
end
function scriptname_main(e)
--Main script here
end
--Distance Calculations:
PlayerDX = g_Entity[e]['x'] - g_PlayerPosX;
PlayerDY = g_Entity[e]['y'] - g_PlayerPosY;
PlayerDZ = g_Entity[e]['z'] - g_PlayerPosZ;
PlayerDist = math.sqrt(math.abs(PlayerDX*PlayerDX)+math.abs(PlayerDY*PlayerDY)+math.abs(PlayerDZ*PlayerDZ));
--Character Variables GLOBAL
charexp = 0
charlvl = 1
currentcash = 0
--Character Condition Variables GLOBAL
Thirst = 3000
Appetite = 5000
Bleeding = 0
-- Timer
-- Timer Variables LOCAL
local TimeStamp1 = 0
local TimePassed = 0
local Time1 = 0
local Time2 = 0
local Delay = 1000
-- Timer Code, set Delay to how many milliseconds between recurrence
if TimeStamp1 == 0 then
Time1 = g_Time
TimeStamp1 = 1
end
Time2 = g_Time;
TimePassed = (Time2 - Time1) * 0.001;
if TimePassed > Delay then
TimeStamp1 = 0;
end
--Prompt Example with Variables readout
Prompt("Press E to pickup exp, exp =" .. charexp);
Prompt("Time =" .. TimePassed .. "seconds")
so some quick notes before you start your script, if your script is Called MyScript.Lua then in the script you must have:
--Declare globals and local variables here before the script use
local YourVariableNameHere = 0
-- to define it as local, no local tag will mean its a global variable
function MyScript_init(e)
end
function MyScript_main(e)
--write all your script here
end
grammer and spelling are critical prompt is not the same as Prompt
using NotePad++ over Notepad has the advantage of when you get a LUA error at runtime using reloaded it gives you a line number where it went wrong and you can quickly find that line number in NotePad++
Hope this helps somebody
p.s when you set language to LUA this is also what tells NotePad++ to save the file as a .lua