Functions - plain English explanation how they work badly needed

Hi there.

I’m only starting with GDevelop (a week worth of experience) making a simple, no animation Castle Builder game. I’ve created a simple functionality: Press the Build Farm button => Level Up a Farm building => Subtract Resources => Produce resources.

And it feels really good to be able to create that I must say!

But that took me quite a lot of time and replicating it for the other Resource buildings will take some more time now which is absolutely fine and expected… but I’m reading that apparently Functions can help speed this up.

THE PROBLEM

…is that I am already pulling my hair out (sometimes, actually, literally…) trying to understand how they work so frankly I need some help. Yes I’ve searched the forum and read the links but I’m only more and more confused how they work as I feel there is a lot of dev jargon and mental cuts.

From what I understood the idea is that in a Function would be identical to the Events logic I created already, just that it’d be using generic placeholders (equivalents to all my variables and text and images) so then depending which button is clicked (Build a Farm, Build a Woodcutter, Build an Iron Mine etc) the Function will pull in the relevant Global Variables into its logic sequence so a single Function can manage variety of functionalities in the game (in this case few buttons that level up resource buildings).

WHAT I DID

So I’ve created Parameters that are equivalents of all my Variables and Text used in my original Button creation. All the Variables that represent dynamic numbers are set as Type:Numbers, the Text that gets updated I set as Type: Object=>Text.

AND WHERE I’M STUCK

I didn’t create any Variables in this Function, as… ekhem… chatgpt told me not to, for whatever reason (but I have no idea if that’s right or not).

But now I’m stuck as it seems that how you set up functions using commands is different from how I did it with Variables because without creating Variables in the Function things like “Change the variable X: set to Y” don’t work as there are no Variables to refer to…

CALLING IN

I’ve read that there is a stage where I need to call in values into a Function so it actually works… which I think would be the next step but for now I am stuck at getting a Function to replicate the logic I created so far.

What am I doing wrong?

I’d seriously need some help of a helpful folk who could explain to me in plain English (again… plain English lol) how to use these Functions.

Thanks folks.

If all you’re doing is calling a function when a set of conditions are met, then consider using external events instead. So rather than call the function, you add a link to the external event.

As long as the scene lining it has the same variables as the external event, you can access the variables in the external events without having to declare or set them again.

I think it’d be a lot simpler to do what you’re after.

1 Like

[weird I can’t edit my posts…?]

I see, thanks a lot MrMen, that sounds like a really good alternative path.

So I wanted to explore and learn about Functions mainly because later on when I start adding variety of bonuses from the tech tree upgrades, as I understand it, it’ll be a lot easier to manage it all using Functions - think I update the Function with a new functionality and it updates all the buildings it affects right?

Same for units etc…? and I think with External Events I’d need to duplicate the code of each of these buildings and/or units and update it manually with every update I add? But there is a very good chance I’m not understanding it correctly.

But I may give it a go if I fail miserably with Functions (which I hope I won’t!) so I appreciate the suggestion.

In the meantime I’m super open to any more Function related help!

Yes, the updated function would act on each object when called. If the function is part of a building’s behaviour, then it would update that building when it’s invoked.

For an external event, you could put all the objects into an Object Group. Then in the scene events, filter the group to the objects to be updated with a condition and add the link to the external events as a subevent. It’ll then run on just the selected objects.

An external event merely holds the position for a chunk of events. When compiled, the actual external events are placed at the point of the link.

Thanks MrMen! that sounds like another way to do it. I’ll explore it a little more to see how well it can mimic a Function.

Thanks again for your help.

I don’t understand why you’re not using variables at all? They’re very helpful!

I had the same issue as you, where if I had multiple buttons that did mostly the same thing, I’d have to duplicate a lot of code, then update every. single. one. whenever I wanted to change something :sweat_smile: Yeah, that’s a hard no.

So what I did was:

  1. create a generic function
  2. make a variable to show which button called that function
  3. make a boolean variable that would trigger the generic function

So when you press the button “Build a Farm”, for instance, the “build a farm button”'s function would change (text variable) buttonPressed to something like “farm”, and would change (boolean variable) buildNow to “true”.

The function buildStuff is triggered when buildNow is set to “true”, and THE FIRST THING IT DOES is set buildNow to “false” (or else it will run the function forever!) Then the function does whatever generic stuff that all the buttons need to be done. If there’s anything specific to the building needed to be done by buildStuff, it can refer to the buttonPressed variable to check which button called that buildStuff.

I’m sure there’s a better way to do this kind of thing? But this is simple and works for me lol

If you want down to earth advises, I guess you could:

  • give a screenshot of a small portion of your events
  • explain what the feature is
  • explain why you would have to duplicate these events to make your game
  • maybe also show the wip of your function

think this is precisely what I’m trying to do ha! just have no idea how Functions work ha! which is what my help request is for. Just FYI This is my first time I’m trying to create a Function so don’t know if I’m doing something wrong.

I’ve made small progress but now I’m stuck at - when I try to do a simple test to see if my little function works and I go to the (Events) tab and under Actions I search for Function section, nothing comes up. but on the help pages it’s there.

So not sure what I’m doing wrong that this doesn’t appear in my Actions list

To give all the context, take a screenshot like this one:

@davy sure thing!

(to clarify, the image I posted above with the Function related Actions, this is from the help pages. And this is what I can’t see my end which is causing the issue now - the Function related Actions when I click Add Action).

The feature is pressing a button to level up a Farm building. So the general mechanism is quite standard:

  1. Click the button to level up a farm, if the conditions for wood / stone / farmers available are met
  2. With each level it takes more wood and stone to level up and also takes more time
  3. Each level produces more food
  4. There is also an overlay that visually shows the building process

I created all this using standard Variables and Events, but then I read that by using Function I can easily automate this same process to build logic for other building (where it applies). So now I am trying to create a Function and will then ditch the original Events that I created so far.

Why I need to duplicate this - so when I am creating the same “Build” button for all other Resource producing buildings (like a woodcutter or quarry where the logic is exactly the same and it functions in an exact same way) I don’t have to duplicate the Events and entire logic and then manually create and then update every Variable within it.

Than also whenever there are new tech unlocks I wouldn’t want to create a very long list of “ifs” for all the different techs per building as that’s a lot of manual work, which I understand a Function can replace also?

Re the Function: I haven’t really created much. Just the Function and the Parameters (which mirror all the Variables + Text / Images that update dynamically from my original Events that I built).

Now within the Function that I created:
Conditions parts seems to work ok, and I can see the Parameters when I add a condition.

************* Where I am currently stuck *************

BUT, under Actions I can’t see my Parameters anymore… so not sure what I am getting wrong at this stage

Here is a screenshot to my Events and logic I’ve built for the Farm Level Up process, in case that’s useful for anything.

Thanks for your help guys.


UPDATE:
Im starting to lose hope. I updated the “Sentence in Events Sheet” with a very long sentence describing what this process does and adding PARAM1 etc, and now I seem to be able to see the Function menu under Add Action with 6 options, so a bit of a victory.

But for God’s heavens I can’t figure out how this whole Function thing works as the Parameters are not showing when I select these Function Actions (I didn’t add any Variables here in the Function, don’t know if I need to? Just added the Parameters so far…).

Either I am very not smart, or this is very not intuitive and help pages are not explaining things too well…
:face_with_head_bandage:


If you need more than 10 parameters in a function, there is a good chance that you’re not going in the right direction.

Making extensions is indeed useful to organize the events, but in you case, I think you should experiment with the following concepts first:

These concepts will allow you to handle all buildings at once without duplicating the events.

You don’t need to use functions like GlobalVariable() around the variables.

Conditions parts seems to work ok, and I can see the Parameters when I add a condition.

I guess you’re expecting parameters to be the variables you filled in the main events. Actually, what you fill in the main events are formulas which can happen to only use 1 variable. So, you can’t change their value since it could be anything from a plain value 123 to a formula like MyVariable * MyVariable2.

1 Like

thanks Davy, much appreciate that. I’ll definitely read on these concepts today and see how I could use them.

But aside of that I’d also want to actually understand how to use the Functions, but seems like there is something mysterious about them… as in I can’t find a good explanation how to implement them and everybody seems to try direct me away from them, even though they seem to be exactly what I need… :thinking:

Thanks @gem !! sorry never got back to you.

I had the same issue as you, where if I had multiple buttons that did mostly the same thing, I’d have to duplicate a lot of code, then update every. single. one. whenever I wanted to change something :sweat_smile: Yeah, that’s a hard no.

This is exactly how I feel right now!!

I don’t understand why you’re not using variables at all? They’re very helpful!

Ah think I didn’t explain myself well. I am using Variables in my “standard” Events (see the screenshot I posted above), but not in the Function as I don’t know if or how to use them… which is actually part of what I am trying to understand in this thread.

So what I did was:

  1. create a generic function
  2. make a variable to show which button called that function
  3. make a boolean variable that would trigger the generic function

And do you think this way of building can be used to create a logic that automatically applies tech upgrades to buildings and units (I image two separate processes needed but that’s perfectly fine).

So if I unlock a tech upgrade for a specific military unit it would add this upgrade to that specific unit - and it would work this way for any tech unlock across all units - and without a massive duplication of the code or manually typing 1000 lines of if=>then" ?

Your solution definitely sounds very interesting! I’m trying it out tonight :slight_smile:

@gem thanks a lot for your suggestion, it’s working like magic!!! this is exactly the solution I was looking for! :)))

@davy also thanks a lot for your help and knowledge sharing, super helpful!

1 Like