Function didn't work...

So I have a .cpp file in GDevelop

#include <GDCpp/CommonTools.h>
#include <scripts/settings.h>
#include <GDCpp/RuntimeScene.h>
class RuntimeScene

void setColor(RuntimeScene & scene, int r, int g, int b)
scene.SetBackgroundColor(r, g, b);
Then in the directory of my GDevelop project I put in a .h file which contains:

void setColor(RuntimeScene & scene, 100, 100, 100)

It was supposed to call the function from the .cpp file and change the background color of the game, but it didn’t work, there’s no errors, it simply didn’t work :frowning:, why didn’t it worked? I already spent a few hours trying to figure it out but I couldn’t :neutral_face:

Where is the function called ?

I made the .cpp file in GDevelop then I made the .h file in the directory of the project([…]project_directory/scripts/). I’m calling the function from the .h file.

In a .h file (header file), you define/declare the function, you don’t call it.
You need to learn C++ before trying to do anything with it in GDevelop.

(By the way, there is an example provided with GDevelop)

Ah so I declare the function in the .h file then call it in the .cpp file? but is it possible to declare and call a function in the same file?

By the way, there is an example provided with GDevelop

Ok so I made these:

#include <RuntimeScene.h>
#include <RuntimeObject.h>
#include <settings.h>

bool setPos(int o_id, int x, int y, std::vector<RuntimeObject*> & objectsList)
if GlobalVariable(state) == “playing”
for unsigned int a = 1;a<objectList.size();a++)
if objectList[i].Variable(id) == o_id
return true
else { return false }

setPos(1, 500, 500, std::vector<RuntimeObject*> & objectList)

Problem: It doesn’t work lol, the position of the main character is the same, it didn’t change position… :frowning:

By the way, there is an example provided with GDevelop

You are confused, I’m not a C++ developer, but the function call doesn’t need the type of parameter “std::vector<RuntimeObject*> & objectList” but just “objectList”, and you are doing a call from a .h file when victor said

By the way you forgotten the semicolon after the function call. Try to do some C++ programs outside GD to know a bit about it :wink:
Again, not a C++ developer, but: “for unsigned int a = 1;a<objectList.size();a++)” doesn’t looks good, maybe a missed “(”, and then you use “i” as index instead “a” :astonished:

Correct me (4ian, victor, …) if I’m wrong, but the idea is something like:
In ObjectTools.h:

void SetPosition(float x, float y, std::vector<RuntimeObject*> & objectList);

So the compiler get the basic information to declare the function (its name, parameters type and return value), I seem to remember that parameters name are not needed, just types.

In ObjectTools.cpp:

void SetPosition(float x, float y, std::vector<RuntimeObject*> & objectList){ for(unsigned int i=0, i<objectList.size(), i++){ objectList[i]->SetX(x); objectList[i]->SetY(y); } //return; }
Add the two files as GD external source files, then in a C++ event add the file in the dependencies and incude the .h file, finally call the function:

SetPosition(100, 50, objectsList);

GD is not intended to be used with sources to be useful (unlike others “non programming” software), sources are there in case you are a developer and want to fully exploit its potential, or show cool things that could be added through demonstration. If you need something that can be only made through C++ and you find it useful, you could make a feature request, or start learning C++ (wich is a good choice, really) :slight_smile:

Ok I’ll try to learn some basic C++ stuff first, I’m still a bit confused about C++ :frowning: