Open2

Siv3D Script 機能のサンプル

Ryo SuzukiRyo Suzuki

関数のロード

# include <Siv3D.hpp> // Siv3D v0.6.14

void Main()
{
	Script script{ U"example/script/test.as" };

	if ((not script) || (not script.compiled()))
	{
		throw Error{ U"Falied to load script" };
	}

	const auto Add = script.getFunction<int32(int32, int32)>(U"Add");
	const auto PrintMessage = script.getFunction<void()>(U"PrintMessage");
	const auto DrawShapes = script.getFunction<void()>(U"DrawShapes");

	if ((not Add) || (not PrintMessage) || (not DrawShapes))
	{
		throw Error{ U"Failed to get function" };
	}

	Print << Add(1, 2);

	PrintMessage();

	while (System::Update())
	{
		DrawShapes();
	}
}
Ryo SuzukiRyo Suzuki

リロード

test2.as
void DrawShapes()
{
	Scene::SetBackground(ColorF(0.8, 0.9, 1.0));

	Rect(Arg::center(200, 300), 160).draw(ColorF(0.6, 0.8, 0.0));

	Circle(Scene::Center(), 80).draw(ColorF(0.0, 0.6, 0.8));

	Shape2D::Cross(100, 20, Vec2(600, 300)).draw(ColorF(0.8, 0.6, 0.6));
}
# include <Siv3D.hpp> // Siv3D v0.6.14

void Main()
{
	Script script{ U"test2.as" };

	if ((not script) || (not script.compiled()))
	{
		throw Error{ U"Falied to load script" };
	}

	auto DrawShapes = script.getFunction<void()>(U"DrawShapes");

	while (System::Update())
	{
		if (SimpleGUI::Button(U"Reload", Vec2{ 600, 40 }))
		{
			if (not script.reload())
			{
				Print << U"Failed to reload script";
			}

			DrawShapes = script.getFunction<void()>(U"DrawShapes");
		}

		DrawShapes();
	}
}