Is it possible to draw from c++?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Lord_Kaltenberg

So I have been working on a Project for quite some time. In the interest of improving performance, I decided to look into GDNative and alike, but found some stumbling stones (things that I do not understand).

I am unsure how it would be possible to use “draw_line” for example.
Now this might be, because I do not know enough about c++ to figure out on my own, but let me give an example:

TestDLL.cpp

#include "TestDLL.h" 
using namespace godot;


void TestDLL::_register_methods() {
	register_method("_process", &TestDLL::_process);
	register_method("_draw", &TestDLL::_draw);
}

void TestDLL::_init() {
	
}

void TestDLL::_process(float delta)
{
	_draw();
}

void TestDLL::_draw()
{
	CanvasItem::draw_line(Vector2(200, 100), Vector2(200, 1000), Color(255, 255, 255, 1), 1);
}

TestDLL.h

#pragma once

#include <Godot.hpp>
#include <Node.hpp>
#include <CanvasItem.hpp>

namespace godot {
	class TestDLL : public Node
	{

	private:
		GODOT_CLASS(TestDLL, Node)
	public:
		static void _register_methods();
		void _init();
		void _process(float delta);
		void _draw();

	};
	
}

The code will not compile since it detects an error with “CanvasItem::draw_line” which says: “a nonstatic member reference must be relative to a specific object”

The biggest problem here is that I have no clue where to go from here, or if what I have written would even work, without this error.

Any suggestion are appreciated, though if someone could show me a more generalized guide on how to use c++ in this context that might be even more helpful. Thanks! :slight_smile:

:bust_in_silhouette: Reply From: RedPilled

Error “a nonstatic member reference must be relative to a specific object” means that draw_line() is not a static function so you need to create an instance of the class CanvasItem first.

Try this:

CanvasItem canvasItem;
canvasItem.draw_line(....);

Thank you very much! It resolved the problem perfectly!

Lord_Kaltenberg | 2020-02-25 09:19

Ok I need to correct myself a little bit.
The solution worked for the problems I had with building the dll File.
Today I actually applied it to godot with a GDNative-Library.
There nothing showed up. No Error, no message, nothing.
So I played around with the Code a little bit and turns out to call draw_line(), you do not need any object at all.
So in the _draw() function I just wrote the draw_line(…) Stuff, and it actually worked! Awesome!

Lord_Kaltenberg | 2020-03-02 08:42

Ahh! I see. I haven’t worked in GDNative, my answer was purely on the basis of the c++ error that you got. Glad it worked out for you!

RedPilled | 2020-03-02 10:32