Go.get causing silent crash

#1

Source

Hi, I am trying to use go.get() to fetch a property into a factory created object from another factory created object using a url stored in a list. With the go.get() line in place it causes a silent crash with nothing returned to the console, not even any previous print statements.

The attached code should demonstrate this, line 22 of sp.script is the culprit.

I hope you can recreate the issue, if not let me know and I`ll try the code on some other systems.

Cheers,
Giles

0 Likes

#2

You never attached any file!

0 Likes

#3

Click on “Source”.

0 Likes

#4

Ah. I took a look. It’s bad that we crash but there is a reason. This line crashes:

print(go.get(list[math.random(0,listpos-1)], "parentid"))

First some comments:

  • list and listpos are global. It’s usually not recommended to use global variables as it can result in a lot of unwanted side effects etc. In this case it’s not the cause of the problem though.

  • I usually recommend that you break-up hard to read lines of code into several shorter easier to read lines. Like this:

      local list_index = math.random(0,listpos-1)
      local list_value = list[list_index]
      local parent_id = go.get(list_value, "parentid")
      print(parent_id)
    

Now, in the init() function you do:

go.property("parentid", hash(""))

function init(self)
	spawntimer = 0.1
	list[listpos] = go.get_id()
	listpos = listpos + 1
end

So, on the current script you have a script property called parentid. And you’re trying to look that up in your update() function where the crash happens. The thing is though that in init you store:

	list[listpos] = go.get_id()

Where it really should be:

	list[listpos] = go.get_id("#") -- the property is on this script

You could also do like this in init() and store the url:

list[listpos] = msg.url()
0 Likes

#5

Ah, thank you.

The only reason for the one liner is this was quickly put together code to demonstrate the problem.

So, Defold should not crash but the crash is actually being caused by my doing something unexpected.

Is it only go.get_id() that needs the “#” if referring to the same script or does that also apply to the other get_… methods as well?

Thanks for the help, I can move forward now :slight_smile:

0 Likes

#6

It’s for go.get and go.set only since they need to know which component you’re trying to get a property from.

0 Likes