0
Answered

Variable alternates between working and not working

Steph 1 year ago updated by Lazlo Bonin (Lead Developer) 1 year ago 4

Hello

I've just set up Bolt for the first time and I've run into a weird issue. When I press Play I get a "variable not found" error.


If I switch that variable to another variable then back to the original variable (through the Get dropdown), it works. If I stop running the game and then press Play again it works:



The weird thing is, if I don't change anything at all it alternates between
Play -> doesn't error
Play -> works


Bolt Version:
Unity Version:
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):

Answer

+2
Answer

In the first screenshot, I can see in the bottom right that the vmove variable truly doesn't exist on the graph when not in play mode.  This tells me that you haven't initialized the variable ahead of time, but rather are relying on the variable being created at runtime.  This is fine, and fully supported, but it DOES mean that if you get a node that asks for it before it is created in the graph, you'll get this sort of failure.

In this case, you have TWO Update events.  Again, this is fine, and fully supported.  However, it's a complete shot in the dark which Update event will fire first, the top one or the bottom one.  Whichever one does fire will run its control flow to completion, then Bolt will look for another one to fire.  This is because Unity game logic is single-threaded, and can only be doing one thing at once.

When it works, it chose the bottom node first, and sets the variable.  When it doesn't, the top one executed first, which results in the flow asking for the variable before it is defined.


There are four ways to handle this:

  1. Define the variable with a default value in the variables pane on the graph
  2. in the graph inspector for the Get Variable that is giving you trouble, look in the graph inspector for the "Fallback" checkbox.  Tick that and you'll see a new port appear that allows you to provide a literal value for a fallback if the variable doesn't exist (requires Bolt 1.4, this is new to the latest versions).
  3. Provide a manual fallback via Is Variable Defined and Branch, performing some logic if the variable doesn't exist
  4. Restructure your graph so that it doesn't have this race condition, and instead uses 1 event to drive it all (perhaps with a sequence node)
GOOD, I'M SATISFIED
Satisfaction mark by Steph 1 year ago

Hm, that wasn't meant to be submitted yet. :P

Continued:


I'm not sure what I could be doing wrong (this is the first thing I've tried). A screenshot of the Graph:


Would appreciate some pointers.

Thanks!

+2
Answer

In the first screenshot, I can see in the bottom right that the vmove variable truly doesn't exist on the graph when not in play mode.  This tells me that you haven't initialized the variable ahead of time, but rather are relying on the variable being created at runtime.  This is fine, and fully supported, but it DOES mean that if you get a node that asks for it before it is created in the graph, you'll get this sort of failure.

In this case, you have TWO Update events.  Again, this is fine, and fully supported.  However, it's a complete shot in the dark which Update event will fire first, the top one or the bottom one.  Whichever one does fire will run its control flow to completion, then Bolt will look for another one to fire.  This is because Unity game logic is single-threaded, and can only be doing one thing at once.

When it works, it chose the bottom node first, and sets the variable.  When it doesn't, the top one executed first, which results in the flow asking for the variable before it is defined.


There are four ways to handle this:

  1. Define the variable with a default value in the variables pane on the graph
  2. in the graph inspector for the Get Variable that is giving you trouble, look in the graph inspector for the "Fallback" checkbox.  Tick that and you'll see a new port appear that allows you to provide a literal value for a fallback if the variable doesn't exist (requires Bolt 1.4, this is new to the latest versions).
  3. Provide a manual fallback via Is Variable Defined and Branch, performing some logic if the variable doesn't exist
  4. Restructure your graph so that it doesn't have this race condition, and instead uses 1 event to drive it all (perhaps with a sequence node)

Hi! Thanks; the explanations helped. :D

This is what I've got now and it's working fine so far:


Thanks again! I'll carry on fiddling with and breaking stuff. :P