Skip to content

Commit

Permalink
simplify API loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian-Toney committed May 18, 2022
1 parent 70696f5 commit 6a03518
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 59 deletions.
48 changes: 14 additions & 34 deletions client/DialogueClientScript/API.lua
Original file line number Diff line number Diff line change
@@ -1,45 +1,25 @@
-- Initalize the API
print("[Dialogue Maker]: Initalizing client API...");
print("[Dialogue Maker]: Initializing client API...");

local API = {};
for _, module in ipairs(script:GetChildren()) do

API[module.Name] = require(module);

end;

-- Pass the API to other methods
for _, module in pairs(API) do
for _, instance in ipairs(script:GetChildren()) do

if typeof(module) == "table" then
-- Get the table.
module = require(instance);

-- Check if we need to set the API.
if module._setAPI then

for funcName, funcCode in pairs(module) do

if typeof(funcCode) == "function" then

local OldEnvironment = getfenv(funcCode);
local NewEnvMT = setmetatable({

script = script;
API = API;

}, {

__index = function(_, index)

return OldEnvironment[index]

end;

});
setfenv(funcCode, NewEnvMT);

end;

end;
module._setAPI(API);

end;

-- Add it to the table.
API[instance.Name] = module;

end;

-- And we're done!
print("[Dialogue Maker]: Client API now available!");

return API;
36 changes: 20 additions & 16 deletions client/DialogueClientScript/API/Dialogue.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ local RemoteConnections = ReplicatedStorage:WaitForChild("DialogueMakerRemoteCon
local Players = game:GetService("Players");
local Player = Players.LocalPlayer;

-- Ignore the API warning
API = nil;

-- Prepare these methods
local DialogueModule = {

PlayerTalkingWithNPC = script.PlayerTalkingWithNPC;

};

local API;
function DialogueModule._setAPI(api)

API = api;

end

function DialogueModule.GoToDirectory(currentDirectory: Folder, targetPath: {string}): Folder

local CurrentPath = "1";
Expand Down Expand Up @@ -149,13 +153,13 @@ function DialogueModule.ReadDialogue(npc: Model)

-- Make sure we aren't already talking to an NPC
if not DialogueModule.PlayerTalkingWithNPC.Value then

-- Make sure we can't talk to another NPC
DialogueModule.PlayerTalkingWithNPC.Value = true;
API.Triggers.DisableAllSpeechBubbles();
API.Triggers.DisableAllClickDetectors();
API.Triggers.DisableAllProximityPrompts();

-- Set up variables we're gonna use
local NPCPrimaryPart = npc.PrimaryPart;
local DialogueContainer = npc:FindFirstChild("DialogueContainer");
Expand All @@ -173,7 +177,7 @@ function DialogueModule.ReadDialogue(npc: Model)
local ConversationTimeoutInSeconds = DialogueSettings.ConversationTimeoutInSeconds or (DialogueSettings.General and DialogueSettings.General.ConversationTimeoutInSeconds);
local WaitForResponse = DialogueSettings.WaitForResponse or (DialogueSettings.General and DialogueSettings.General.WaitForResponse);
local ResponseContainer, ResponseTemplate, ClickSound, ClickSoundEnabled, OldDialogueGui;

-- If necessary, freeze the player
if FreezePlayer then

Expand All @@ -183,9 +187,9 @@ function DialogueModule.ReadDialogue(npc: Model)

-- Set the theme and prepare the response template
local function SetupDialogueGui()

local NPCNF = DialogueGui.DialogueContainer.NPCNameFrame;

-- Set up responses
DialogueGui.Parent = Player:WaitForChild("PlayerGui");
ResponseContainer = DialogueGui.DialogueContainer.ResponseContainer;
Expand Down Expand Up @@ -223,7 +227,7 @@ function DialogueModule.ReadDialogue(npc: Model)

-- Listen to theme changes
local ThemeChangedEvent = API.GUI.CurrentTheme.Changed:Connect(function(newTheme)

DialogueGui:Destroy();
DialogueGui = newTheme;
SetupDialogueGui();
Expand Down Expand Up @@ -267,7 +271,7 @@ function DialogueModule.ReadDialogue(npc: Model)
CurrentDirectory = RootDirectory;

elseif RemoteConnections.PlayerPassesCondition:InvokeServer(npc, CurrentDirectory) then

local MessageText = API.Dialogue.ReplaceVariablesWithValues(npc, CurrentDirectory.Message.Value);
local ThemeDialogueContainer = DialogueGui.DialogueContainer;
local ResponsesEnabled = false;
Expand All @@ -281,7 +285,7 @@ function DialogueModule.ReadDialogue(npc: Model)
local Position = 0;
local Adding = false;
local TextContainer, ContinueDialogue, ResponseChosen, DividedText;

-- Run the before action if there is one
if CurrentDirectory.HasBeforeAction.Value then
RemoteConnections.ExecuteAction:InvokeServer(npc, CurrentDirectory, "Before");
Expand Down Expand Up @@ -466,7 +470,7 @@ function DialogueModule.ReadDialogue(npc: Model)
end;

end;

DialogueGui.Enabled = true;
for index, page in ipairs(DividedText) do

Expand All @@ -475,7 +479,7 @@ function DialogueModule.ReadDialogue(npc: Model)
for wordIndex, word in ipairs(page) do

local Extras = "";

if wordIndex ~= 1 then

Position += 1;
Expand All @@ -488,7 +492,7 @@ function DialogueModule.ReadDialogue(npc: Model)
Message = Message .. " ";

end;

for _, letter in ipairs(word:split("")) do

Adding = false;
Expand All @@ -505,7 +509,7 @@ function DialogueModule.ReadDialogue(npc: Model)
if IP then

local Replacement = letter;

for _, tag in ipairs(IP) do

if not tag.OriginalPosition then
Expand Down Expand Up @@ -689,7 +693,7 @@ function DialogueModule.ReadDialogue(npc: Model)
end;

end;

-- Free the player :)
ThemeChangedEvent:Disconnect();
API.Triggers.EnableAllSpeechBubbles();
Expand Down
24 changes: 15 additions & 9 deletions client/DialogueClientScript/API/GUI.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ function GUIModule.GetDefaultThemeName(): string

-- Check if the theme is in the cache
if DefaultThemeName then

return DefaultThemeName;

end;

-- Call up the server.
Expand All @@ -23,24 +23,30 @@ function GUIModule.GetDefaultThemeName(): string
end;

function GUIModule.CreateNewDialogueGui(theme: string?): ScreenGui

-- Check if we have the theme
local ThemeFolder = script.Parent.Themes;
local ThemeFolder = script.Parent.Parent.Themes;
local ThemeName = (theme ~= "" and theme) or GUIModule.GetDefaultThemeName();
local DialogueGui = ThemeName and ThemeFolder:FindFirstChild(ThemeName);
if not DialogueGui then

error("[Dialogue Maker]: There isn't a default theme", 0);

elseif ThemeName == DefaultThemeName and theme and theme ~= DefaultThemeName then

warn("[Dialogue Maker]: Can't find theme \"" .. theme .. "\" in the Themes folder of the DialogueClientScript. Using default theme...");

end

-- Return the theme
return DialogueGui:Clone();

end;

ReplicatedStorage:WaitForChild("DialogueMakerRemoteConnections").ChangeTheme.OnClientInvoke = function(themeName)

script.CurrentTheme.Value = GUIModule.CreateNewDialogueGui(themeName);

end;

return GUIModule;

0 comments on commit 6a03518

Please sign in to comment.