domingo, 25 de julio de 2021

Godot / GDScript Dialog Box approach

 So, I have a gameplay, an UI over it, and I want in some cases, to ask the player what he wants to do in certain situations.

Comig from a backend/desktop in .net framework background, I use to work with DialogBoxes all the time. But the execution lines and structures are not the same when it comes to gamedev. Also, my experience with Godot is very limited.

I'll show you how I resolved the problem, and if you know any alternative, maybe more efficient, perhaps more elegant, please tell me!

Well, this is so far the gameplay screen, where I've a close button.


When it's pressed, the DialogBox is shown:


And here comes the magic :P

The DialogBox has a setup function that receives the dialog type, the text to show, and the font size. When one of the buttons is pressed, it emits the close signal, passing as well the result pressed, and the dialog type.

Next, I've the UI (a CanvasLayer):


The UI has the show_dialog() function, with the same parameters as the setup from the DialogBox. Also, it connects with the close signal, for receiving it when its called back, and then pops it again with dialog_response signal, to the original caller (in this case, the gameplay). 

Finally I've my gameplay:


Here I firstly connect some signals to the callback functions, including the dialog_response. As will be some variety of dialogs, a type must be specified (for now, it's just a string, but must be an enum). So, when the signal comes, I check if it's a response for a quit game dialog, and if the response is Yes. 

Not the best solution at all, but it works.

Surely in some months I'll be reading this so ashamed.




No hay comentarios:

Publicar un comentario