简体   繁体   中英

Equivalent of JavaScript's splice in Java

I'm trying to port some Javascript code into Java and I've reached a section where I can't seem to port the code without all sorts of errors. There are no actual exceptions thrown it just doesn't work as it should. Basically this code is part of a networking snippet that attempts to reconcile with the server when it receives a new packet because it uses client-side prediction to keep moving the player even when there's no packets to be applied.

I understand the concept but I just can't seem to put it into code. The section of code uses the splice function on an array to remove elements so I thought it'd be easy to port. I'll post the code segment of JS below along with the code segment in Java that gives me problems and tell me what I'm doing wrong. I'm pretty sure I also ported the loop wrong.

JavaScript:

var j = 0;
while (j < this.pending_inputs.length) {
  var input = this.pending_inputs[j];
  if (input.input_sequence_number <= state.last_processed_input) {
    // Already processed. Its effect is already taken into account
    // into the world update we just got, so we can drop it.
    this.pending_inputs.splice(j, 1);
  } else {
    // Not processed by the server yet. Re-apply it.
    this.entity.applyInput(input);
    j++;
  }
}

Java:

for (int i = 0; i < pendingInputs.size(); i++) {
    if (i <= lastProcCmd) {
        // Already proceesed command, remove it from pendingInputs
        for (int j = 1; j < pendingInputs.size(); j++) {
            pendingInputs.remove(j);
        }
    } else {
        applyCmd(pendingInputs.get(i));
    }
}

EDIT So I changed the code to this:

// Server reconciliation
int j =0;
while (j < pendingInputs.size()) {
    String cmd = pendingInputs.get(j);
    if (pendingInputs.indexOf(cmd) <= lastProcCmd) {
        pendingInputs.remove(j);
    } else {
        applyCmd(cmd);
        j++;
    }
}

And I still have a problem so I'm thinking it's elsewhere in the code. This is multiplayer code using client-side prediction and server reconciliation if that helps using these articles: Articles

Pending inputs is an ArrayList of String s that represent commands such as, "Left" or, "Right." The other problem is that my network listener is on another thread even though I use sychronization blocks to prevent any ConcurrentModificationException s from happening in important places. His code was hard to port as JS to Java is something I'm not familiar with.

Untested, but looks close:

for (Iterator<String> iter = pendingInputs.iterator(); iter.hasNext(); ) {
  String cmd = iter.next();
  if(pendingInputs.indexOf(cmd) <= lastProcCmd){
    iter.remove();
  }else{
    applyCmd(cmd);
  }
}

Some things to note:

  • Might be wise to create classes for your commands and use polymorphism to run them
  • Figuring out if a command has been processed via its position in a list is error prone. If commands were pojos you could set a "has been processed" flag them remove based on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM