Comments (10)
@jrenner I have done exactly what he has in his second block, and it works great.
Foo = load(path) as PackedScene
let foo = Foo.instance()
Here you can see me doing this in a tangible example, from an RTS prototype I was working on where buildings could spawn units. Check out lines 25 and 42. As you can see by looking at my unit.nim file, I didn't have to do anything special for it at all, so there's really no setup to make this work.
You really just do exactly what you had in your example question, but use load
instead of preload
.
As @endragor said, you would typically make a resource manager so you can do the "pre" part of preloading, and this has been something I always end up doing in gamedev. It just so happens GDScript makes it more accessible.
from godot-nim.
You should use gdnew
for instantiating Godot objects. So gdnew[Sprite]()
for your example.
from godot-nim.
If the type inherits Reference, then yes. Otherwise, no. Nodes in particular are freed together with their parents. But if you leave a node detached from the scene tree and never free it, then it's a leak. This is similar to how GDScript and Godot work in general.
from godot-nim.
With GDNative there is no notion of "preloading", since the library is loaded as a whole with all the containing types. With GDScript each script is loaded separately, so there is such concept.
load
is a method of ResourceLoader
singleton and you can decide when exactly to load a resource. If you want to load a scene when the GDNative library is loaded (not really recommended), you can do it in global scope:
import godot
import resource_loader, packed_scene, node
let gameUnitScene = load("res://GameUnit.tscn") as PackedScene
gdobj GameLevel of Node:
proc spawnUnit*() {.gdExport.} =
let unit = gameUnitScene.instance()
The scene will be loaded when the library is first loaded and will reside in memory for the whole game session, which is why it's not recommended. In a real game you'd need to implement some kind of resource manager that would know when to load certain resources (usually when switching a level).
In the above example you can prepare the scene in GameLevel
's ready
method and then use it in spawnUnit
:
import godot
import resource_loader, packed_scene, node
gdobj GameLevel of Node:
var gameUnitScene: PackedScene
method ready() =
gameUnitScene = load("res://GameUnit.tscn") as PackedScene
proc spawnUnit*() {.gdExport.} =
let unit = gameUnitScene.instance()
addChild(unit)
from godot-nim.
I see. So the best solution is probably to just keep to GDScript for things like these.
from godot-nim.
Thanks this was all a great help.
from godot-nim.
Sorry for revamping this, but is there some way to instantiate for example a Sprite?
This doesn't seem to work.
var sprite = Sprite()
print(sprite) # (godotObject: ..., linkedObject: ..., isRef: false, isFinalized: false, isNative: false)
sprite.hframes = 16 # gives me the error below in godot's debugger when I run the app
E 0:00:01.427 Unhandled Nim exception (NilAccessError): Could not access value because it is nil.
C:\Users\Skaruts\.nimble\pkgs\godot-0.7.26\nim\godotmacros.nim(613) methFunc
C:\Godot\Godot-Nim\rl-test\rl-test-5\src\fov.nim(34) init
C:\Godot\Godot-Nim\rl-test\rl-test-5\src\godotapi\sprite.nim(153) hframes=
C:\Users\Skaruts\.nimble\pkgs\godot-0.7.26\godotinternal.nim(19) ptrCall
from godot-nim.
Oh nice. Thanks.
Is it reference-counted too?
from godot-nim.
Ah ok. So I got not worries. I was aware of that kind of leak in godot as well. Thanks.
from godot-nim.
One other thing, if you don't mind: is it possible to access a gdscript custom class from Nim? So if I have Tile.gd
, which isn't attached to a scene, and is defined with class_name Tile
, can I somehow bring it to Nim and instantiate it?
And is it possible to have a sequence of a Godot custom type like that one?
(Also, is it ok if I open an issue to ask a few general questions about the usage of godot-nim? I don't know where else to ask...)
from godot-nim.
Related Issues (20)
- Nim constructor not found for class BulletPhysicsDirectSpaceState HOT 1
- Issue with connecting signal BodyEntered (area) HOT 1
- Generate .dll for other platforms (like Linux) HOT 4
- New native extension system for 4.0 HOT 1
- Basis getEuler behavior
- Add proc directionTo for vector2 and vector3
- GDExtentions Support HOT 1
- Nake Build Error HOT 14
- Nake build failed if GODOT_BIN path contains space character
- How to deal with exceptions? (and other beginner notes) HOT 7
- Documentation search doesn't do anything
- Export questions HOT 2
- Is this abandoned? What's happening HOT 4
- nake build error:: type expected but got symbol 'Array' of kind 'enumField' HOT 2
- godot-nim crashing godot (windows 10) [Solved?]
- I can't find the answers
- Error: type expected, but got symbol 'Array' of kind 'enumField' HOT 1
- is this abandoned? HOT 7
- Output is flooding with `A foreign thread is created` when i use NavigationAgent
- Cant compile to wasm
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from godot-nim.