Level Spawn Shifting Trick

Bloodbane

Well-known member
I have a trick to share here, I use this oftenly when placing obstacles in levels.

Level Spawn Shifting Trick

Intro: Entities could be spawned in levels anywhere as the modder preferred. However some entities such as decoration and obstacles must be spawned in specific coordinates and scrollpos to match background or to prevent them from being spawned at wrong place when the scrollpos is reached.
This trick will show how to shift spawn coordinate and scrollpos without moving those-to-be-spawned entities from their correct place
Procedure: The trick is different in direction left and right levels.

A. Direction Right
Say, a painting decoration is correctly spawned with this:
spawn Painting
coords 160 145
at 2000

If this is run in game, Painting will pop in out of nowhere when scrollpos 2000 is reached since it is spawned onscreen. That is not good (well unless the modder doesn't care :) ). To prevent that, Painting must be spawned earlier however shifting scrollpos is not enough, coords must be adjusted as well.
To shift it properly, 1st decide the proper x coords for Painting so it is spawned offscreen.
Let's say, 60 pixels offscreen is minimum distance from right screen edge. So in 320x240 resolution, Painting must be spawned with x coords = 320+60 = 380

2nd, calculate the new scrollpos with this logic: Sum of scrollpos and x is same before and after shifting.
Old scrollpos + old x = new x + new scrollpos
2000 + 160 = 380 + new scrollpos
new scrollpos = 2160 - 380 = 1780

So, the new spawn should be like this:
spawn Painting
coords 380 145
at 1780

With this, Painting will be spawned earlier and it will retain it's correct position

B. Direction left
Let's say, a table is correctly spawned with this:
spawn Table
coords 200 200
at 1400

In game, at scrollpos 1400, Table will pop in around middle of playing field and it might accidentally block moving player or enemy nearby IOW not good!.
To shift it, 1st decide proper x coords for Table so it's spawned offscreen. Let's say, it should be at least 40 pixels from left screen edge. So Table must be spawned with x coords = -40 (resolution doesn't matter)

2nd, calculate the new scrollpos with this logic: Shifting distance of x is same with shifting distance of scrollpos
new x - old x = new scrollpos - Old scrollpos
-40 - 200 = new scrollpos - 1400
new scrollpos = 1400 - 240 = 1160

So, the new spawn should be like this:
spawn Table
coords -40 200
at 1160

Table will be spawned earlier and will be at same place as designed before

Extra: Both examples show how to shift decoration and obstacle spawn offscreen. Actually, this trick can also be used to set up scrollpos-controlled enemy generator properly in game. However, that won't be described here :).
 
I was wondering if there is any way to fix the z axis also...
I got my level with z range wider than screen, but because the spawn is relative to the screen, the z axis can be totally off...
 
On the contrary, z coords is not affected by scroll position at all. So no matter which scrollpos you spawn the entity, z coords is same
 
Dear, thank you for the topic, you have pointed out a possibility so that I can solve a problem.

Can you help me?

How can I correct the character generator by changing the resolution of the game from 320x240 to 480x272?
 
Bloodbane isnt that obvious and logical? you have to spawn offscreen otherwise your entity just appears in the middle of the screen.
I just spawn my decorations at 0 and give offscreenkill 999999gazillion so they  wont disappear.
I had issues with direction both and using scrollx command, sometimes when screen locks so you fight with enemies then you unlock the screen and your new enemies will spawn in a bit different positions depending on how far you were when killing last enemy, if you was on far right then it will spawn another entity differently than when you killed last enemy on far left side of the screen.
scrollx is still a bit confusing for me , it works but i have some issues with it when it spawns stuff moved too far depending when it "unlocks" screen to move forward.
 
bWWd said:
Bloodbane isnt that obvious and logical? you have to spawn offscreen otherwise your entity just appears in the middle of the screen.
I just spawn my decorations at 0 and give offscreenkill 999999gazillion so they  wont disappear.

I think you're missing my whole point of this tutorial and it's not referring offscreenkill at all
Besides, some decorations can be spawned onscreen such as door opening animation

scrollx is still a bit confusing for me , it works but i have some issues with it when it spawns stuff moved too far depending when it "unlocks" screen to move forward.

What you need is not just this trick but a simple script like this:

Code:
spawn	enemy
@script
void main()
{
    void self = getlocalvar("self");
    changeentityproperty(self, "position", 120);
}
@end_script
health	100
flip	1
coords	-200 350 1
at	0

This script will preserve enemy's x spawn coords when it is spawned i.e x = -80 (120-200). No matter when or where the camera or scroll position is, this enemy will start at x = -80 when spawned

How can I correct the character generator by changing the resolution of the game from 320x240 to 480x272?

I'm sorry egeu, what do you mean by character generator?
BTW this trick isn't affected by game resolution at all :)
 
Well thats exactly what i was writing about, i want to actually spawn and preserve position ACCORDING to where camera is , its important because when camera is far left and unlocks then entity is spawned differently than with camera on far right when you use scrollx.So there must be something wrong with my coding stages and scrollx or problem with scrollx itself.
When you use simple wait command it does not happen because screen just stops and wont move , but with scrollx when screen unlocks after killing enemies then spawn positions are messed up and shifted if it unlocked when you were on far left.Did You ever had this issue with scrollx ?
 
bWWd said:
Well thats exactly what i was writing about, i want to actually spawn and preserve position ACCORDING to where camera is , its important because when camera is far left and unlocks then entity is spawned differently than with camera on far right when you use scrollx.So there must be something wrong with my coding stages and scrollx or problem with scrollx itself.
When you use simple wait command it does not happen because screen just stops and wont move , but with scrollx when screen unlocks after killing enemies then spawn positions are messed up and shifted if it unlocked when you were on far left.Did You ever had this issue with scrollx ?

Ah, now i see what you mean. I had to deal with this as well on my DD game. It was driving me crazy until i found what the trick was. This is happening on direction both levels but is an easy fix once you know what is going on.
On direction both levels, when you make some progress to right until 600 pixels for example and lock the screen there, then after unlocking the screen if you move left and want to make a group back to 400, then the spawn coords will always count from 600 even if you will set  "at  400".

Another thing i wonder, did you not have the screen automoving left yet? If yes i have the solution for this as well ;)

Oh, and one more thing. About this issue in general, i don't think it have to do with scrollx. I feel like the "direction both" levels are treated from the engine more like "direction right" levels when it comes to spawns and grouping.
 
Maggas where have you been? I will try to experiment with this , now im into creating patterns for boss enemies.
 
but with scrollx when screen unlocks after killing enemies then spawn positions are messed up and shifted if it unlocked when you were on far left.Did You ever had this issue with scrollx ?

That's strange. I never had such issues
Even if spawns were shifted, it won't be too far cause enemies don't need specific spawn coords
Just what kind of entity you're trying spawn correctly here?
 
Ive got this to fix scrollx problems
Code:
	@script
void self = getlocalvar("self");
    int x = getentityproperty(self,"x"); //Get character's x coordinate
    int XPos = openborvariant("xpos");
       if( frame == 0){
changeentityproperty(self, "position", x - XPos);
}
	@end_script
It spawns at exact coords according to entire stage not camera or player.
I need spawns for enemies at doors and windows, they cant be shifted and this script does the trick.
How should i convert this script to one line ? something like :
Code:
@cmd	changeentityproperty getlocalvar("self") "position"  (getentityproperty(self,"x") - openborvariant("xpos"))
Which doesnt work cause i dont know how to properly assembe such script
 
bWWd said:
Ive got this to fix scrollx problems
Code:
	@script
void self = getlocalvar("self");
    int x = getentityproperty(self,"x"); //Get character's x coordinate
    int XPos = openborvariant("xpos");
       if( frame == 0){
changeentityproperty(self, "position", x - XPos);
}
	@end_script

Well this is the same as Bloodbane's "noscrollposition" script:

Bloodbane said:
There's another useful script I usually use, it is spawnscript which spawns entity at defined coordinates ignoring current scroll position.

Since I can attach anything here, I just post the whole script here:

Code:
// Removes scroll pos effect allowing entities to be spawned exactly at defined x coordinate
void main()
{
    void self = getlocalvar("self"); //Get calling entity.
    int x = getentityproperty(self,"x"); //Get character's x coordinate
    int XPos = openborvariant("xpos");

    changeentityproperty(self, "position", x - XPos);
}

Save this as noscpos.c
Example of usage is:

spawn DoorG
spawnscript data/scripts/noscpos.c
flip 1
coords 1000 400
at 0

There's a script tag version for this, here:

spawn DoorG
@script
void main()
{
    void self = getlocalvar("self");
    int x = getentityproperty(self,"x"); //Get character's x coordinate
    int XPos = openborvariant("xpos");

    changeentityproperty(self, "position", x - XPos);
}
@end_script
flip 1
coords 1000 400
at 0

Works same as spawnscript version. Good thing about the 2nd version is that it allows you to insert more script.

This script is useful in direction both level in which you want to spawn entity in exact spot regardless of current scroll position such as door animation.
 
Yes , we should have this hardcoded somehow cause some stuff need it.Something like last value of coords  and if its "coords 222 333 444 1" then it would spawn according to stage coords , kinda like with this script.
 
bWWd said:
Yes , we should have this hardcoded somehow cause some stuff need it.Something like last value of coords  and if its "coords 222 333 444 1" then it would spawn according to stage coords , kinda like with this script.

I liked this idea.
 
(sigh)
magggas has posted it above. I made the script to be used with spawnscript cause there was no feature to allow scripts in level texts that time. However, now since script is allowed, I could make a simpler script like this:

Code:
@script
void main()
{
    void self = getlocalvar("self");
    changeentityproperty(self, "position", 120);
}
@end_script

Same effect but simpler and easier to understand ;)

bWWd said:
How should i convert this script to one line ? something like :
Code:
@cmd	changeentityproperty getlocalvar("self") "position"  (getentityproperty(self,"x") - openborvariant("xpos"))
Which doesnt work cause i dont know how to properly assembe such script

IMO this effect shouldn't be hardcoded into entity's SPAWN. There might be some levels where entities which special SPAWN could be spawned not on specific coords

But if you insist, here's the single line code:
Code:
@cmd	changeentityproperty getlocalvar("self") "position" (getentityproperty(getlocalvar("self"),"x")-openborvariant("xpos"))
 
Yeah this worked , thanks for the script
Bloodbane did you tried to write single line @CMD script for x v elocity change wih direction taken into account so it will flip velocity based on where youre facing? Or we cant use "if" in such script so its not possible to keep it short like that ?
 
IMO this effect shouldn't be hardcoded into entity's SPAWN. There might be some levels where entities which special SPAWN could be spawned not on specific coords
I think you misunderstood what he said. His idea is, IF PRESENT, that value will make the spawn relative to the stage and not to the camera.
Kinda like the parameter RELATIVE on Summonframe or Projectile.

If this setting make sense on those examples (and it does), it makes sense here too ;)
And I don't get your "sigh", lol.
 
did you tried to write single line @CMD script for x v elocity change wih direction taken into account so it will flip velocity based on where youre facing? Or we cant use "if" in such script so its not possible to keep it short like that ?

No cause frankly, this script:
Code:
@cmd	changeentityproperty getlocalvar("self") "position" (getentityproperty(getlocalvar("self"),"x")-openborvariant("xpos"))

isn't short. I can't imagine how long the @cmd would be if "if" were to be included (if that's even possible)
I prefer to just write a function for that

I think you misunderstood what he said. His idea is, IF PRESENT, that value will make the spawn relative to the stage and not to the camera

That's why I posted that before :), the script does make spawn relative to the stage
I think he misunderstood what I posted .... that's why I sighed
 
Back
Top Bottom