1. Scripting basics - Quick look at Entity properties (that effect scripts) and how to create your first script.
*please note, this is aimed at new users, if you have any experience with writing scripts you can skip it*
function pickup_points_init(e)
end
function pickup_points_main(e)
end
function pickup_points_exit(e)
end
2. Displaying an entity name on screen - how to write your first script which will return an entity's name onto the screen in game.
weapon_name = {}
function pickup_points_init_name(e,name)
weapon_name[e] = name
end
function pickup_points_main(e)
if GetPlayerDistance(e) < 120 then
Prompt(weapon_name[e])
end
end
function pickup_points_exit(e)
end
3. Collecting an object for points - how to store points in a variable and increase them by collecting an object (also shows how to remove objects from the game world).
weapon_name = {}
points = 0
pressed = 0
function pickup_points_init_name(e,name)
weapon_name[e] = name
end
function pickup_points_main(e)
if GetPlayerDistance(e) < 120 then
Prompt("Collect "..weapon_name[e].." for "..g_Entity[e]['health'].." points?")
if g_KeyPressE == 1 and pressed == 0 then
pressed = 1
points = points + g_Entity[e]['health']
Destroy(e)
end
end
if g_KeyPressE == 0 then
pressed = 0
end
TextCenterOnX(50,10,4,"Points = "..points)
end
function pickup_points_exit(e)
end
4. (continuing on from 3) How to gain points by killing enemy characters - learn how to use the exit(e) and your first multiple script variable change
function ai_soldier_exit(e)
points = points + weapon_name[e]
end
5. Spend our collected points to unlock a door
-- LUA Script - precede every function and global member with lowercase name of script + '_main'
-- Door Prompts 'Closed' can be opened with entity collected specified by 'USE KEY'
-- state to ensure user must release E key before can open/close again
door_pressed = 0
function door_points_init(e)
end
function door_points_main(e)
PlayerDist = GetPlayerDistance(e)
if (PlayerDist < 100 ) and g_PlayerHealth > 0 then
GetEntityPlayerVisibility(e)
if 1 then
if g_Entity[e]['activated'] == 0 then
if g_Entity[e]['haskey'] == -1 then
SetActivated(e,3) --change this to 3
else
if g_Entity[e]['plrvisible'] == 1 then
if g_Entity[e]['haskey'] == 1 then
Prompt("The door is locked. Press E key to unlock door")
if g_KeyPressE == 1 then
SetActivated(e,3) --change this to 3
end
else
Prompt("The door is locked. Find a key to unlock door")
end
end
end
--added code
elseif g_Entity[e]['activated'] == 3 then
if GetPlayerDistance(e) < 120 then
local points_needed = 1000
local unlock_points = (points_needed-points)
if unlock_points <= 0 then
unlock_points = 0
Prompt("Spend "..points_needed.." points to unlock the door?")
else
Prompt("You need "..unlock_points.." more to unlock the door")
end
if points >= points_needed then
if g_KeyPressE == 1 then--and door_pressed == 0 then
--door_pressed = 1
SetActivated(e,1)
points = points - points_needed
end
end
end
--back to stock code
else
if g_Entity[e]['activated'] == 1 then
-- door is unlocked and closed
if g_Entity[e]['plrvisible'] == 1 then
Prompt("Press E to open door")
if g_KeyPressE == 1 and g_Entity[e]['animating'] == 0 and door_pressed == 0 then
SetAnimation(0)
PlayAnimation(e)
g_Entity[e]['animating'] = 1
SetActivated(e,2)
ActivateIfUsed(e)
PlaySound(e,0)
StartTimer(e)
g_Entity[e]['timer'] = g_Time
door_pressed = 1
end
end
else
if g_Entity[e]['activated'] == 2 then
-- door is open
if g_Entity[e]['plrvisible'] == 1 then
if g_KeyPressE == 1 and g_Entity[e]['animating'] == 0 and door_pressed == 0 then
SetAnimation(1)
PlayAnimation(e)
g_Entity[e]['animating'] = 1
SetActivated(e,1)
PlaySound(e,1)
CollisionOn(e)
door_pressed = 1
end
end
end
end
end
end
end
if g_Entity[e]['activated'] == 2 then
-- door collision off after 1 second
if GetTimer(e)>1000 then
CollisionOff(e)
end
end
if g_KeyPressE == 0 then
door_pressed = 0
end
--PromptLocal ( e, "activated=" .. g_Entity[e]['activated'] .. "PlayerDist=" .. PlayerDist .. " plrvisible=" .. g_Entity[e]['plrvisible'] .. " haskey=" .. g_Entity[e]['haskey'] )
end
*update: 08/06/2017 below*
show_points.lua
points = 0
--don't forget to set your entity to always active = yes
function show_points_init(e)
--hide and stop player from bumping into our object
--*because it's always active it may be best to use a low poly object*
Hide(e)
CollisionOff(e)
end
function show_points_main(e)
--simply show the points on screen at all times
TextCenterOnX(50,10,4,"You have "..points.." points")
--after 1 every second add 1 point to player's total
if GetTimer(e) > 1000 then
StartTimer(e) --resets the timer to start counting from 0 again
points = points + 1
end
end
--custom function included here as this script is always active and we will be using this function a lot
--for other scripts
function PlayerLooking(e,dis,v)
if g_Entity[e] ~= nil then
if dis == nil then
dis = 3000
end
if v == nil then
v = 0.5
end
if GetPlayerDistance(e) <= dis then
local destx = g_Entity[e]['x'] - g_PlayerPosX
local destz = g_Entity[e]['z'] - g_PlayerPosZ
local angle = math.atan2(destx,destz)
angle = angle * (180.0 / math.pi)
if angle <= 0 then
angle = 360 + angle
elseif angle > 360 then
angle = angle - 360
end
while g_PlayerAngY < 0 or g_PlayerAngY > 360 do
if g_PlayerAngY <= 0 then
g_PlayerAngY = 360 + g_PlayerAngY
elseif g_PlayerAngY > 360 then
g_PlayerAngY = g_PlayerAngY - 360
end
end
local L = angle - v
local R = angle + v
if L <= 0 then
L = 360 + L
elseif L > 360 then
L = L - 360
end
if R <= 0 then
R = 360 + R
elseif R > 360 then
R = R - 360
end
if (L < R and math.abs(g_PlayerAngY) > L and math.abs(g_PlayerAngY) < R) then
return 1
elseif (L > R and (math.abs(g_PlayerAngY) > L or math.abs(g_PlayerAngY) < R)) then
return 1
else
return 0
end
else
return 0
end
end
end
shop_keeper.lua
function shop_keeper_init_name(e,name)
--store the string to show on screen in the entity's name property
weapon_name[e] = name
end
function shop_keeper_main(e)
--if player is nearby then show the text
if GetPlayerDistance(e) < 200 then
PromptLocal(e,weapon_name[e])
end
end
shop_weapon.lua
local points_needed = {}
pressed = 0
function shop_weapon_init_name(e,name)
--the numbers in the array (in square brackets [ ]) are the entity numbers used
--the numbers after the equals are the points cost
points_needed[21] = 100
points_needed[57] = 400
points_needed[58] = 200
weapon_name[e] = name
end
function shop_weapon_main(e)
--using a function to check if the player is looking at the object
--don't forget to include the function in the your game if you aren't using show_points.lua
if PlayerLooking(e,100,5) == 1 then
--check player has enough (or more points)
if points >= points_needed[e] then
PromptLocal(e,"Spend "..points_needed[e].." points for "..weapon_name[e].."?")
--player confirms purchasing weapon
if g_KeyPressE == 1 and pressed == 0 then
pressed = 1
--remove points (cost) from player total
points = points-points_needed[e]
AddPlayerWeapon(e)
Destroy(e)
PlaySound(e,0)
end
else --player doesn't have enough points so show him how many more he needs
PromptLocal(e,"You need "..points_needed[e]-points.." more points for the "..weapon_name[e])
end
if g_KeyPressE == 0 then
pressed = 0
end
end
end
shop_ammo.lua
local points_needed = {}
pressed = 0
function shop_ammo_init_name(e,name)
points_needed[59] = 50
weapon_name[e] = name
end
function shop_ammo_main(e)
if PlayerLooking(e,100,5) == 1 then
if points >= points_needed[e] then
PromptLocal(e,"Spend "..points_needed[e].." points for "..weapon_name[e].."?")
if g_KeyPressE == 1 and pressed == 0 then
pressed = 1
points = points-points_needed[e]
AddPlayerAmmo(e)
Destroy(e)
PlaySound(e,0)
end
else
PromptLocal(e,"You need "..points_needed[e]-points.." more points for the "..weapon_name[e])
end
if g_KeyPressE == 0 then
pressed = 0
end
end
end
shop_health.lua
local points_needed = {}
pressed = 0
function shop_health_init_name(e,name)
points_needed[e] = 300
--points_needed[61] = 300
weapon_name[e] = name
end
function shop_health_main(e)
if PlayerLooking(e,100,5) == 1 then
if points >= points_needed[e] then
PromptLocal(e,"Spend "..points_needed[e].." points for "..weapon_name[e].."?")
if g_KeyPressE == 1 and pressed == 0 then
pressed = 1
points = points-points_needed[e]
AddPlayerHealth(e)
Destroy(e)
PlaySound(e,0)
end
else
PromptLocal(e,"You need "..points_needed[e]-points.." more points for the "..weapon_name[e])
end
if g_KeyPressE == 0 then
pressed = 0
end
end
end
function shop_health_exit(e)
end
i've also made a few changes to the previous scripts (not edited above so as not to cause confusion with the previous videos) which can be found in the attachment (along with the new scripts and the map used in the new video *video coming soon*).
Making a basic game using the scripts we've learned so far.
*video coming soon ~ 2hours upload time*
---------------------------------------
Another good source of videos and tutorials can be found on the
official GameGuru twitchcasts on youtube
and also check the link in my signature for a good written guide on scripting in GG
i will continue adding videos and tutorials as time permits and if others have any videos to add please do so.
if anyone has any suggestions or questions please post them (but i will try to keep my guides aimed at the simple scripts so requests for scripts that will take a long time to write or would be too confusing to follow will likely not be covered).