How to change player spawn point in middle of a level?

hechelion

Member
Short version:
How to change player spawn point in middle of level (after the player dies.)?
I think is with "respawn#.c" but I don't know the code to change the spawn point for player.

Long version:
I have this level and I has limit the walking area with walls


If the player dies when the screen are in position B (bottom right), the player respawn in the upper position of the level, over the wall. And I need is the player respawn over the path.
 
You can either use a antiwall code in the respawn animation (or in respawn1.c), to make the player move away from the wall,
or,
Create a code for this specific level, where you will get the camera position to be able to change the spawn position.

I will have a stage kinda like that in my mod, but I haven't got that far yet.
 
Thank for the reply @O ilusionista.

I searched information for antiwall code, but I prefer used  (Or try first) the respawn#.c but after a lot of try and error, I can not get it to work.

I reduced the script to most basic, but I can not make the player change the position when respawn.
any suggestion is welcome

Code:
void main()
{
	void self = getlocalvar("self");
	int x = getentityproperty(self, "x");
	
	changeentityproperty(self, "position", x, 340);	
	
	/*if(openborvariant("current_stage")==203){
		if(openborvariant("xpos")>=1540){
			changeentityproperty(self, "position", x, 340);
		}
	}*/
}
 
Usually in this kind of stage, you can push player's respawn point with walls (the ones on the back). This is simple and effective but will put players near those walls

I have script system to control where players would spawn. I could share it if you want :)
 
Don't know if this is the problem but you should have a third parameter with position.  The wait command may also stop you from spawning further back in the stage.

"position" - Follow by x, z, a, do a nice warp.
 
@msmalik681 Thanks for your answer, I tried your suggestion But it does not work.

After a lot of hours of trial and error, I discovered this line don't return the player handler (at lest in respawn#.c)
Code:
void self = getlocalvar("self");

The solution, get the player handler with this other function and now, the rest of the code work great.
Code:
void self = getplayerproperty(0,"ent");

 
hechelion said:
The solution, get the player handler with this other function and now, the rest of the code work great.
Code:
void self = getplayerproperty(0,"ent");

That's because respawn.c is a player event, not an entity event. But your script will break when other players are active, because you've hard coded the script to target player 1 no matter which player is actually respawning. You should accommodate all players by getting the player index first instead, like this:

Code:
int player_index; // Player index.
void entity; // Entity.

// Get player that triggered event.
player_index = getlocalvar("player");

// Get the player's entity.
entity = getplayerproperty(player_index, "ent");
 
Thank for the explanation Damon Caskey and thank for the code.

but one question:
is it correct to assume that respawn.c trigger for all player but respawn1.c only for player 1? or respawn1.c can be trigger for other player too?
 
You are correct. Respawn<player>.c only fires on <player>. You still need to get the player index so that whatever it does targets the appropriate player entity.

DC
 
hechelion, sorry for the wait.
Here's respawn1.c to control player 1's respawn coords:
Code:
void main(){
    void P = getplayerproperty(0, "entity");
    int x = getindexedvar(0); //Get respawn x coordinate
    int z = getindexedvar(1); //Get respawn z coordinate
    int a = getindexedvar(2); //Get respawn a coordinate
    int Flag = getindexedvar(3); //Get respawn flag
    int XPos = openborvariant("xpos");
    int Sx;

    if(a == NULL()){
      a = 0;
    }

    if(x && z){
      if(Flag){
        Sx = x;
      } else {
        Sx = XPos+x;
      }

      changeentityproperty(P, "position", Sx, z, a);
    }
}

The script takes respawn coords defined in indexed variables 0,1,2 for x, z and y respectively
To define those, you can do it directly on level text like this:

Code:
spawn	xxxx
@script
void main()
{
  setindexedvar(0, 100); // respawn x
  setindexedvar(1, 210); // respawn z
  setindexedvar(2, 302); // respawn a
}
@end_script
coords	450 200
at	120

This script is entity independent so it can be declared on any entity, just make sure it's declared on correct scroll position. Ah yes, the most important part is the scroll position, in above example the respawn coords will be applied after the entity in example is spawned at 120
You can declare the script on a blank entity if you don't have any entities to declare on
You can declare another of this script to change previous respawn coords

Indexed variable 3 is for defining respawning type. Setting the variable to 1 will set strict respawn coords i.e player 1 will be respawned at defined coords regardless of current scroll position.

This is for player 1. For player 2,3 and 4, you might need to make similar respawn script using different set of indexed variables

I know that's long explanation but there's another important thing. Indexed variables are persistent and won't be cleared on its own therefore you'd need to created endlevel.c to clear values in indexed variables. Here's an example:
endlevel.c
Code:
void main()
{
  setindexedvar(0, NULL());
  setindexedvar(1, NULL());
  setindexedvar(2, NULL());
  setindexedvar(3, NULL());
}

Above system uses 4 indexed variables for one player. I have an idea on how to reduce number of used indexed variables, let me try it myself :D

BTW this thread should be in help section
 
Use globalvar not indexedvar !

Indexdvars

setindexedvar(<key>, <value>)
getindexedvar(<key>)

Indexed vars are depreciated as of OpenBOR version 3.902. They are now simply wrappers for the globalvar() functions and should not be used.
 
Back
Top Bottom