0 votes

So I'm having an error deemed: Invalid call. Nonexistent function 'randf_range' in base 'Nil'

This game is in development so please only fix this problem

Code:

extends Control

onready var News = get_node("RichTextLabel2")
onready var Stockboard1 = get_node("RichTextLabel3")
onready var Stockboard2 = get_node("RichTextLabel4")
onready var Stockboard3 = get_node("RichTextLabel5")
onready var Stockboard4 = get_node("RichTextLabel6")


onready var eventrandomizer = RandomNumberGenerator.new()
var eventvar = eventrandomizer.randf_range(0, 20)
onready var stockidrandomizer = RandomNumberGenerator.new()
var stockidvar = stockidrandomizer.randf_range(1, 6)
onready var reporterrorrandomizer = RandomNumberGenerator.new()
var reporterrorvar = reporterrorrandomizer.randf_range(3, 7)
var reporterrorvar2 = reporterrorrandomizer.randf_range(8, 12)
var reporterrorvar3 = reporterrorrandomizer.randf_range(13, 18)
var reporterrorvar4 = reporterrorrandomizer.randf_range(35, 67)

var ORANGE_HARVESTING = 5
var NORMALSOFT = 5 
var LOGICPROGRAMMING = 5
var SPINFAN = 5

var currentevent = 0

func _ready():
    updatefunc() 

func updatefunc():
    event_director()

func news_director():
    if currentevent > 0:
        if currentevent == 1:
            print("set newsfeed 1")
            News.text = str("CURRENT NEWS: Stocks expected to drop $", reporterrorvar)
        if currentevent == 2:
            print("set newsfeed 2")
            News.text = str("CURRENT NEWS: Stocks expected to decrease $", reporterrorvar2)
        if currentevent == 3:
            print("set newsfeed 3")
            News.text = str("CURRENT NEWS: Stocks are on a downfall of $", reporterrorvar3)
        if currentevent == 4:
            print("set newsfeed 4")
            News.text = str("CURRENT NEWS: Stocks are crashing with a loss of $", reporterrorvar4)
    else:
        print("set newsfeed N/A")
        News.text("CURRENT NEWS: Market predictions yields no change right now")


func event_director():
    eventrandomizer.randomize()
    if eventvar < 5:
        currentevent = 0
        market_update(0)
    if eventvar > 5:
        if eventvar < 10:
            currentevent = 1
            market_update(-8)
        if eventvar < 15:
            currentevent = 2
            market_update(-12)
        if eventvar < 20:
            currentevent = 3
            market_update(-15)
        if eventvar > 20:
            currentevent = 4
            market_update(-53)

func market_update(marketevent):
    market_price_director(marketevent, stockidvar)


func market_updater():
    Stockboard1.text = str("ORHV: $", ORANGE_HARVESTING)
    Stockboard2.text = str("NOSO: $", NORMALSOFT)
    Stockboard3.text = str("LOPR: $", LOGICPROGRAMMING)
    Stockboard4.text = str("SPFA: $", SPINFAN)


func market_price_director(rate, stockidnum):
    if stockidnum == 1:
        if ORANGE_HARVESTING < rate:
            pass
        else: 
            ORANGE_HARVESTING = ORANGE_HARVESTING - rate
    if stockidnum == 2:
        if NORMALSOFT < rate:
            pass
        else:
            NORMALSOFT = NORMALSOFT - rate
    if stockidnum == 3:
        if LOGICPROGRAMMING < rate:
            pass
        else:
            LOGICPROGRAMMING = LOGICPROGRAMMING - rate
    if stockidnum == 4:
        if SPINFAN < rate:
            pass
        else:
            SPINFAN = SPINFAN - rate

    else: # failsafe measures
        pass

    market_updater()
asked Mar 8 in Engine by Nothing (56 points)

1 Answer

+1 vote
Best answer

All of your rnd-related syntax is correct. The problem is that you're creating the RandomNumberGenerator objects as onready vars, but using them as normal vars - outside of a function.

That'll cause the normal vars to be accessed before the random number objects have been created - leading to your issue.

The solution is to either move the access of the random number objects to a function call (such as _ready) or simply make those vars onready vars also.

So, either this:

onready var eventrandomizer = RandomNumberGenerator.new()
onready var eventvar = eventrandomizer.randf_range(0, 20)

Or this:

onready var eventrandomizer = RandomNumberGenerator.new()
var eventvar

func _ready():
    var eventvar = eventrandomizer.randf_range(0, 20)
answered Mar 8 by jgodfrey (5,248 points)
selected Mar 8 by Nothing
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.