I have an EnemySpawner Blueprint Class that does:
EnemySpawner has a Spline Component named "Path" which the Spawned Enemy must follow. For it, Enemy's Blueprint has another Spline Component named "Enemy Path" that must be initialized by the Enemy Spawner at the moment of the creation, as does in the screenshot above.
But, in my Enemy, if I try to access to "EnemyPath", I get the following error:
Blueprint Runtime Error: Accessed None trying to read property
PathActor from function: 'ExecuteUbergraph_Enemy' from node:
DestroyActor in graph: EventGraph in object: Enemy with description:
Accessed None trying to read property PathActor
So, in other words, Null Pointer Exception.
If, for example, in the Enemy's Blueprint I do (with testing purposes):
The Actor Owner of the Spline Component isn't destroyed.
Even if I try to assign all the Owner actor, not only the Spline Component, it doesn't work either.
I've also tried to print something in screen after the "SpawnActor Enemy" node of EnemySpawner and in the "Event BeginPlay" node of Enemy, and I've checked that obviously the print in EnemySpawner happens first, so the only problem is that the variable isn't really assigned.
The last thing I've tried is to destroy the Spline's Owner right after the "Set" node, directly in EnemySpawner blueprint, by getting it from the Spawned enemy, and to my surprise, it's destroyed!
So, some idea about what's the properly way of doing this?
You can set any variable in a BP actor with an "Expose on Spawn" flag (in the details panel when the variable is selected in the BP editor) which will add a pin for a value to pass into it when using the SpawnActorFromClass node. Make sure you set the variable(s) "Editable" as well.
I think maybe we are doing this wrong.
In straight up C++ you create a class, create the variables, modify the variables by creating a function & passing by ref.
I think that if the Blueprint is a class ( which it is), and if the variable is a variable (which it is), you can't really expect to assign a value to it without calling a function that does it... Then again for all i know maybe the engine does do it for you...
Related
I am adding a main menu to my game. The thing I am doing is adding them in a scene and then changing the scene to the game scene. However, my game has errors which belong to other scenes that do not have an instance in the existing scene. I get errors such as:
Invalid get index 'HasEntered' (on base: 'null instance').
My entire project is here: https://github.com/Ripple-Studios/Godot-Wild-Jam-36-Game-Ripple-Studios
I would appreciate if someone would help me.
Thanks,
I had a look at the linked code.
What is happening is that the practice of getting nodes of the parent has come back to bite you. I'm talking code that looks like this:
get_parent().get_node(...)
You have an scene (HospitalScene.tscn) instanced in your main scene (MainMenu.tscn), which has code similar to the shown above.
The particular error you refer to comes from code that looks like this:
func _on_Area2D_body_entered(body):
var HospitalPosition = get_parent().get_node("Hospital")
if HospitalPosition.HasEntered == true:
HospitalPosition.isInHospital = false
This code is firing at the start because the Area2D is overlapping an StaticBody.
The code is trying to get a sibling "Hospital" which does not exist in the scene tree. And thus HospitalPosition is null. And trying to access HospitalPosition.HasEntered when HospitalPosition is null results in the error you mention:
Invalid get index 'HasEntered' (on base: 'null instance').
The scene is trying to reach a node outside of itself. And there is no guarantee that the scene will be instanced where such node is available. Thus, in each and every case of get_parent().get_node(...) this could happen.
In fact, when running the game, I get four error that look like this (but with different paths):
E 0:00:01.494 get_node: (Node not found: "Hospital" (relative to "/root/CanvasLayer/ParentSprite/Control").)
<C++ Error> Condition "!node" is true. Returned: nullptr
<C++ Source> scene/main/node.cpp:1325 # get_node()
<Stack Trace> HospitalScene.gd:28 # _on_Area2D_body_entered()
You could use get_node_or_null instead of get_node and check for null.
You could also export NodePath variables instead of hard-coding the paths.
Better decoupling strategies than checking if instances are valid include:
Connect to the signal from outside the scene. It is a common pattern in Godot to call down the scene tree, and signal up the scene tree. See Node communication (the right way).
Connect all the signals through an Autoload (singleton). Which is another common pattern in Godot called a Signal Bus or Event Bus. See Best practices: Godot GDScript - Event Bus.
I am trying to create a topdown shooter with a player and a bullet on a separate scene. But whenever I run the game it says
Attempt to call function 'get_position' in base 'null instance' on a null instance.
BTW I am new to Godot.
The problem seems to be in this function:
const SPEED = 300
var bullet = preload("res://Mini-Scenes/Bullet.tscn")
func shoot():
var b = bullet.instance()
add_child(b)
b.set_position(position)
b.move_and_slide(Vector2(1, 0).rotated(rotation) * SPEED)
And I also don't think that I understand how instancing works,
so my questions are:
How does Instancing work?
What makes the above function fail?
Alright, in order:
Instancing is simple in principle. Godot takes the base Node of a scene and all its children, and copy-and-pastes them into memory. Then, when you add this Node as child to another in the scene, _ready is called, the Nodes begin to _process and _physics_process, as well as a few other things like receive events (as a Node must be within a SceneTree in order to interact with other nodes, and the most common way to add one to a tree is making it the child of another).
For more information, see the documentation on Instancing
The above function actually works flawlessly, I just created a new project in Godot 3.1.1 and duplicated your setup, no errors occur. The problem must be somewhere else in your code or your scene setup, and we'd need to see your full script and scene layout to determine the cause of the problem. If you update your question with that information, or shoot me a message on Discord (Mantissa#2558), I'll be able to fully answer your question.
I want to use point picker to pick (get) coordinates of my points in a point cloud. That is why I want to set QPickingSettings to PointPicking but it seems that cannot be done. Events sent to method mouse_event are of type QPickEvent and NOT QPickPointEvent. What am I doing wrong?
self.picker = Qt3DRender.QObjectPicker(self)
picking_settings = Qt3DRender.QPickingSettings(self.picker)
picking_settings.setFaceOrientationPickingMode(
Qt3DRender.QPickingSettings.FrontAndBackFace)
# set QObjectPicker to PointPicking:
picking_settings.setPickMethod(
Qt3DRender.QPickingSettings.PointPicking)
picking_settings.setPickResultMode(
Qt3DRender.QPickingSettings.NearestPick)
picking_settings.setWorldSpaceTolerance(.5)
self.picker.setHoverEnabled(True)
self.picker.setDragEnabled(True)
def mouse_event(e):
# do something
# e should be QPickPointEvent type
pass
self.picker.moved.connect(mouse_event)
self.picker.pressed.connect(mouse_event)
self.picker.clicked.connect(mouse_event)
self.picker.released.connect(mouse_event)
self.addComponent(self.picker)
I'm not familiar with the python API, but you should not create your own instance of QPickingSettings but use the one that exists as a property of the instance of QRenderSettings (that is a singleton).
If you're using the Qt3DWindow from Qt3DExtras, it will provide an accessor to the QRenderSettings instance.
Otherwise, you'll need to create an instance of QRenderSettings and add it as a component of an entity. By convention we tend to use the root of the scene graph.
I have a question that keeps bothering me. Currently, I have started using Kohana 3.2 Framework. I've written a helper to handle some functionality - I have a number of methods, which are (as it should be) declared STATIC. But, all of these methods are somehow working with the database, so I need to load a model. Currently, every method has a non-static variable like this:
$comment = new Model_Comments;
$comment->addComment("abc");
OK, it seems to be working, but then I wanted to get rid of this redundancy by using class attribute to hold the instance of the model (with is class as well).
Something like this:
private static $comment; // Declaring attribute
self::$comment = new Model_Comment; // This is done within helper __constuct method
self::$comment->addComment("abc"); // And call it within the method.
But, I got failed with: Call to a member function addComment() on a non-object
Question is: is it possible to do it ? Maybe there are some other approaches ?
Sorry for a long story and, thanks in advice! :P
A static method cannot call a non-static method without operating on an instance of the class. So, what you're proposing won't work. There may be a way do accomplish something similar, but what about trying the following:
You could implement the singleton or factory pattern for your "helper" class. Then, you could create the model (as an attribute) as you instantiate/return the instance. With an actual instance of your "helper" class, you won't have to worry about the static scope issues.
In other words, you can create a helper-like class as a "normal" class in your application that, upon creation, always has the necessary model available.
I'd be happy to help further if this approach makes sense.
David
Groovy noob here, I'm working through my first Groovy book and it has example code where it states roughly
"If I want a property to be a ready-only property then declare it final. This is not defining a final field but a read-only property-you can change the property from within instance methods of the defining class, but not from outside"
Here is the code I have in question, but I keep getting an error stating:
cannot modify final field 'miles' outside of constructor.
Code:
class Car
{
final miles = 0
def getMiles()
{
println "getMiles called"
miles
}
def drive(dist){if (dist>0) miles += dist }
}
The book says I should be able to modify miles from within the drive instance method, am I doing something wrong?
I think what they meant (not sure what they said, if you're paraphrasing) is that there's no setter method defined, so it can't be modified from outside the class.
It is, however, still a final property, which means it can't be modified once its set, which would be in a constructor or during the declaration.
Property and field rules
That said, see these two issues: 1628, 2752, so more exploration might be necessary, although this appears limited to local script properties.
My guess is you're using a 1.7+ Groovy, while the book targets <= 1.6.
See also this SO question.