Skip to content

Commit

Permalink
The interpolator it's now using Array instead of Vector<Variant>.
Browse files Browse the repository at this point in the history
This change was needed as the new GDExtension bindings doesn't not support .
  • Loading branch information
AndreaCatania committed May 12, 2022
1 parent 1d80fb5 commit dea1d3d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
22 changes: 11 additions & 11 deletions modules/network_synchronizer/interpolator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void Interpolator::begin_write(uint32_t p_epoch) {
} else {
// Make room.
epochs.push_back(UINT32_MAX);
buffer.push_back(Vector<Variant>());
buffer.push_back(Array());
// Sort the epochs.
for (int i = epochs.size() - 2; i >= int(write_position); i -= 1) {
epochs[uint32_t(i) + 1] = epochs[uint32_t(i)];
Expand All @@ -140,12 +140,12 @@ void Interpolator::begin_write(uint32_t p_epoch) {
// No sort needed.
write_position = epochs.size();
epochs.push_back(p_epoch);
buffer.push_back(Vector<Variant>());
buffer.push_back(Array());
buffer[write_position].resize(variables.size());
}

// Set defaults.
Variant *ptr = buffer[write_position].ptrw();
Variant *ptr = &buffer[write_position][0];
for (uint32_t i = 0; i < variables.size(); i += 1) {
ptr[i] = variables[i].default_value;
}
Expand All @@ -156,17 +156,17 @@ void Interpolator::epoch_insert(int p_var_id, const Variant &p_value) {
ERR_FAIL_INDEX_MSG(p_var_id, int(variables.size()), "The variable_id passed is unknown.");
const uint32_t var_id(p_var_id);
ERR_FAIL_COND_MSG(variables[var_id].default_value.get_type() != p_value.get_type(), "The variable: " + itos(p_var_id) + " expects the variable type: " + Variant::get_type_name(variables[var_id].default_value.get_type()) + ", and not: " + Variant::get_type_name(p_value.get_type()));
buffer[write_position].write[var_id] = p_value;
buffer[write_position][var_id] = p_value;
}

void Interpolator::end_write() {
ERR_FAIL_COND_MSG(write_position == UINT32_MAX, "You can't call this function before starting the epoch with `begin_write`.");
write_position = UINT32_MAX;
}

Vector<Variant> Interpolator::pop_epoch(uint32_t p_epoch, real_t p_fraction) {
ERR_FAIL_COND_V_MSG(init_phase, Vector<Variant>(), "You can't pop data if the interpolator is not fully initialized.");
ERR_FAIL_COND_V_MSG(write_position != UINT32_MAX, Vector<Variant>(), "You can't pop data while writing the epoch");
Array Interpolator::pop_epoch(uint32_t p_epoch, real_t p_fraction) {
ERR_FAIL_COND_V_MSG(init_phase, Array(), "You can't pop data if the interpolator is not fully initialized.");
ERR_FAIL_COND_V_MSG(write_position != UINT32_MAX, Array(), "You can't pop data while writing the epoch");

double epoch = double(p_epoch) + double(p_fraction);

Expand All @@ -182,10 +182,10 @@ Vector<Variant> Interpolator::pop_epoch(uint32_t p_epoch, real_t p_fraction) {
ObjectID cache_object_id;
Object *cache_object = nullptr;

Vector<Variant> data;
Array data;
if (unlikely(position == UINT32_MAX)) {
data.resize(variables.size());
Variant *ptr = data.ptrw();
Variant *ptr = &data[0];
if (buffer.size() == 0) {
// No data found, set all to default.
for (uint32_t i = 0; i < variables.size(); i += 1) {
Expand Down Expand Up @@ -238,7 +238,7 @@ Vector<Variant> Interpolator::pop_epoch(uint32_t p_epoch, real_t p_fraction) {
} else if (unlikely(position == 0)) {
// No old data.
data.resize(variables.size());
Variant *ptr = data.ptrw();
Variant *ptr = &data[0];
for (uint32_t i = 0; i < variables.size(); i += 1) {
switch (variables[i].fallback) {
case FALLBACK_DEFAULT:
Expand Down Expand Up @@ -279,7 +279,7 @@ Vector<Variant> Interpolator::pop_epoch(uint32_t p_epoch, real_t p_fraction) {
} else {
// Enought data to do anything needed.
data.resize(variables.size());
Variant *ptr = data.ptrw();
Variant *ptr = &data[0];
for (uint32_t i = 0; i < variables.size(); i += 1) {
switch (variables[i].fallback) {
case FALLBACK_DEFAULT:
Expand Down
4 changes: 2 additions & 2 deletions modules/network_synchronizer/interpolator.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Interpolator : public Object {
/// Epoch ids, sorted from youngest to oldest.
LocalVector<uint32_t> epochs;
/// Epoch data.
LocalVector<Vector<Variant>> buffer;
LocalVector<Array> buffer;

bool init_phase = true;
uint32_t write_position = UINT32_MAX;
Expand All @@ -89,7 +89,7 @@ class Interpolator : public Object {
void epoch_insert(int p_var_id, const Variant &p_value);
void end_write();

Vector<Variant> pop_epoch(uint32_t p_epoch, real_t p_fraction);
Array pop_epoch(uint32_t p_epoch, real_t p_fraction);
uint32_t get_last_pop_epoch() const; // TODO do I need this? Remove if not.
uint32_t get_youngest_epoch() const;
uint32_t get_oldest_epoch() const;
Expand Down

0 comments on commit dea1d3d

Please sign in to comment.