Page 1 of 1

New AI related Lua Functions

Posted: Thu May 23, 2013 9:44 pm
by Jabantiz
Details on each function on their wiki pages
GetFollowTarget
IsFollowing
SetBrainTick
SetFollowTarget
SetLuaBrain
ToggleFollow

Here is an example using all of the above functions and using a lua brain for AI

Code: Select all

function spawn(Spawn)
    -- Makes the NPC use the Think function in lua for its AI
	SetLuaBrain(Spawn)
    -- Have the AI function be called twice a second
    SetBrainTick(500)
end

function respawn(Spawn)
    -- Make sure to call the spawn function on a respawn so the brain gets set back to a lua brain
	spawn(Spawn)
end

-- This is the function that will be called for the AI if set to use a lua brain
function Think(NPC, Target)
    -- Check to see if the spawn is not currently following some one and that there follow target is not null
	if not IsFollowing(NPC) and GetFollowTarget(NPC) ~= nil then
        -- Toggle the follow flag on
		ToggleFollow(NPC)
	end
end

function hailed(NPC, Spawn)

	FaceTarget(NPC, Spawn)

	-- create the conversation
	conversation = CreateConversation()

	AddConversationOption(conversation, "Start", "Choice1")
	AddConversationOption(conversation, "Cancel")

	-- set the npc dialog and start the conversation
	StartConversation(conversation, NPC, Spawn, "Follow test")
end

function Choice1(NPC, Spawn)
    -- Check if the npc is following some one
	if not IsFollowing(NPC) then
    -- Not following any one then set the follow target to the player that hailed it
		SetFollowTarget(NPC, Spawn)
	else
    -- Is following some one then clear follow target (clearing follow target will also toggle following off so no need to call ToggleFollow)
		SetFollowTarget(NPC, 0)
	end
end