Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global shared RuntimeVar storage? #49

Open
BrodyB opened this issue Jul 25, 2017 · 2 comments
Open

Global shared RuntimeVar storage? #49

BrodyB opened this issue Jul 25, 2017 · 2 comments
Labels

Comments

@BrodyB
Copy link

BrodyB commented Jul 25, 2017

Something I'm trying to figure out how to implement is a way to have multiple Cradle stories get/set StoryVars from a shared location. This way, Cradle stories can share information with one another, or I can have scripts in my Unity scene listening to variables that a number of stories are affecting.

My initial thought is to have a singleton GlobalRuntimeVars class and to add <<setGlobal $variable = "something">> type setter/getter commands to access those shared variables.

So my question is: is adding those getter/setter macros easy to pry in with the current code base and where would I do that? Or is there a simpler way to go about this?

Thanks!

@daterre
Copy link
Owner

daterre commented Jul 26, 2017

A macro could work but unfortunately not as natively as yoursetGlobal example. Something like this:
<<setGlobal "someVar" "something">>
<<getGlobal "someVar">>

The easiest way to implement this would be via runtime macros:

[RuntimeMacro]
public void setGlobal(string globalVarName, StoryVar value)
{
	// Some static class or singleton
	GlobalVariables[globalVarName] = value;
}

[RuntimeMacro]
public StoryVar getGlobal(string globalVarName)
{
	return GlobalVariables[globalVarName];
}

Because you're using SugarCube, which doesn't have the concept of nesting macros, you'd probably need to use GlobalVariables directly if you need it in an expression, e.g.:

<<print "You have " + GlobalVariables["someVar"]>>

A better option would be to make a sync script that listens for standard variables' value changes and propagates them to other stories. You could do this in an update loop and compare each variable in Story.Vars to a list you saved in the previous frame (a bit tedious).

Actually, I might add an event to the Story object, sort of like this: Story.OnVariableChanged(Story story, string varName, StoryVar newValue, StoryVar previousValue). You could then register a single handler for all your stories and propagate the value to all of them.

@BrodyB
Copy link
Author

BrodyB commented Jul 26, 2017

Ah, yeah. I think I was moving away from that because I didn't think runtime macros received StoryVars, but rather what they cast down to? I'll give it a shot.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants