Skip to content

Instantly share code, notes, and snippets.

View germandiagogomez's full-sized avatar

Germán Diago germandiagogomez

  • Spain
View GitHub Profile
class GameView {
var playerNumber
var cppView
def GameView(playerNumber) {
// Uses copy constructor
playerNumber = playerNumber
// does not use copy constructor
cppView := createGameView("MatchView")
}
@germandiagogomez
germandiagogomez / try_catch.chai
Created May 24, 2021 22:37
Exceptions in ChaiScript
try {
myFunc(2);
}
catch (e) : type(e, "int") {
}
@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 / 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 / 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>());