Getting user name and saving it to scene variable?

I want my game to propose given player’s name for highscores feature and I’d like to make it so it’d propose his name used for logging in to the system.

The question is how to get it using C++ feature and save to a variable? My game will be windows-only so I don’t care about portability.

1 Like

Is it really necessary to do it with C++ events? You have the “Text entry” object :confused:

Text entry is indeed designed for such needs.

Whoops, it was supposed to go to help for game creation section. Accidentally posted here. Sorry! :blush:

Anyway, the idea is to get user’s name and then allow him/her change it to whatever he/she wants via text entry object. In most cases player would just need to press enter. This will lead to the better player experience.

And why c++ events? Well, unless I’m missing something you can’t get windows user name by actions, so using winapi directly seems to be the only option.

So, could someone help me with this (and move this thread to Help for games creations, awful mistake to post here, I’m sorry)? I really want this feature.

I thought about the possibility of getting the username through SFML, but I can’t find anything about it :frowning:
I am not a Windows fan, less when it’s about developing, but maybe you can find and include some built-in windows library that allows you to do it :slight_smile:

Well, I’ve found this: msdn.microsoft.com/en-us/library … 85%29.aspx but can’t make heads or tails of it, not to mention how to use it with GD. Not a C++ programmer, I prefer better languages that don’t need me to deal with pointer hell. Such as Java, C# and Delphi.

Note that using modern C++, standard containers you’ll avoid to use lots of pointers. The old WinAPI is not a good example of how sweet it is to code with nice libraries (SFML for example) :slight_smile: In managed language like Java/C# or Javascript, every variable you use except primitives types (number, strings) are reference to objects: they are like pointers but you do not realize that (until you come across some kind of “memory leak” or “object that get destroyed and I don’t know why” and that’s when you start thinking about who is the owner of your object? And what should be the lifetime of your object).

Maybe, but even then you get across some weird bugs that causes segfaults and which wouldn’t be so easy to make in managed languages. Or Delphi for that matter.

Like the infamous Or bug of GDevelop.

Anyway, since you know C++ better than me, could you help me with that?

Bumpity-bump.

Trying my luck using C++ events and WinAPI, but it doesn’t work. Here’s my cpp code:

[code]#include <GDCpp/RuntimeScene.h>
#include <windows.h>
#include <tchar.h>

void uName(RuntimeScene & scene)
{
//For now, we’re just changing a variable to check if the function works.
scene.GetVariables().Get(“NextLevel”) = “player”;
char user_name[256];
DWORD user_name_size = sizeof(user_name);
if (GetUserName(user_name, &user_name_size)){
scene.GetVariables().Get(“UserName”) = user_name;}
else {
scene.GetVariables().Get(“UserName”) = “player”;
}
}[/code]

And here’s header:

[code]class RuntimeScene; //This is a forward declaration: It avoids including the entire RuntimeScene header file.

void FoolsName(RuntimeScene & scene); //The prototype of the function[/code]

The function doesn’t even seem to be executing (NextLevel doesn’t change).

//edit: List of includes set in cpp event:

"GDCpp/CommonTools.h" "functions.h"

And screenshot how it is set up:

Side note: The C++ event dialog should be streamlined so it is more intuitive and user-friendly. Usually, without outside help I can decipher what each UI control does and how to use it properly. And I’ve figured out some fairly complex UIs, such as Blender’s, AutoCAD’s or Matlab’s (again, without outside help). I can’t figure out C++ event dialog. This is not a good sign.

I saw this question and quickly whipped up this little demo in GD:

imgur.com/e3Tcd8w

It seems special keys (e.g. Spacebar) are given their literal name rather than consequence, so you need to add special cases for those. All seems to work though.

Darkhog, what are the errors shown during compilation?

Why did you name your function:

void uName(RuntimeScene & scene)

but when you declared it, you changed its name:

void FoolsName(RuntimeScene & scene); //The prototype of the function

StackOverflow is your friend (less than a minute searching):
http://stackoverflow.com/questions/11587426/get-current-username-in-c-on-windows

Includes:

<iostream> <windows.h> <Lmcons.h> <SFML/Window.hpp> "GDCpp/CommonTools.h" "TextObject/TextObject.h"
Code:

[code]char username[UNLEN+1];
DWORD username_len = UNLEN+1;
GetUserName(username, &username_len);

std::vector<RuntimeTextObject*> textObjects = GetSpecificObjects(objectsList);
for(unsigned int i=0; i<textObjects.size(); i++){
textObjects[i]->SetString(username);
}[/code]

(With a text object pass for debug) At least it works for me! :smiley:

That foolsname thing was from old version of the code when I was just fooling around. Strange, I remember changing it to uname as well. Maybe forgot to save, oh well. Anyay, even with replacing it to uname, it doesn’t work. There are no compile errors, it just doesn’t work.

//edit: Even example from wiki.compilgames.net/doku.ph … ithcppcode (creating the functions section) doesn’t work. Kept some of the code as you saw in the function to explicitly change NextLevel scene variable to “player”, just like in the example. C++ events are loaded from external events.