Giter Site home page Giter Site logo

Comments (10)

rosshadden avatar rosshadden commented on July 16, 2024 1

@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.

endragor avatar endragor commented on July 16, 2024 1

You should use gdnew for instantiating Godot objects. So gdnew[Sprite]() for your example.

from godot-nim.

endragor avatar endragor commented on July 16, 2024 1

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.

endragor avatar endragor commented on July 16, 2024

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.

jrenner avatar jrenner commented on July 16, 2024

I see. So the best solution is probably to just keep to GDScript for things like these.

from godot-nim.

jrenner avatar jrenner commented on July 16, 2024

Thanks this was all a great help.

from godot-nim.

Skaruts avatar Skaruts commented on July 16, 2024

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.

Skaruts avatar Skaruts commented on July 16, 2024

Oh nice. Thanks.
Is it reference-counted too?

from godot-nim.

Skaruts avatar Skaruts commented on July 16, 2024

Ah ok. So I got not worries. I was aware of that kind of leak in godot as well. Thanks.

from godot-nim.

Skaruts avatar Skaruts commented on July 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.