I have a c++ SaveGame object, but no idea how to call it in C++ or blueprints

I am following the tutorial on how to make a C++ SaveGame object. Everything compiles just fine, but I do not know how to actually call it. I want to learn how to call it in C++ and in blueprints. This is how I am trying to call it in blueprints http://i.imgur.com/RHwl7d9.png?1

The function I am trying to call in blueprints is SaveMeNow(). No matter what I have tried it does not allow me to actually call that specific function. I also do not know how to call it from a different C++ class because it gives me an error that says something like “illegal call of non-static member” or something. I am a newbie when it comes to C++ and any help would be much appreciated.

cppSaveGame.h

#pragma once

#include "GameFramework/SaveGame.h"
#include "cppSaveGame.generated.h"

/**
 * 
 */
UCLASS()
class BP_CRASH_TEST_API UcppSaveGame : public USaveGame
{
	GENERATED_BODY()

public:

	UPROPERTY(VisibleAnywhere, Category = Basic)
		FString PlayerName;

	UPROPERTY(VisibleAnywhere, Category = Basic)
		FString SaveSlotName;

	UPROPERTY(VisibleAnywhere, Category = Basic)
		uint32 UserIndex;

		UcppSaveGame();

		UFUNCTION(BlueprintCallable, Category = "1_Custom")
		void SaveMeNow(FString newName);
};

cppSaveGame.cpp

#include "BP_crash_test.h"
#include "cppSaveGame.h"

UcppSaveGame::UcppSaveGame(){



}

void UcppSaveGame::SaveMeNow(FString newName)
{

	newName = TEXT("Default");
	PlayerName = newName;
	SaveSlotName = TEXT("Fake  name");

	UserIndex = 0;


	UcppSaveGame* SaveGameInstance = Cast<UcppSaveGame>(UGameplayStatics::CreateSaveGameObject(UcppSaveGame::StaticClass()));
	SaveGameInstance->PlayerName = newName;

	UGameplayStatics::SaveGameToSlot(SaveGameInstance, SaveGameInstance->SaveSlotName, SaveGameInstance->UserIndex);

}

Thanks a lot man this worked, and I Google’d to see what you meant by default values in the constructor. I always knew it was possible but didn’t know how!

Thanks again man :smiley:

First off you don’t need to create a new SaveGame variable and do any casting in your “SaveMeNow” function, the only thing you need to do is set your variables and then use:
UGameplayStatics::SaveGameToSlot(this, SaveSlotName, UserIndex);
You should also put all the default values for you variables in your constructor, not in your save-function. It’s just a little bit cleaner that way and you won’t accidentally overwrite your function arguments right in the first line. That way should work with the Blueprint you provided now. For this to work in c++ you can use something like:
UcppSaveGame* SaveGameInstance = Cast(UGameplayStatics::CreateSaveGameObject(UcppSaveGame::StaticClass())); SaveGameInstance->SaveMeNow(SomeName);
wherever you want to save your game/variables(you could also use what you put on the end of your SaveMeNow function, but it’s easier to just use the function you made).
Hope this helps :slight_smile:

EDIT: the UcppSaveGame after the cast is missing, beacuse it interprets the brackets as html, not sure how to fix it.

Thank you~

I think you misunderstood the tutorial a little bit there. Both ways(using the SaveMeNow function and doing what the tutorial says) do exactly the same thing. You create an object of your savegame class, cast it to your type, set the variable and then save it to a slot. In your case your function does the 2 last things. Now to your mistake, you tried to create the UcppSaveGame object inside of the UcppSaveGame class(which is possible but really unnecessary) and then tried to save it with the values of the new object, which weren’t initialized because you didn’t set them in the constructor, instead of just saving the existing object(this pointer). So the code from the tutorial is for use outside of the class(just like using the SaveMeNow function), using the this pointer is for use inside of the class. Otherwise you just create a new savegame object inside of you class, pass the variables on and then save that new object, which is just slower and uses more memory than the direct way(probably not noticeable with PC’s these days, but still a bad thing). This should hopefully clear up the problems you had with it, otherwise feel free to ask again.