Skip to content

Instantly share code, notes, and snippets.

View germandiagogomez's full-sized avatar

Germán Diago germandiagogomez

  • Spain
View GitHub Profile
@germandiagogomez
germandiagogomez / CppToChaiScriptBindings.cpp
Created May 24, 2021 22:20
Exposing C++ code to ChaiScript
// C++ code
ChaiScript chai;
chai.add(user_type<IBasicGameView>(), "IBasicGameView").
add(user_type<IGameMatchView>(), "IGameMatchView").
add(user_type<IGameMatchView::Player>(), "IGameMatch_Player").
add(user_type<OnlineRoomsView::MatchRoom>(), "OnlineRoomsView_MatchRoom").
add(user_type<Guinyote::RuntimeConfig>(), "RuntimeConfig");
// Support inheritance:
chai.add(base_class<IBasicGameView, IGameMatchView>());
chai.add(base_class<IGameMatchView, GameMatchView>());
@germandiagogomez
germandiagogomez / freeFunctions.chai
Created May 24, 2021 22:33
Free functions in ChaiScript
def nextPlayer(player) {
return ((player + 1) % 4)
}
// typed (optional)
def nextPlayer2(int player) {
return ((player + 1) % 4)
}
def getPlayerPositionRelativeTo(refPlayer, targetPlayer) {
if (refPlayer == targetPlayer) {
@germandiagogomez
germandiagogomez / functionGuards.chai
Created May 24, 2021 22:35
Functions with guards in ChaiScript
def maybeCallMe(x) : x > 2 && x < 5 {
}
def maybeCallMe(x) : x >= 5 {
}
maybeCallMe(3) // Calls the top one
maybeCallMe(6) // Calls the second one
@germandiagogomez
germandiagogomez / try_catch.chai
Created May 24, 2021 22:37
Exceptions in ChaiScript
try {
myFunc(2);
}
catch (e) : type(e, "int") {
}
class GameView {
var playerNumber
var cppView
def GameView(playerNumber) {
// Uses copy constructor
playerNumber = playerNumber
// does not use copy constructor
cppView := createGameView("MatchView")
}
@germandiagogomez
germandiagogomez / DynamicJson.chai
Last active May 25, 2021 10:17
Dynamic Json object
class JsonObject {
var contents
def JsonObject(string jsonStr) {
this.contents = from_json(jsonStr)
}
def JsonObject(Map contents) {
this.contents = contents
}
@germandiagogomez
germandiagogomez / StdFunctionParameter.chai
Created May 24, 2021 22:41
Function Parameters can take callback from ChaiScript
// C++ side
// void f(std::function<void (MyRegisteredClass)> callback);
...
chai.add(fun(f), "f");
//chaiscript side
f([](myObject) {
})
@germandiagogomez
germandiagogomez / ClassInWren.wren
Last active May 25, 2021 10:19
Classes in Wren
class Counter {
construct new(withValue) {
_currentValue = withValue
}
increment() {
_currentValue = _currentValue + 1
}
value { _currentValue }
}
@germandiagogomez
germandiagogomez / MenuScreen.wren
Last active May 25, 2021 10:13
Menu Screen from game scripted
import "environment" for Env
import "guinyoteview" for IBasicGameView, TrySelectEvent
import "inputsystem" for Input
import "guinyoteutils" for MouseClickedEvent, MouseReleasedEvent
class InputSystem {
construct new(inputSystem) {
_input = inputSystem
}
@germandiagogomez
germandiagogomez / ExposeCppStateInWren.cpp
Last active May 24, 2021 22:51
Workaround to expose C++ instances in Wren with Wrenbind17
// 1. Create a struct to be able to expose some fields
struct ScriptEnv {
std::shared_ptr<spdlog::logger> gameLog;
Guinyote::View::VideoManager * videoManager;
RuntimeConfig const * config;
std::string orsAddress;
};
// Create a C++ instance to pass the data to wren
ScriptEnv scriptEnv{
.gameLog = logger_,