Exsuce me, Can anyone post the source code for a project using Druid?
I’m new to both Defold and Druid. Documentation is difficult for me. And I really can’t figure out how to use it.
With some example, perhaps I can understand what’s what. But the truth is that the examples from github, including this application(druid 1.0), are not enough for me to understand.
The “Basic Usage” is the simplest explanation out there:
It shows how to create druid instance and button with a callback.
My explanation and tips:
Druid is a library with a code to handle certain types of GUI components.
Initially, you don’t have to dig into how things are done there, you can treat it like a black box that handle all the stuff that you would need to code yourself and it does so with some assumptions like common behaviors - e.g. button is pretty much very common and behaves more or less in all games (and applications) in a very similar way.
To use druid in your gui script you have to link it in game.project as described in “Setup” and you have to add input bindings as described in “Input bindings”.
To instantiate druid, or so to say, to create your very own druid black box you write:
self.druid = druid.new(self)
This instance can handle all components of your GUI (e.g. multiple buttons in e.g. your main menu). So to add a button you add a node in your GUI, take it name (e.g. button_node_name) and use it to create also “logic” part in your code:
The “:” is sometimes confusing for beginners, but it means that you are passing self.druid as first parameter to function new_button, so those are equivalents:
API documentation might be confusing, but it explains what is needed for such built-in, basic components like button is.
You can create all basic components with functions "new_<name_of_component>". (Later on, for advanced usage, you will be able to create your own custom components, or use Druid Extra components in the same way, but you have to “register” them before)
Now, button can do something “on_click” and this is what you define in the third parameter (remember that first is druid instance) and in this exampl there is a function defined:
local function on_button_callback(self)
print("The button clicked!")
end
That is referenced when creating button, so that everytime you click the button, this function is called. You don’t have to worry about how to detect clicking on button, animating it etc - you just worry about what happens, when you click it
Of course, for it to work, you need to somehow handle input, therefore in this example in on_message, there is:
-- "on_input" is used in almost all Druid components
-- The return value from `druid:on_input` is required!
function on_input(self, action_id, action)
return self.druid:on_input(action_id, action)
end
And analogically for other Defold specific lifcycle functions. Again, Druid will handle input correctly and will call your on_button_callback, when user clicks the button.
Though Druid is a simplification, especially for more complex GUI components, I recommend also to nevertheless check how a simple button can be handled from scratch in Defold, just to understand, what’s beneath more or less:
For advanced understanding - Druid is a very big OOP class that handles GUI related logic, so you can create an instance of it that uses components through composition to expand it’s functionality. Basic components are already included in Druid, Extra components are “custom components” that also already included, but not registered in Druid (you have to do it explicitly) and the true power is within creating your very own custom components that follow Druid architecture and API.
If you have a question about other specific components, don’t hesitate to ask on forum
The tutorials, docs and API sure kind of confusing for beginners and I’m slowly work on it, so it should be much better in this year. But you can help me a lot by sharing what exactly you trying to do and how you currently think how it should work. It will help my align with ideas that I need to cover, @Blinchik . Also these two Druid’s questions can help you: