Design of Main.tscn - leiget/Godot_FPC_Base GitHub Wiki

This chapter explains the main scene (β€œMain.tscn”) and its scripts. Each item has the name first and its node type in parentheses.

  • Main (Spatial)
    • β€œInit_Script.gd” is attached to this node.
    • Env_and_Lights (Spatial)
      • WorldEnvironment (WorldEnvironment)
        • Uses the β€œMain_Env.tres” file in the β€œenvironment” folder.
      • Sun (DirectionalLight)
        • Just the main light.
    • Base (Spatial)
      • A instance of β€œBase.tscn”.
      • Platform_Low (KinematicBody)
        • β€œelevator.gd” is attached to this node.
        • Has a β€œAnimationPlayer” node which has a β€œLift” animation.
      • Platform_High (KinematicBody)
        • β€œelevator.gd” is attached to this node.
        • Has a β€œAnimationPlayer” node which has a β€œLift” animation.
      • Big_Sphere (RigidBody)
        • β€œSphere.gd” is attached to this node.
      • Blue_Sphere (RigidBody)
        • β€œSphere.gd” is attached to this node.
      • ZipBox (KinematicBody)
        • β€œelevator.gd” is attached to this node.
        • Has a β€œAnimationPlayer” node which has a β€œLift” animation.
      • FloorPlatform (KinematicBody)
        • β€œelevator.gd” is attached to this node.
        • Has a β€œAnimationPlayer” node which has a β€œLift” animation.
      • SmallStep_01 (StaticBody)
      • Smallstep_02 (StaticBody)
      • Smallstep_03 (StaticBody)
      • BigStairs (StaticBody)
      • BigStairsPlatform (StaticBody)
    • GIProbe (GIProbe)
    • Player (KinematicBody)
      • And instance of β€œPlayer.tscn”.

Scripts Inside Main.tscn

There is only one not explained elsewhere.

Init_Script.gd

There is only one global variable in this scene: a bool that says if we want to have collision slide visualizations. If you set this to true make sure you also put in β€œemit_signal("Coll_Sphere_Show", slide, get_slide_collision(slide).position)” somewhere in β€œPlayer.gd”. Check the bottom of this script for a little more information.

After that we have a β€œ_ready()” function. It sets the window title, mouse mode, and unhandled input process.

Then we have a "if" statement checking if we want to have collision spheres. If we do then it sets up a variable for pointing to the player node, a collision sphere array to hold the instance pointers to the collision spheres, and an integer that holds whatever β€œMaxSlides” is inside β€œPlayer.gd”. After that it resizes the collision sphere array to whatever β€œMaxSlides” is. By default that’s four, so our array would be four elements long. We also have a variable that holds the collision sphere scene so that we can instance it however many times we need.

After this there is a loop that creates instances of the collision sphere scene (β€œColl_Sphere.tscn”) according to the amount of slides there are in the player’s script.

In this loop it first creates the instance that we need. Whenever we β€œinstance” a scene, what we are doing is basically just copying it and making a new, independent object of whatever is in that scene. As I said it’s completely independent and has it’s own variables, settings, and whatever else.

The reason that is important is because our next step in this loop is to set the β€œArray_Element_Number” variable inside the β€œColl_Sphere.gd” script that is linked to the instanced scene. Each when setting these variables, each scene has it’s own β€œArray_Element_Number” variable. It doesn’t copy over from one sphere to the next. We set the number of β€œArray_Element_Number” according to whatever iteration the loop is currently in. By default, the variables would number from 0 to 4.

After this, we add the instanced sphere to be a child of the current scene, which would be β€œMain.tscn” in this case, as it is the one that has this script attached to it. Here’s the code:

get_tree().get_root().get_child(0).call_deferred("add_child", Coll_Sphere_Array[x])

Click here to go to the online Godot manual to see how scene trees work.

If that link doesn’t work, then look in the Godot manual for β€œScene Tree”. If that doesn’t work, then look for β€œget_tree()” or β€œget_root()” and go from there.

One important aspect of this line of code is the β€œcalled_deferred()” function. What this does is simply defer the execution of the function named in the first argument to until the current scene is completely loaded, so as to keep the engine from trying to load something that isn’t ready yet.

The first argument of β€œcall_deferred()” is the function you want to execute when everything is done loading, which in this case is β€œadd_child()” without the parentheses. It must be a string. The second argument is the what you want the first argument of β€œadd_child()” to be. If you have another function with multiple arguments just add them in a comma separated list. An example:

call_deferred(β€œSomeFunc”, 1.2, SomeVar, 5.0)

After that, we finally connect the β€œColl_Sphere_Show” signal of the player’s node to the β€œColl_Sphere_Show()” function of the collision sphere’s script. Make sure to emit that signal in β€œPlayer.gd” somewhere.

The following code, which is an initialization of a temporary variable and a for loop, just sets the initial y position of these sphere instances in 50cm increments above each other, so that the programmer can see them and make sure that things are initializing correctly. It’s not necessary.

After the β€œ_ready()” function we have an unhandled input function. All it does it quit the program when the player presses whatever the β€œui_cancel” action is (the β€œEscape” key by default), and toggles the window into or out of fullscreen whenever the player presses the β€œToggle_Fullscreen” action (F4 by default). Note that β€œis_action_just_pressed()” only gets input once when the button is initially pressed. This works well here as we only want the window to change whenever the player presses the key, but not when it is held down, as that would cause the window to toggle between fullscreen and windowed mode way too much.

Lastly, at the bottom we have the example code you can put into β€œPlayer.gd” to show the collision spheres.

Debug Scripts

Here I explain how the two debug scripts work.

Coll_Sphere.gd

This is to be used with an β€œImmediateGeometry” node. At the top it has a single global variable called β€œArray_Element_Number” to be used to say what element index the current instance of this script’s collision sphere inside the collision sphere array is. So if you have a β€œMaxSlides” of 4 inside the player’s script, then the β€œArray_Element_Number” at the top of the file represents the slide number to be visualized. Also check the "Init_Script.gd" section on for more information.

β€œ_ready()” makes a sphere and a line going through it. Check the Godot manual or online for tutorials and information about ImmediateGeometry.

The only other thing in here is a function called β€œColl_Sphere_Show()” which sets the position of the sphere according to the position of the collision slide referenced in the player script.

What it does is take a slide number and a 3D vector. It checks to see if the current slide number (inside the player script) matches the β€œArray_Element_Number” set while being instanced in β€œInit_Script.gd”. If they’re the same it sets the position of the sphere to the current slide collision’s position using an signal.

Pos_Visual_Axis.gd

This is also to be attached to an β€œImmediateGeometry” node.

In the β€œ_ready()” section it creates a line primitive that has three lines all intersecting each other in the center. After that it connects β€œSet_Position()” in this script to the β€œRender_Pos” signal in β€œPlayer.gd”.

In the β€œSet_Position()” function it takes a 3D vector given when emitting the signal from β€œPlayer.gd”, and simply sets the location of the geometry according to the vector given. That’s it.