Hello.
I'm currently doing a Pong game, and I think I got a pretty nice result and I'm close to completing it, but I'm having two problems finalizing it:

  • I have a Main Menu scene, with some button options (1P that goes into easy/normal/hard ptions; and a multiplayer option). From this Main Menu scene, the game goes into the Game scene. But I don't know how to change some parameters from some objects in the game, based on what I press on the previous scene (the main menu)
  • For example, the main state of the game is what I would consider the "easy" option for 1P. But if I click on the normal or hard option, I don't know how to affect directly the speed of the ball and the speed of the AI on the code.
  • There's a similar problem for the 2P option. The main state of the game is with a pretty basic AI, but if I click on the 2P option on the main menu scene, I don't know how to alter the code of the enemy to change it to only a controllable sprite, without my AI code.
  • I know how to make a controllable sprite for the second player, or to speed up the ball on every collision, or increase the A.I speed, I just don't know how to do this "exchange" of code based on what I press on the previous scene.

I could avoid all of these questions by having a new scene for all the options (1P/Easy, 1P/Normal, 1P/Hard, and Multiplayer), but I don't think this is a wise choice for organization and optimization.

If it is necessary for me to post my code or my entire project, just let me know, but I didn't post it now since I just want to understand the logic of doing this procedure.
Thanks!

  • Crawfish replied to this.
  • pedrohfmg well, there's also another way, using a Singleton. I don't know what version this is from, it's not listed, but it's 2017, so probably still works. 🙂 Personally I'm not thrilled at recommending any type of Global variable, but don't yet understand what options are available in gdscript, in terms of data-structures, organization, etc.

    pedrohfmg I'm not yet up to newbie-status with Godot, so take this for what it's worth. Was just watching a video on the new features in 3.5, and they mentioned Unique Scene-Names, which basically means that a scene-property, could have a globally-unique name, and is access with the percent-sign, in other code. Don't know if this will do what you're asking for, but it sounds like you do want some kind of global-variable.

    (And the code for ball-speed, say, would have an if-statement to set the speed based on the variable).

      Crawfish This is veeeery interesting. My Godot version is 3.4.4, but I will download 3.5 and see if this works. This is not exactly the procedure that I wanted, but I will definitely take a look into this. Thanks!

        pedrohfmg well, there's also another way, using a Singleton. I don't know what version this is from, it's not listed, but it's 2017, so probably still works. 🙂 Personally I'm not thrilled at recommending any type of Global variable, but don't yet understand what options are available in gdscript, in terms of data-structures, organization, etc.

          There's nothing wrong with using global variables. You don't want everything to be global, since that would make it harder to think up new variable names locally. You also have to be more careful using globals in threads, but most software isn't multi-threaded. Anyway, an autoload is more of a global class, which has easy-to-access parameters, so namespace pollution is barely an issue.

          I use autoloads for anything I want to keep track of between classes or scenes.

          6 days later

          Crawfish I finished the game, and I ended using an autoload.

          I still had some problems when I first tried to use it: there was a part of the code (that I autoloaded) that was refering to another script that I wasn't using autoload, and as the game started, it tried to read this other code as it was being loaded right from the start, but it couldn't read this other script, as it wasn't instantiated.

          But I got a workaround and in the process of trying to solve this, I organized better my code and everything just worked fine. Feeling 1% more like a decent programmer.

          Thanks