Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
state.hpp
Go to the documentation of this file.
1
4#pragma once
5#include <aegis2d/script/export.hpp>
6
7#include <cstddef>
8#include <cstdint>
9#include <filesystem>
10#include <memory>
11#include <optional>
12#include <string>
13#include <string_view>
14
15namespace aegis2d::script {
16
20 std::string chunk;
22 int line = 0;
24 std::string message;
25};
26
27struct ScriptAccess;
28
32[[nodiscard]] AEGIS2D_SCRIPT_EXPORT std::string to_string(const ScriptError& error);
33
52class AEGIS2D_SCRIPT_EXPORT ScriptState {
53public:
55 static constexpr std::uint32_t default_instruction_limit = 10'000'000;
57 static constexpr std::size_t default_memory_limit = std::size_t{64} * 1024 * 1024;
58
61 ScriptState(const ScriptState&) = delete;
64 ScriptState(ScriptState&& other) noexcept;
67 ScriptState& operator=(ScriptState&& other) noexcept;
70
75 bool run(std::string_view code, std::string_view chunk = "script");
76
80 bool run_file(const std::filesystem::path& file);
81
85 bool call(std::string_view function);
86
91 bool call(std::string_view function, double argument);
92
96 [[nodiscard]] bool has(std::string_view name) const;
97
101 [[nodiscard]] std::optional<double> number(std::string_view name) const;
102
106 [[nodiscard]] std::optional<std::string> text(std::string_view name) const;
107
111 [[nodiscard]] std::optional<bool> boolean(std::string_view name) const;
112
117 void set_instruction_limit(std::uint32_t instructions) noexcept;
118
121 [[nodiscard]] std::uint32_t instruction_limit() const noexcept;
122
126 void set_memory_limit(std::size_t bytes) noexcept;
127
130 [[nodiscard]] std::size_t memory_limit() const noexcept;
131
134 [[nodiscard]] const std::optional<ScriptError>& last_error() const noexcept;
135
137 void clear_error() noexcept;
138
139private:
140 friend struct ScriptAccess;
141 struct Impl;
142 std::unique_ptr<Impl> impl_;
143};
144
145} // namespace aegis2d::script
A Lua state for the game's scripts, sandboxed.
Definition state.hpp:52
bool call(std::string_view function)
Calls a global function without arguments.
~ScriptState()
Closes the state.
ScriptState(const ScriptState &)=delete
void set_instruction_limit(std::uint32_t instructions) noexcept
Sets the instructions a run or a call may take.
std::optional< double > number(std::string_view name) const
A global number.
ScriptState & operator=(ScriptState &&other) noexcept
Takes over the state of other, left without one.
bool run_file(const std::filesystem::path &file)
Runs a Lua file.
std::optional< bool > boolean(std::string_view name) const
A global boolean.
ScriptState()
Opens a sandboxed state.
std::optional< std::string > text(std::string_view name) const
A global string.
std::uint32_t instruction_limit() const noexcept
Instructions a run or a call may take.
bool run(std::string_view code, std::string_view chunk="script")
Runs Lua code.
ScriptState(ScriptState &&other) noexcept
Takes over the state of other, left without one.
bool call(std::string_view function, double argument)
Calls a global function with a number, such as the seconds of a tick.
ScriptState & operator=(const ScriptState &)=delete
bool has(std::string_view name) const
Tells whether a global is set (not nil).
Entity behaviors in Lua, so that new enemies and patterns need no recompilation.
Definition components.hpp:19
AEGIS2D_SCRIPT_EXPORT std::string to_string(const ScriptError &error)
Formats an error as a log line shows it.
Definition endpoint.hpp:85
Where and why a script failed.
Definition state.hpp:18
std::string message
What went wrong, without the chunk and the line.
Definition state.hpp:24
int line
Line of the error in the chunk, or 0 when Lua does not tell it.
Definition state.hpp:22
std::string chunk
Name of the chunk: the file, or the name given to ScriptState::run().
Definition state.hpp:20