Adding temporarily checkpoints and how to remove them?

Started by Ridwan Rz, Aug 04, 2023, 03:22 AM

Previous topic - Next topic

Ridwan Rz

Hey there, The idea was to add checkpoints temporarily and removing them if the placement is not correct, so that I can understand where I'm putting while I'm mapping.

So far I'm using this:
else if ( cmd == "addcp")
{
  local PosX      = player.Pos.x;
  local PosY      = player.Pos.y;
  local PosZ      = player.Pos.z;
  local newline;
   
  CreateCheckpoint(null, 1, true, Vector(PosX.tofloat() , PosY.tofloat() , PosZ.tofloat()), ARGB(255,255,255,255), 2);
  MessagePlayer("[#FFFFFF]Check point has been created.!", player);
  MessagePlayer("CreateCheckpoint(null, 1, true, Vector("+ PosX.tofloat() +", " + PosY.tofloat() + ", "+ PosZ.tofloat() +", ARGB(255,255,255,255), 2);", player);
  print("CHECKPOINT: " + PosX + ", " + PosY + ", " + PosZ);
       
  newline = "CreateCheckpoint(null, 1, true, Vector("+ PosX.tofloat() +", " + PosY.tofloat() + ", "+ PosZ.tofloat() +", ARGB(255,255,255,255), 2); \n";

   local f = file("savedcp.txt","a+");
   foreach (c in newline)
   f.writen(c, 'b');
   f.close();
}
else if ( cmd == "delcp")
{
  if (text)
  {
    local cp = text;
    cp.Remove()
    MessagePlayer("[#FFFFFF]Check point has been removed!", player);
  }
  else
  {
    MessagePlayer("[#FFFFFF]write something to remove cp!", player);
  }
}


So the thing is I copy the CreateCheckpoint whole created function from /addcp and paste it on /delcp (pasting the checkpoint).

So I'm not sure I'm not doing it correctly. I know I'm not that good at scripting but trying to learn.

I can add perfectly but it's not deleting anything using /delcp.

The ERROR on console:
AN ERROR HAS OCCURED [the index 'Remove' does not exist]
CALLSTACK
*FUNCTION [onCommand()] scripts/_MapperSystem.nut line [566]
*FUNCTION [onPlayerCommand()] scripts/main.nut line [1961]
LOCALS
[cp] "CreateCheckpoint(null, 1, true, Vector(-1730.41, -154.432, 14.9084, ARGB(255,255,255,255), 2);"
[text] "CreateCheckpoint(null, 1, true, Vector(-1730.41, -154.432, 14.9084, ARGB(255,255,255,255), 2);"
[cmd] "delcp"
[player] INSTANCE
[this] TABLE
[text] "CreateCheckpoint(null, 1, true, Vector(-1730.41, -154.432, 14.9084, ARGB(255,255,255,255), 2);"
[cmd] "delcp"
[player] INSTANCE
[this] TABLE

Yuriitwo

The error occurred because the variable 'cp' is directly receiving the text of the checkpoint (the command 'local cp = text;' is assigning 'text' directly to the variable 'cp'). However, to remove a checkpoint, you need the checkpoint object that was created earlier, not the string with the representation of the 'CreateCheckpoint' command.

The corrected command should use the 'FindCheckpoint' function to find the checkpoint object corresponding to the specified ID. Here is the corrected command:
else if (cmd == "delcp") {
    if (text) {
        local cpID = text.tointeger();
        local cp = FindCheckpoint(cpID);

        if (cp) {
            cp.Remove();
            MessagePlayer("[#FFFFFF]Checkpoint has been removed!", player);
        } else {
            MessagePlayer(format("[#FFFFFF]Checkpoint with ID %d not found!", cpID), player);
        }
    } else {
        MessagePlayer("[#FFFFFF]Write the checkpoint ID to remove it!", player);
    }
}

With this correction, the code will search for the correct checkpoint using the 'FindCheckpoint' function, and if the checkpoint is found (i.e., if the specified ID corresponds to a valid checkpoint), it will be removed using the 'cp.Remove()' function. Otherwise, a message indicating that the checkpoint was not found will be sent to the player.



Please also replace these two lines in the /addcp command script:
CreateCheckpoint(null, 1, true, Vector(PosX.tofloat() , PosY.tofloat() , PosZ.tofloat()), ARGB(255,255,255,255), 2);
MessagePlayer("[#FFFFFF]Check point has been created.!", player);
MessagePlayer("CreateCheckpoint(null, 1, true, Vector("+ PosX.tofloat() +", " + PosY.tofloat() + ", "+ PosZ.tofloat() +", ARGB(255,255,255,255), 2);", player);
print("CHECKPOINT: " + PosX + ", " + PosY + ", " + PosZ);

With these two lines:
CPMapping <- CreateCheckpoint(null, 1, true, Vector(PosX.tofloat() , PosY.tofloat() , PosZ.tofloat()), ARGB(255,255,255,255), 2);
MessagePlayer("[#FFFFFF]Check point has been created, ID: [#f0d1d1]" + CPMapping.ID + "[#FFFFFF].", player);
MessagePlayer("CreateCheckpoint(null, 1, true, Vector("+ PosX.tofloat() +", " + PosY.tofloat() + ", "+ PosZ.tofloat() +", ARGB(255,255,255,255), 2);", player);
print("ID: " + CPMapping.ID + " CHECKPOINT: " + PosX + ", " + PosY + ", " + PosZ);

Now, with the corrected lines, the checkpoint creation message will include the ID of the newly created checkpoint.
Fiesta in Malibu Developer


Ridwan Rz

Quote from: Yuriitwo on Aug 04, 2023, 10:09 AMThe error occurred because the variable 'cp' is directly receiving the text of the checkpoint (the command 'local cp = text;' is assigning 'text' directly to the variable 'cp'). However, to remove a checkpoint, you need the checkpoint object that was created earlier, not the string with the representation of the 'CreateCheckpoint' command.


Thank you @Yuriitwo That is a perfect explanation for me to understand what I did incorrectly and wrong. Thank you again.

2b2ttianxiu