My code with FStringView ; requires UE 4.25

Hi
i revised the whole BullCowCartridge to include FStringView which is similar to std::string_view which basically const TCHAR* to FString . It is a pointer to first character and has the size info . it is quite light weight but has negative side as lifetime issues;

Here is my BullCowCartridge.h with FStringView

class BULLCOWGAME_API UBullCowCartridge : public UCartridge
{
	GENERATED_BODY()

	public:
	virtual void BeginPlay() override;
	virtual void OnInput(const FString& Input) override;
	//virtual void OnInput(FStringView Input) override;
	void SetupGame();
	void EndGame();
	void ProcessGuess(FStringView Guess);	// Guess=Input
	void GetValidWords(TArray<FString>& wordlist);
	bool IsIsogram(FStringView Guess) const;

	private:
		FString HiddenWord;
		int32 Lives{0};
		bool bGameOver{false};
		TArray<FString>WordList;
};

I also made an alternative version with OnInput(); with FStringView

virtual void OnInput(FStringView Input) override;

but to do that i had to revise the base class Cartridge.h because OnInput() is a virtual function and made a copy of it with FStringView .
But i was not sure if it will be safe since it will be temporary value . Althoug OnInput() function runs over the lifetime of the game it should not be a problem as all other functions are called from here . Here is the Cartridge.h version with FStringView (both versions works with no problem)

class BULLCOWGAME_API UCartridge : public UActorComponent
{
	GENERATED_BODY()
public:
	virtual void OnInput(FStringView Input) PURE_VIRTUAL(UCartridge::OnInput, );
protected:
	void BeginPlay() override;

	void PrintLine(const FString& Line) const;
	void PrintLine(const TCHAR* Line) const; // Avoid template for this case.
	template<SIZE_T N, typename ...Types>
	void PrintLine(const TCHAR (&Fmt)[N], Types... Args) const
	{
		PrintLine(FString::Printf(Fmt, Args...));
	}
	void ClearScreen() const;
private:
	class UTerminal* Terminal;
};

I love your explanation! Keep up the great work! Have you figured out if it is safe even with the temporary value?

1 Like

thank you :slight_smile:
for the temporary value issue; I compiled and ran the code it works OK and gives correct responses . if it was not it would either give a run time error or give a wrong or does not make sense kind of response ;
Logic wise;
OnInput() takes a temporary value and process it and it gets destroyed at the end so if I use FStringView it will call the ProcessGuess() function then it will go thru IsIsogram () referring to that temp value and it will still exist since it is in the calling function . When these two functions return to the calling function then the temp value will be destroyed , we will either win the game or a new Guess will be asked . the old temp value will be destroyed and a new one will be input then it will repeat the same process…
So long story safe logic wise it is safe…but I will make a debugging session putting a breakpoint on the OnInput() and ProcessGuess() and IsIsogram() … I believe that will enough to see how the value is passed and returned .
What do you think ??? :))) (sorry for the long answer but it also works for me to think thru :slight_smile:

hi
I finally did a debugging of the FStringView inside the OnInPut function, using VS Studio Debugging, attaching the process to UEEditor …Cool stuff …you can see / track local variables when you are playing…when you input a value and hit enter thru the editor, the debugger takes over and I went over the function making Step Into and see the Input variable is being transferred as FStringView to other function correctly and finally returns the OnInPut() function and the temporary value is destroyed after that .
So it is safe …and I think it is as best I could do :slight_smile:

Note: It was kind of tricky how to start debugger thru VS Studio 2019 while using the editor, there are several ways to do it , I still need to clearly understand how it works . When you click on the start debug directly it compiles and start getting symbols and also opens the UE Editor directly and once the editor is open you hit play and start input and depending on the code the debugger takes over and holds until one cycle is over which you click on continue then it returns back to editor and the message is displayed and you do another input afterwards and it just goes on.
The other way I found is to open UE Editor and VS Studio both and in the debugger of VS you select Attach to Process and select UEEditor and the moment you select it . VS studio tried load symbols for the code when that one is done you can just go ahead and start play at UE Editor but sometimes that symbol loading does not work or maybe I start play too early when Play starts the debugger does not do anything you can keep playing but can not track the local variables or so…Not sure if there is a step I am missing…

Privacy & Terms