I want to understand if for example a game object like a SPRITE that if inserted alone represents a scene in godot is related to the concept of OOP object, in practice I want to understand if it is also an OOP object....
In Godot, a node is an object. Not all objects are nodes.
We can see the documentation for Node, that it inherits from Object, where Object is the base class for everything in Godot.
And, of course, Sprite is a Node.
So, all Sprites are Nodes, and all Nodes are Objects.
What is a scene?
First of all, scenes are not C++ classes. I hope this is evident.
Second, there is something called scene inheritance, which suggest scenes are some kind of classes. However, we cannot extend them in code. Scenes are not classes.
Third, we can create Nodes from scene files. If the scene file is not a class, that suggest they are prototypes. But if scenes are prototypes, how do we explain scene inheritance? Well, as it turns out, scenes are not prototypes. It is much simpler than that…
Scenes are Nodes.
As you know, when you tell Godot to add a scene file to the Scene panel you get an item that has a special icon. And - by default - you don't see any children that where in that scene file. This is only they way they are shown in the UI.
Perhaps you wonder how does it look like in memory. Is there a special type of Node for any scene files you add? And the answer is NO. They are regular Nodes.
When you tell Godot to add a scene file to the Scene panel, Godot loads it and instantiate it, getting a Node. Then Godot adds that Node as a child where you told it.
So how does it remember that that Node was added from a scene file?
The Nodes have a filename property that tell us from where it was loaded. So when you are serializing a Node to an scene file, and it has some child Node that has a filename set, instead of serializing that child Node to the same scene file... It is serialized to the file indicated by its filename, and then included by reference.
Of course, the filename property is not visible in the Inspector panel.
But then, what is scene inheritance?
When you have a serialized scene Node (i.e. a scene file), it contains the properties, scripts, children, connections and so on. And as said above, it can include other files by reference.
Scene inheritance is the special case of inclusion where the serialized Node is defined as another scene. That is scene inheritance is extension by inclusion.
Finally there is something called SceneTree. The game will have an SceneTree object (unless you instantiate more), and it references a Node which is considered the current scene.
Related
I am making an adventure game and, in order to go inside buildings, I would have to change scenes. I wanted to ask, since I want to keep the UI when I am entering buildings, how can I swap out a scene that is not the current one?
I am now using the scenemanager that I got from the official Godot documentation to switch scenes, but it doesn't have any functions to swap existing scenes.
I was thinking about placing a main scene within the UI and then another scene which would hold yet another scene. That last scene would be the one switching.
I'm quite new in programming and especially with javafx.
I'm writing a game and i'm trying to visualize at the same time more instances of the same sprite ( a kind of bullet). It translates and rotates.
In my attempts program doesn't work or normally when press 'fire', bullet run but when press 'fire' more times, every time the 'old' bullet' disappear and another bullet starts. Only 1 bullet on the screen at the same time. What have i to do to have more bullets on the screen, exactly one bullet for every time 'fire' is pressed?
Thank you in advance!
You are probably trying to add the same node more than once to the scene graph.
From the JavaFX Node documentation:
A node may occur at most once anywhere in the scene graph. Specifically, a node must appear no more than once in all of the following: as the root node of a Scene, the children ObservableList of a Parent, or as the clip of a Node.
The scene graph must not have cycles. A cycle would exist if a node is an ancestor of itself in the tree, considering the Group content ObservableList, Parent children ObservableList, and Node clip relationships mentioned above.
If a program adds a child node to a Parent (including Group, Region, etc) and that node is already a child of a different Parent or the root of a Scene, the node is automatically (and silently) removed from its former parent. If a program attempts to modify the scene graph in any other way that violates the above rules, an exception is thrown, the modification attempt is ignored and the scene graph is restored to its previous state.
It is possible to rearrange the structure of the scene graph, for example, to move a subtree from one location in the scene graph to another. In order to do this, one would normally remove the subtree from its old location before inserting it at the new location. However, the subtree will be automatically removed as described above if the application doesn't explicitly remove it.
Here is a sample of multiple animated images in a single scene. In the example, multiple nodes are used, each sharing the same Image data. Because Image is not a Node, it can be shared without issue:
How show specific part of an image in javafx
I am new to JavaFX and i have been recently googling around for a while to understand this.
I am planning to rewrite an existing screen-manager framework in my project which is in Swing.
I am interested to understand what is the alternative to JComponent in FX ?
Is it Stage or window or Control or Parent, i am not able to conclude. Neither i am sure if there is such an alternative any.
Why do i need the alternative for JComponent ?
Well, in the screen-manager framework that i have in place, we always return the type for any individual swing component (say a panel) as JComponent. So i am eager to know the alternative in FX, if we have one.
Thanks in advance for any help.
Somewhat loosely, the equivalent to JComponent is Node
I say loosely, because they are different things and it isn't a 100% equivalence relation, but I guess you can think of them as roughly representing similar things. Nodes represent more stuff than JComponents because they can represent shapes, media views, etc.
JComponent is:
The base class for all Swing components except top-level containers. To use a component that inherits from JComponent, you must place the component in a containment hierarchy whose root is a top-level Swing container. Top-level Swing containers -- such as JFrame, JDialog, and JApplet -- are specialized components that provide a place for other Swing components to paint themselves.
And Node is:
Base class for scene graph nodes. A scene graph is a set of tree data structures where every item has zero or one parent, and each item is either a "leaf" with zero sub-items or a "branch" with zero or more sub-items.
Note that a Scene is contained within a Window (or Stage), so somewhat analogous to a JComponent, a window is not a Node (but pretty much everything else that JavaFX displays is).
See the Working with the Scene Graph tutorial from Oracle for more information on what Nodes are and how they are used.
I wish to implement some Frustum Culling in my JavaFX application, as there can be a large amount of Nodes outside the scene but there are some mouse-events such as dragging which may bring them back to the Scene. Is there some method already in the JavaFX framework or would I have to implement this by myself. I have already tried to implement a clipping method which removes the Nodes outside of the Scene however its difficult to update the Scene after mouse events.
Any help would be appreciated
Thanks
Do you require the nodes to be completely removed forever or you worried about rendering?
Nodes outside of the scene should not be rendered at all, this is handled by JavaFX.
My guess is that each node has Listeners attached.. therefore you are creating
(n ->) listeners ...
Perhaps try creating a method that when hovered, add drag listeners / and remove on hover exit.. Also Nodes not rendered on screen should remove such listeners as well.
And maybe wrapping those listeners in the WeakListener classes
just my thoughts...
I'm just going through the javadoc and various tutorials on libgdx and I'm at the stage of trying to figure out differences between various concepts that seem similar to me or provide similar capabilities in libgdx.
At first I thought scene2d was about creating interactive items such as menus, etc but various tutorials I'm reading use scene2d/actors for the main game items (i.e. the player, etc) and others just use sprites.
What exactly is the difference between using Sprite and Actor (i.e. scene2D) in a game and when should you choose?
Thanks.
A Sprite is basically an image with a position, size, and rotation. You draw it using SpriteBatch, and once you have your your Sprites and your SpriteBatch, you have a simple, low-level way to get 2D images on the screen anywhere you want. The rest is up to you.
Actor, on the other hand, is part of a scene graph. It's higher-level, and there's a lot more that goes into a scene graph than just positioning images. The root of the scene graph is the Stage, which is not itself displayed. The Stage is a container for the Actors that you add to it, and is used for organizing the scene. In particular, input events are passed down through the Stage to the appropriate Actor, and the Stage knows when to tell the Actor to draw itself. A touch event, for example, only gets sent to the Actor that got touched.
But note that Actor does not contain a texture like Sprite does. Instead you probably want to use Image, a subclass of Actor that's probably closer to Sprite than just a plain Actor. Other subclasses of Actor contain text, and so on.
Another big advantage of Actors is that they can have Actions. These are a big topic, but they essentially allow you to plan a sequence of events for that Actor (like fading in, moving, etc) that will then happen on their own once you set them.
So basically Actor does a lot more than Sprite because it's part of a graphical framework.
It is more or less matter of taste. If you want to use actions and stage, use actors. Actors cannot be drawn directly, you need to override draw method. Inside draw you can use sprites.