Hello World plugin

Started by Kara Jo Kalinowski, Tue 18/02/2025 03:09:44

Previous topic - Next topic

Crimson Wizard

There's no such thing as "const char*" in AGS script. You need to use String type, which is a string class.

In plugin you will have to call IAGSEngine.CreateScriptString in order to create the string object that you can return.

Example:
Code: cpp
"import String HelloWorld();\r\n"

Code: cpp
const char *HelloWorld() {
    return engine->CreateScriptString("Hello World");
}

Snarky

#21
You could take a look at the Clipboard plugin, which also has to do the conversion to and from AGS strings. (Though note that it has not been updated to support Unicode strings.)

https://github.com/messengerbag/Clipboard

The important bits are:

Code: C
const char *ourScriptHeader =
"#define CLIPBOARD_PLUGIN\r\n"
"#define CLIPBOARD_PLUGIN_VERSION 0.04\r\n"
"/// Methods to access the Windows clipboard (via AGS Clipboard Plugin)\r\n"
"builtin struct Clipboard {\r\n"
"  /// Paste a String from the clipboard. Returns null if not available\r\n"
"  import static String PasteText ();\r\n"
"  /// Copy a String to the clipboard. Returns true if successful\r\n"
"  import static bool CopyText (String copyString);\r\n"
"};\r\n";

const char* Clipboard_PasteText(void) {
	HGLOBAL   hglb;
	LPTSTR    lptstr;
	const char* pasteString = nullptr;

	if (!IsClipboardFormatAvailable(CF_TEXT))
		return nullptr;
	if (!OpenClipboard(engine->GetWindowHandle()))
		return nullptr;

	hglb = GetClipboardData(CF_TEXT);
	if (hglb != nullptr)
	{
		lptstr = reinterpret_cast<LPTSTR>(GlobalLock(hglb));
		if (lptstr != nullptr)
		{
			pasteString = engine->CreateScriptString(lptstr);
			GlobalUnlock(hglb);
		}
	}
	CloseClipboard();

	return pasteString;
}

void AGS_EngineStartup(IAGSEngine *lpEngine) {
	engine = lpEngine;

	// Make sure it's got the version with the features we need
	if (engine->version < 3) {
		engine->AbortGame("Engine interface is too old, need newer version of AGS.");
	}

	engine->RegisterScriptFunction("Clipboard::PasteText", Clipboard_PasteText);
	engine->RegisterScriptFunction("Clipboard::CopyText^1", Clipboard_CopyText);
}

This adds this header to AGS, and hooks up the methods (I've omitted Clipboard.CopyText) to the corresponding plugin functions:

Code: ags
#define CLIPBOARD_PLUGIN
#define CLIPBOARD_PLUGIN_VERSION 0.04
/// Methods to access the Windows clipboard (via AGS Clipboard Plugin)
builtin struct Clipboard {
  /// Paste a String from the clipboard. Returns null if not available
  import static String PasteText ();
  /// Copy a String to the clipboard. Returns true if successful
  import static bool CopyText (String copyString);
};

SMF spam blocked by CleanTalk