Forum rules - please read before posting.

Post user data to a server

Hello Chris, 
I am trying to create and save a player account to a server.
I succesfully created a menu system that asks for email and password, and stores them inside global variables. 
Now I'm trying to write a custom Action to call the function that posts data to a server.
I have this function working, but I need to pass some parameters and I don't know how, using Object -> send Message. 

IEnumerator AddUserstring emailstring password){

        string hash = Utilities.Md5Sum(email + password + secretKey);
        string post_url = addUserURL  +
            "email=" + WWW.EscapeURL(email) + "&password=" + WWW.EscapeURL(password) + "&hash=" +hash;
        print (post_url);
        WWW hs_post = new WWW(post_url);
        yield return hs_post;

        if(hs_post.error != null){

            print ("There was an error adding user: " + hs_post.error);
        }
    }

This function is called like this:

void Run_AddUser(string emailstring password){

        StartCoroutine(AddUser(emailpassword));

    }

How to call this function from AC?
Thank you

Comments

  • Unity's SendMessage command only allows for one parameter, so that wouldn't be suitable here.

    Instead, you'd have to write a custom Action that does it instead.  See the tutorial here.  In your Run() function, you'd call the Run_AddUser function in your other script, e.g:

    myOtherScript.Run_AddUser ("email@email.com", "thepassword").

    Making sure that Run_AddUser is a public function, and that myOtherScript is a reference to your custom script.

    You could then replace your two string fields with e.g. global variables:

    string email = GlobalVariables.GetStringValue (2); // Global variable with ID = 2
    string password = GlobalVariables.GetStringValue (2); // Global variable with ID = 3
    myOtherScript.Run_AddUser (email, password);
  • Thank you!
  • Hello, 
    I am trying to add an empty gameObject with the server script to the custom Action. 
    I can add it, but when I start the game the reference disappears. 
    If I add it while the game is running, it works perfectly.
    What am I doing wrong?
    Here is a screenshot:
    image

  • Looks fine to me.  And you're referencing a GameObject that's within the current scene?

    Do a test with a GameObject - no need to do anything with it in Run(), just get the UI to work.  Your code follows the same structure as other Actions, so it's not apparent from your code why it doesn't work.
  • Yes the GameObject is inside the current scene, with just the "HSController" script.
    I tried several times, renamed the script to 'ServerScript', restarted Unity, but the reference disappears as soon as I start the game.
    Since the reference to a GameObject doesn't disappear, I tried with a GameObject and then with GetComponent<'Script Name'>() and it worked.

    if(serverScript){
                    serverScript.GetComponent<ServerScript>().Run_AddUser(GlobalVariables.GetStringValue(11), GlobalVariables.GetStringValue(12));

    }

    Thank you!

  • Hi g__b and Chris.
    I know it's an old post, but may I ask for the final working custom action? The link is dead.
    Thanks
  • You're probably best off sending g__b a PM.
  • I hoped someone kept the memory of it somewhere :smile:
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.