I want to save high scores as part of existing usernames on wordpress website, instead of duplicating tables sql.
Can't find any tutorials or code for this.
Otherwise can't find code for Gamipress and Godot games integration.

How to get and reference each existing WP user?
User:// is vague where it stores data

  • Toxe replied to this.

    redrc You need to explain the problem better and what exactly you are trying to do.

    To elaborate:, every wordpress (wp) site has an existing user base table (SQL). I want to save high scores for each and every user in the existing users table, rather than have Godot create a new separate table with a new users list. Having 2 separate lists would become confusing, redundant and inefficient.
    Also, there may be multiple users logged into the site, playing the games.
    So: steps should be:-

    1. Retrieve the logged in wp-user (username) for scoring purposes (& display it in the game);
    2. Save/ update each player's high score against their name/ id in the existing wp-users table;
    3. Load this high score each time game is played.

    Alternatively: incorporate this into Gamipress - no docs on incorporating Godot games found yet.
    Thanks.

      Thanks Toxe.
      Thanks DaveTheCoder.
      For suggesting SQLite, however as the name suggests it is designed for "light" work. The docs do not recommend it for client/server traffic - which may pull heavy at times, and to choose enterprise SQL instead. It also again would mean a duplicate, separate db and new table of users- or I would have to port over the existing wp db which may break wp server links/ configs, plugins, codec, third parties, etc- a possible nightmare.
      However it is a good mention to keep in mind for future local application usage- thanks.

      I only mentioned SQLite because there's a Godot SQLite addon that lets Godot access an existing SQLite database.

      However, It looks like WordPress uses a MySQL/MariaDB database, so SQLite probably wouldn't work.

      How does a user access Godot on the site? Is it a web export that's run using a URL?

      Communicating between WordPress and Godot could be done using parameters in an HTTP request. You could provide the WordPress username to Godot as a URL parameter when launching Godot, and Godot could use its HTTPClient class to provide the user's score to WordPress. WordPress could keep track of the high scores and display them.

      I have no experience with WordPress, but I've used a different PHP/MySQL content management system.

        redrc Where is your game running? Embedded in a WP site running in a browser or are we talking about a desktop/mobile app?

        In any case, I would go this route:

        • On your web server, where your WP site is running, run a small service that you can connect to. Something like a REST API, but could be whatever. Write it in Python, PHP, JavaScript or whatever you know.
        • From your game connect to that service via HTTPS and send requests like "give me the score for user X" or "here is the new score for user X".
        • Your service, that is running on your server, handles all the database requests.
        • DO NOT connect directly to your WP database from your Godot game.
        • Your game only talks to the service and never to the database directly.
        • Totally create a new database table for the user scores. This is a database, this is what they do. Do not pollute your WP user table with something that has no place there (like user scores for games).

        Thanks Toxe, DaveTheCoder., Tomcat.

        It will be a web export on a WP site URL- running in a browser.
        I will stick to MySQL to be fully compatible with existing WP default. (Avoiding porting too much over to MariaDB replacement- the Github demo video is hours of work for an expert- days for me), and because lots of other plugins I'm using are optimised for Mysql wp tables.
        ..
        Yes HTTP is one way, mentioned on the WP API, however I would prefer direct PHP instruction, avoiding HTTP bottlenecks/ delays- it is like talking to yourself through a telephone- or using a car to return to where you are standing already, after navigating an entire city. The API states PHP as an option- (i'm still learning the wp API).

        Toxe- what is your fear about using the same table, or connecting directly? (Which MariaDB extension does!)

        Other options I'm Looking at are: Advanced Custom Fields plugin
        My preference is to link game scores to Gamipress, because not all games will be Godot. Some will be quizzes and puzzle plugins.
        However there is no Godot addon for Gamipress.

        One script eg I've come across is: (althogh I would have to add security- sanitze it)"

        1. eg user notes input

        `function save_user_data_7231(){
        global $current_user;
        if is_user_logged_in{ //check if user is logged in.
        if (isset($POST['Notes'])){
        // get current user info
        get_currentuserinfo();
        $old_notes = get_user_meta($current_user->ID, 'user_notes', true);
        if (isset($old_notes)&& is_array($old_notes)){
        //if we saved already more the one notes
        $old_notes[] = $
        POST['Notes'];
        update_user_meta( $current_user->ID, 'user_notes', $old_notes);
        }
        if (isset($old_notes)&& !is_array($old_notes)){
        //if we saved only one note before
        $new_notes = array($old_notes,$POST['Notes']);
        update_user_meta( $current_user->ID, 'user_notes', $new_notes)
        }
        if (!isset($old_notes)){
        //first note we are saving fr this user
        update_user_meta( $current_user->ID, 'user_notes', $
        POST['Notes'])
        }
        }
        }
        }

        they to display there notes you can use get_user_meta

        function get_user_notes_654(){
        global $current_user;
        if is_user_logged_in{ //check if user is logged in.
        // get current user info
        get_currentuserinfo();
        $old_notes = get_user_meta($current_user->ID, 'user_notes', true);
        if (!isset($old_notes)){
        $re = 'No Notes YET!';
        }
        if (isset($old_notes)){//we have notes. Removed the extra ! here.
        if (is_array($old_notes)){//more then one
        foreach($old_notes as $note){
        $re .= '<strong>note:</strong>' . $note . '<br />';
        }
        }else{//just one
        $re = '<strong>note:</strong>' . $old_notes . '<br />';
        }
        }
        re .='//add note form would come here';
        return $re;
        }
        }
        `

        Another is inside wp syntax:

        • Toxe replied to this.

          redrc Yes HTTP is one way, mentioned on the WP API, however I would prefer direct PHP instruction, avoiding HTTP bottlenecks/ delays- it is like talking to yourself through a telephone- or using a car to return to where you are standing already, after navigating an entire city. The API states PHP as an option- (i'm still learning the wp API).

          I... cannot follow you. What do you mean by "direct PHP instruction" and "HTTP bottlenecks"?

          redrc Toxe- what is your fear about using the same table

          I don't see why you would want to write data that has nothing to do with WP user account credentials into the same table. You already have a database. Use it. Create a new table, let's call it "user_game_scores", add two columns "user_name" and "game_name" as the combined primary key and add a third column "score" to save the highscores. Done. Now you can add as many unique user + game combinations as you want.

          However if you want to save the highscores in the WP user table you would need to add columns like "game1_score", "game2_score", "game3_score" for every game you would want to add. Also, again, game scores have no reason to be saved in the same table as user account credentials.

          redrc or connecting directly? (Which MariaDB extension does!)

          You should never expose your database server to the horrors of the internet.

          Hi Toxe,
          HTTP is a protocol connecting to other nodes, whether internet, or within a local network of computers.--- ie: requests must leave the initiating node and connect outwards through physical modem or link switch. If you have multiple players on a heavy game, it can drain resources- "bottleneck". But direct PHP requests can remain internal within the same machine interacting with the API. No need to go the long route. It's an unnecessary trip if you can avoid it.

          Right now, I'm trying a simple save script on local machine.
          Can any one tell me why it's not saving/loading my high score?

            redrc does it write to the file?
            maybe when you load there is mismatch with var types

            redrc Can any one tell me why it's not saving/loading my high score?

            When using FileAccess.open, check its return value to see if it failed, and if it did, look at the error code to find out the reason.

                    var err: Error
            	var file := FileAccess.open(path, FileAccess.READ)
            	err = OK if file != null else FileAccess.get_open_error()
            	if err != OK:
            		print_debug("Failed to open '%s' for reading: %d (%s)"\
            				% [path, err, error_string(err)])

            https://docs.godotengine.org/en/4.4/classes/class_fileaccess.html#class-fileaccess-method-open

            redrc HTTP is a protocol connecting to other nodes, whether internet, or within a local network of computers.--- ie: requests must leave the initiating node and connect outwards through physical modem or link switch. If you have multiple players on a heavy game, it can drain resources- "bottleneck". But direct PHP requests can remain internal within the same machine interacting with the API. No need to go the long route. It's an unnecessary trip if you can avoid it.

            I know what HTTP is, I'm just not sure you know what you are talking about. But, it's fine, I said my piece. 😉

            redrc Can any one tell me why it's not saving/loading my high score?

            Do you get any errors or warnings?

            Also, when loading check first if the file exists before trying to open it, not the other way around.

            redrc If you have multiple players on a heavy game, it can drain resources

            If you want to display historical high scores, there's one HTTP request when a player starts a game and one HTTP request when a player finishes a game. I wouldn't think that would cause a problem.

            If you want to display current scores in a multiplayer game in real time, that's different.

            Thanks kuligs2, Toxe, Dave:

            Yes the file exists, and its time stamp modified/updated (per func _ready> save, as expected, -& not without it).

            Dave, thanks for the code- I entered it under the FileAccess.open statement: I don't get any errors.
            Toxe, that makes sense- I tried:- swapping lines 23 & 24, but still no luck.
            Kuligs2 -you could be right about var mismatch: Does the underscore "_" cause problems in this DB?
            Otherwise all lower case.
            ( Scoreboard is otherwise functioning correctly real time: score , high_score and life_score.)

              redrc Im trying to understand whats not working? If you say the save file is being created, does it record the score values?
              So the problem is reading values?

              Can you maybe post minimal reproduction project that we could run and test this particular piecie of code?

              create .zip and upload it here. just delete .godot dir from that zip.

              redrc still no luck

              You need to add print statements, or use the debugger and set breakpoints and inspect variables, so that you can determine exactly which lines of code are working or not working.

              Posting a picture of some code and saying that it doesn't do what you want is not useful.

              redrc Yes the file exists, and its time stamp modified/updated (per func _ready> save, as expected, -& not without it).

              Then I am not sure what the problem is. How big is your file and did you open it and look inside it? And did you try printing the values that you loaded from the file or looking at them in the debugger?

              Also what values are you trying to save? Be aware that store_8() and store_16() only store 8 and 16 bit values and your values must not be outside that range. Also, if you store a value like "0" or "3" and try to open the file with a text editor then you will probably not see anything, because those would be unprintable ASCII characters. So you have to use a hex editor to check your file.

              Try saving a simple string with store_string() just to see what happens or try using store_var().

              Hey Guys,
              I fixed it. It was actually saving to file - But saving "Zero" (0) instead.
              I was following a You Tube tut, and had HUD on Canvas item.
              FileAccess does not inherit this-so I Moved it to Control child node- it worked!