Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | MisterAcoustic |
Apologies in advance, I’m brand new at this.
I have a scene (.tscn) that contains a Spatial with a child MeshInstance of a rock model.
In my main scene, I load the rock scene, and call .instance.
On the instance, I thought I could call a function in the scene script, but I get “Nonexistant function ‘Initialize’ in base ‘Spatial’”.
I’m sorry to say, I don’t know why :).
(Quick background: trying to make a moving asteroid field. I put initial rotation code in the rock scene, thinking it would get called when instanced. Moved the code to an Initialize function, which I cannot call. Also have _process code that I would like to be called on each to rotate each rock, but it doesn’t get called. I’d like some help with that too )
Entire rock scene script:
extends Spatial
var minRotationalVelocity = 0.0
var maxRotationalVelocity = 0.040
var RNG = RandomNumberGenerator.new()
var xRotationVelocity = 0
var yRotationVelocity = 0
#var zRotationVelocity = RNG.randf_range(minRotationalVelocity, maxRotationalVelocity)
# Called when the node enters the scene tree for the first time.
func _ready():
pass
func Initialize():
print("Initialize Called")
print("I am a rock")
RNG.randomize()
xRotationVelocity = randomNegate(RNG.randf_range(minRotationalVelocity, maxRotationalVelocity))
RNG.randomize()
yRotationVelocity = randomNegate(RNG.randf_range(minRotationalVelocity, maxRotationalVelocity))
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
var b = Basis()
var xaxis = b.x
var yaxis = b.y
var zaxis = b.z
rotate(xaxis, xRotationVelocity)
rotate(yaxis, yRotationVelocity)
#rotate(zaxis, zRotationVelocity)
orthonormalize()
func randomNegate(value):
var doIt = RNG.randi_range(1, 2)
print(doIt)
if(doIt == 2):
value = -value
return value
And entire main scene script:
extends Spatial
var initialObjectCount = 1000
var maxObjectCount = 50000
var maxDistanceFromOrigin = 100.0
var objectCount = 1
func AddObjects():
if (objectCount >= maxObjectCount):
return
var numberToAdd = clamp(objectCount * 2, initialObjectCount, maxObjectCount)
var objectScene = load("res://moss rock 05.tscn")
if(objectScene):
print("LOADED")
for i in range(objectCount, numberToAdd):
var newObject = objectScene.instance()
newObject.Initialize()
var t = newObject.transform
t.origin = Vector3(randf() * maxDistanceFromOrigin, randf() * maxDistanceFromOrigin, randf() * maxDistanceFromOrigin) - Vector3(0.5 * maxDistanceFromOrigin, 0.5 * maxDistanceFromOrigin, 0.5 * maxDistanceFromOrigin)
newObject.set_transform(t)
add_child(newObject)
newObject.set_process(true)
objectCount = objectCount + numberToAdd
else:
print("Failed to load asteroid object scene")
# Called when the node enters the scene tree for the first time.
func _ready():
randomize()
AddObjects()
var mouse_sens = 0.3
var camera_anglev=0
func _input(event):
if event is InputEventMouseMotion:
$Camera.rotate_y(deg2rad(-event.relative.x*mouse_sens))
var changev=-event.relative.y*mouse_sens
if camera_anglev+changev>-50 and camera_anglev+changev<50:
camera_anglev+=changev
$Camera.rotate_x(deg2rad(changev))
Look into using a ‘singleton’ to make functions global, that’s going to be something handy for you to understand, probably in this context.
I think this might be helpful also: https://www.youtube.com/watch?v=iq4eOaox5Oo&t=7s
I’m relatively new to godot and I’m realizing it’s important to create some fully functional super simple games and picking up the concepts along the way as you build more and more complex games.
ryanscott | 2020-05-03 12:35