Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
C++ tweak variable example.
// This file is automatically generated, but you can add your own variables here too.
// Syntax: SET_VAR(variable_name, default, low_limit, upper_limit, slider_exponent)
// In code you can read the variable with: GET_VAR(variable_name)
SET_VAR(tower_height, 520.0f, 0.0f, 3000.0f, 1.0f)
SET_VAR(KSpring, 520.0f, 1.0f, 3000.0f, 1.0f)
SET_VAR(WireRadius, 2.0f, 0.1f, 34.0f, 1.0f)
SET_VAR(volcanic_sun_x, 0.088000, -20.0f, 20.0f, 1.0f)
SET_VAR(mothership_sun_x, 2.026000, -5.0f, 5.0f, 1.0f)
SET_VAR(mothership_sun_y, 0.507000, -5.0f, 5.0f, 1.0f)
SET_VAR(wire_fog_exp, 0.0f, 0.0f, 100.0f, 1.0f)
SET_VAR(FADE, 0.0f, 0.0f, 1.0f, 1.0f)
SET_VAR(ship_tr_x, 57851.214844, -200000.0f, 400000.0f, 1.0f)
SET_VAR(ship_tr_y, 18595.037109, -50000.0f, 90000.0f, 1.0f)
SET_VAR(ship_tr_z, 6611.561035, -200000.0f, 400000.0f, 1.0f)
SET_VAR(ship_sc_x, 32892.554688, 0.0f, 40000.000000, 1.0f)
SET_VAR(ship_sc_y, 14710.742188, 0.0f, 40000.000000, 1.0f)
SET_VAR(ship_sc_z, 36619.710938, 0.0, 80000.000000, 1.0f)
SET_VAR(ship_sdf_scale, 3801.652100, 0.0, 5000.0f, 1.0f)
SET_VAR(ship_rot_x, 0.0, -180.0f, 180.0f, 1.0f)
SET_VAR(ship_rot_y, 102.122002, -180.0f, 180.0f, 1.0f)
SET_VAR(ship_rot_z, 0.0, -180.0f, 180.0f, 1.0f)
SET_VAR(bomb_x, 30555.492188, -10080.0f, 80080.0f, 1.0f)
SET_VAR(bomb_y, 77540.273438, -10080.0f, 80080.0f, 1.0f)
SET_VAR(bomb_z, 51416.414063, -10080.0f, 80080.0f, 1.0f)
SET_VAR(bomb_sep, 71.752998, 0.0f, 80.0f, 1.0f)
SET_VAR(bomb_rot, 99.668999, -180.0f, 180.0f, 1.0f)
SET_VAR(bomb_dir_x, -2.893000, -10.0f, 10.0f, 1.0f)
SET_VAR(bomb_dir_y, 0.000000, -10.0f, 10.0f, 1.0f)
SET_VAR(bomb_dir_z, 0.0, -10.0f, 10.0f, 1.0f)
#pragma once
#include "tweaks.h"
#ifdef DEBUG
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include "util.h"
std::map<std::string, float> tweakvars;
std::map<std::string, std::tuple<float,float,float>> tweaklimits;
#define SET_VAR(var, value, lo, hi, exp) tweakvars[#var] = value; tweaklimits[#var] = std::make_tuple(lo, hi, exp);
#define SET_VAR_UNIT(var, value) SET_VAR(var, value, 0.0f, 1.0f, 1.0f);
void tweaks_init() {
#include ""
void tweaks_save(const char* path)
using std::vector;
using std::string;
vector<string> lines;
std::ifstream infile(path);
string line;
while (std::getline(infile, line))
using std::get;
FILE* fp = fopen(path, "w");
for (auto& s : lines) {
int text_start = 0;
for (int i = 0; i < s.length(); i++) {
if (!iswspace(s[i])) {
text_start = i;
if (s[text_start] == '/') {
// output comments verbatim
fputs(s.c_str(), fp);
fputs("\n", fp);
char name[200];
float val, low, hi, exp;
// Try to read version without whitespace if initial attempt fails.
//if (5 != sscanf(&s[text_start], "SET_VAR(%[^','], %f, %f, %f, %f)", name, &val, &low, &hi, &exp)) {
// sscanf(&s[text_start], "SET_VAR(%[^','],%f,%f,%f,%f)", name, &val, &low, &hi, &exp);
if (2 != sscanf(&s[text_start], "SET_VAR(%[^','], %f", name, &val)) {
sscanf(&s[text_start], "SET_VAR(%[^','], %f", name, &val);
// HACK: We
char orig[5][200];
sscanf(&s[text_start], "SET_VAR(%[^','],%[^','],%[^','],%[^','],%[^','')'])", &orig[0], &orig[1], &orig[2], &orig[3], &orig[4]);
float val2 = tweakvars[name];
auto& lim = tweaklimits[name];
if (abs(val - val2) > 1e-6) {
sprintf(orig[1], " %f", val2);
fprintf(fp, "SET_VAR(%s,%s,%s,%s,%s)\n", orig[0], orig[1], orig[2], orig[3], orig[4]);
#define SET_VAR(var, value, lo, hi, exp) float tweak_var_ ## var = value;
#include ""
void tweaks_init() {}
#pragma once
void tweaks_init();
#ifdef DEBUG
#include <map>
#include <string>
#include <tuple>
extern std::map<std::string, float> tweakvars;
extern std::map<std::string, std::tuple<float,float,float>> tweaklimits;
void tweaks_save(const char* path);
// Use GET_VAR to access the tweak variable.
#define GET_VAR(var) (tweakvars[#var])
#define GET_VAR(var) (tweak_var_ ## var)
#define SET_VAR(var, value, lo, hi, exp) extern float tweak_var_ ## var;
#include ""
#undef SET_VAR
// this is actually pretty awful because you need to always use the GET_VAR macro
// but it's needed if we want to use a std::map (tweakvars in tweaks.h) to access them
vec3 bombDir = vec3(GET_VAR(bomb_dir_x), GET_VAR(bomb_dir_y), GET_VAR(bomb_dir_z));
// the editor GUI generated with imgui
using std::get;
Begin("Tweaks", &show_tweaks_window);
if (Button("Save")) {
for (auto& t : tweakvars) {
const string& var = t.first;
auto& limits = tweaklimits[var];
SliderFloat(var.c_str(), &tweakvars[var], get<0>(limits), get<1>(limits), "%.3f", get<2>(limits));
//DragFloat(var.c_str(), &tweakvars[var], 1.0f, get<0>(limits), get<1>(limits), "%.3f", get<2>(limits));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment