#Problem with procedurally generated platform system

23 messages · Page 1 of 1 (latest)

tropic mica
#

So, i currently writing a procedurally procedural platform spawner script, and here's the spawn functions :

var platform1Scene = preload("res://Scenes/Platform1.tscn")

func spawnLevelPart():
    spawnPosition = lastEndPosition #get the value of lastEndPosition 
    lastLevelPart = spawnLevelPartInstance() #get the return of the function 
    lastEndPosition = lastLevelPart.get_node("EndPlatform").position #get the position of the EndPlatform object from the current spanwed platform
    
func spawnLevelPartInstance(): 
    levelPartInstance = platform1Scene.instantiate() #get the instantation of the scene Platform1
    levelPartInstance.transform.origin = spawnPosition #place the instance of the scene Platform1 to the spawnPosition value, so at the EndPlatform object of the previous platform
    add_child(levelPartInstance) #add the instance as a child of the node 
    levelPartInstanceNode = levelPartInstance as Node #make the instance as a node ('cause before that, it was a Transform)
    return levelPartInstanceNode #return the node of the instance 

The problem i encounter is that the second platform, and all those that follow don't seem to get the spawn position value, as they spawn at the end of their own EndPlatorm object, and not the one of the previous platform.

Did someone can help me resolve that issue ?

wet turtle
wet turtle
#

Nevermind. What I said is probably wrong, because origin is the "translation offset of the transform"
Nontheless, we typically would use levelPartInstance.position

tropic mica
#

I tried with it, it doesn't change anything

#

Does the spawnLevelPartInstance function is correct, in the sense that she effectively create a new instance of platform1Scene every time she's called ?
I wonder if the problem is not related to that

wet turtle
#

I'm setting up a small test project to try things out. Might take a moment.

tropic mica
#

Alright
I can give you the entire script if you want

#

Maybe this will help

wet turtle
#

How is your scene hierarchy set up for levelPart?

#

because you're returning leelPartInstanceNode as lastLevelPart?

tropic mica
#

Yes, because i have to get the node "endPosition" of lastLevelPart
When i instantiate it, it's a Transform object and so i have to make it a node, otherwise i can't get the node "endPosition"

#

@wet turtle Oh wait ! I just realize that i can actually get the node "endPosition" of levelPartInstance, not only levelPartInstanceNode

#

But, eh, that doesn't change anything

#

I really think that the problem is causing by these 2 lines :

lastEndPosition = lastLevelPart.get_node("EndPlatform").position #get the position of the EndPlatform object from the current spawned platform

But i really don't know why

wet turtle
#

@tropic mica I should hove noticed this earlier, you're using "position", which is always local to its parent.
lets assume that endPlatform is V3(3,0,0) offset from platform1Scene:

The first platform will spawn as 0,0,0 -- its endplatform.position will be 3,0,0
The second platform will spawn at 3,0,0 -- its endplatform.position will be at 3,0,0
The third platform will spawn at 3,0,0 -- and so on

You want to use globalPosition.

#

I also simplified things a bit. We don't need 5 global variables to instantiate the levelParts 😄
mainFunction is where you've been calling spawnLevelPart from.

var initialSpawnPos : Vector3 = Vector3.ZERO

func mainFunction():
    var spawnPos : Vector3 = initialSpawnPos;
    for n in 5:
        spawnPos = spawner(spawnPos)
    
func spawner(spawnPos : Vector3):
    var Part : Node3D = platform1Scene.instantiate()
    Part.global_position = spawnPos;
    return Part.get_node("EndPlatform").global_position```
tropic mica
#

What is initialSpawnPos ?

#

@wet turtle sorry, your code don't seem to work with mine

#

Ok, i resolve the issue !!

#

That was so simple, i'm really dumb 😅

#

So, to explain, i use global_position at theses lines :
levelPartInstance.position = spawnPosition #place the instance of the scene Platform1 to the spawnPosition value, so at the EndPlatform object of the previous platform
lastEndPosition = lastLevelPart.get_node("EndPlatform").position #get the position of the EndPlatform object from the current spanwed platform

I added a print to see what the returned position was, and i saw that it was the position of the endPosition node of the last spawned platform, but the platforms doesn't spawned
So i search what could cause that, and i saw that my line for calculating the player position compared to the position of the last spawned platform what that : playerPosition -= lastEndPosition
So yeah, no wonder why the platforms don't spawned 😅
So, i removed the lastEndPosition subtraction, and here it is ! The platforms spawned correctly

Ps : sorry if i do some langage mistakes, i'm not an english native 😅

wet turtle
# tropic mica <@281131995356987392> sorry, your code don't seem to work with mine

I don't quite understand what playerPosition has to do with the platforms spawning, unless the -= pushed the platforms out of the camera frustum?

As a rule of thumb for keeping code legible, avoid giving temporary variables global scope. There's lots of exceptions to this rule, but functions should be self-containing wherever possible. So either passing in parameters from a previous operation, or using an actual global variable.
That keeps them more flexible and legible.

Do you want to look at why my code sample didn't work with your script?

tropic mica
#

Not now sorry, i have to go to bed, i have school today
Anyway, thanks for your help !