Global ChoiceScript functions! (Mod)

Tired of copy and pasting the same ChoiceScript code in every scene because you have a chunk (or chunks!) common code you need to use throughout your game? Maybe you’ve been shying away from writing a certain ChoiceScript game because of the complexity of the code you would have to copy and paste in order to fulfill your idea. In either case, this mod is for you! Now instead of copy and pasting, you can have all of your common ChoiceScript code in a single file, in this case “choicescript_global.txt” (located in the ‘scenes’ folder), and you can *gosub to them from ANY scene! This mod will, with every scene in your game, automatically load the global code and append it to the current scene.

How to use? Simple! Just put all of the code you need in the file “choicescript_global.txt”, located in the “scenes” folder, then call the code chunks using the ‘*gosub’ command!

If you want to add global CS function capability to your current project, you will need the following:
-In the file “scene.js”, either comment out or replace the function loadScene() with the one from the mod
-Also in the same file, add the function loadGlobalScene() from the mod version of “scene.js” to your game’s version
-In the “scenes” folder, create a file named “choicescript_global.txt”

… After this, you should be able to use global CS functions!

A few things to keep in mind:
-I would refrain from calling to any global functions in the Stats scene, as this may have unexpected results, though I have not tested this to see what would happen
-I wouldn’t use “*goto” to call to a global function, however using “*goto” within a global function itself is fine as long as you remember to eventually use “*return” to branch back to the main scene
-Make sure that you name all of your global CS functions in a unique manner, perhaps with a prefix of “g_” or the like, else if there is another label with the same name in any of your normal scene files, you will get an error
-Though I am a fairly skilled programmer, this is my first time working with JavaScript, so there may be unknown errors and I may not have crafted this mod as efficiently as it could be

If you have any questions or find any errors/bugs, or if you have comments or suggestions, please let me know. Until then, happy ChoiceScripting!

Download Link

Glad to see this put out there, I can confirm however, that using it in the stat-scene(s) is not an issue. It functions correctly.

Essentially they’re just a seperate scene file, loaded by a button rather than a goto_scene or finish command.

Side note: Incase anyone gets any ideas I’d personally advise AGAINST trying to make multiple global scene files (although it’s entirely plausible) in the same way one should consider it a global access file, not a code dump. Remember your game will constantly load your current + the global scene, so if you shove it chock full of stuff it may affect performance.

Thank you for adding the side note! I neglected to mention that in my initial post, as I was in a bit of a rush to finish before the end of my lunch break.

Hey no problem and you know I said you don’t need to define that variable? Turns out one browser disagrees, yup… The one… The only… The infamous:
Internet Explorer.

So you do need to define that, somewhere. :’(

Actually I’m having a lot of problems with IE in general, it doesn’t seem to always load the global scene before running the current scene and so results in bad label errors. Might need to devise a way to ensure it’s loaded before parsing any c/s.

Thanks, @stainedofmind, your code is working well for me. I tweaked it so as not to modify the scene.js file, putting the new code in a globalscene.js file instead, which I then load into index.html after the scene.js file is loaded. I don’t like modifying any of the official CoG JS files if I can help it. I then moved the globalScene variable from the mygame.js file into the globalscene.js file, placing it in the Scene namespace.

@CJW, so far it’s working in IE9 for me.

1 Like

Yeah, it was the result of an if j/s function I was using, I’ve removed that and not had any additional problems so far…Fingers crossed it stays that way.

Actually I still get it on occassion on my phone, but a refresh usually sorts it. I’m not sure what’s up with that.

Glad everything is working so far for everyone. When my schedule settles down a little and I get a little more time to myself, I’ll probably make similar changes to the ones eposic made. I like the idea of having all the code separate from the official files as well. Easier to manage that way. The copy I posted was basically my working copy which I stopped modifying the moment I had the desired results, so it’s pretty much a hack job :stuck_out_tongue:

BTW, another way to get global functionality in your games is to create JS functions in a file that you include in index.html and then call those JS functions from a *script command in your CS. The problems with this approach are that you have to know JS, your JS has to work on all browsers, you have to access your game stats differently, and communication back to CS has to be through your game stat variables. But it’s a lot easier to program certain logic in JS than CS.

I don’t know how/if either of these approaches will work in native apps on mobile devices, if you’re considering a commercial license. But it shouldn’t be too difficult to append the contents of choicescript_global.txt to all your scene files if necessary, whereas I don’t know how easy it would be to convert a game to a commercial native app if it contains a lot of custom JS. If you’re only worried about games working on the web, both approaches can be of value.

Hmm. Decisions, decisions.

It’s at times like this when I really wish one or more of CoG’s own devs could spare the time to be more involved on the forums here, if only to comment on the deeper “Game Development” threads like this one.

Subjects like global functionality, extra buttons / stats screens and even Save / Restore features could all really use some overall guidance to avoid shooting ourselves in the foot somewhere down the line (and not finding out about it until it’s too late).

1 Like

As far as I’m aware it’s actually only Dan Fabulich who deals with the interpreter, at least currently. Most of the “writers” probably have no more of an idea than we do.

But it’s all javascript - html - You can’t really “go” wrong, I’ve messed around quite a bit and haven’t managed to break anything yet, the only thing I’ll tell you to keep in mind is cross compatibility. The more you change and add things, the more likely something, somewhere will break. Mobile phones and IE particularly don’t tend to play nice when you begin modifying the interpreter’s code.

@Epsoic - That defeats the purpose of C/S - You may as well make a custom web app from scratch. You’re right of course, but the whole point behind C/S is that non-programmers should be able to make games out of their stories.

However as long as your j/s works in most browsers, you’ll be ok porting it to apps. I know android and iPhone both support “web-based applications” - meaning you can make an app using web code, you don’t need java knowledge or anything.

1 Like

@CJW, there are some things that CS does not handle well. I understand that one intent for CS is to enable non-programmers to make games out of their stories. But CS is a good framework to use even for programmers. If I were not worried about converting my games to commercial native apps, including for the Kindle, I’d not be concerned in the slightest about using all sorts of custom JS in my CS games.

My primary reason for using CS is as an avenue to converting my games to native apps on iOS, Android, and Kindle. I don’t want to worry about all the extra trappings of the various devices. CS games run on these devices, so if I use CS, my games can theoretically be converted to native apps. But JS is much easier to use for certain things than CS is. So I’d like to know what I can do in JS without destroying any chance of my games being converted to native apps. I figure that mucking with the interface from JS won’t easily convert, but calculations and data management might.

I’ve sent email to Dan Fabulich with technical questions regarding coding practices with a commercial license in mind. I’ve yet to receive any useful technical replies. I’m involved now in a discussion with Adam Morse about a commercial license, and I’m hoping this will encourage Dan to answer my technical questions.

1 Like

Who’d you email about that? I tried to coax some talk on commercial licenses and percentage of royalties, but have yet to receive a reply (about a week ago).

You needn’t worry about most javascript working on mobile devices. Think of the websites people visit on their phones and how many of them use j/s and what type of j/s that may be. Your only concern should be taxing the hardware, and even that will prove hard in a text based game. My only concern would be your kindle. I have no knowledge of them, bar knowing that their browser is pretty basic (and hence their j/s support may be too).

But that’s my point, using purely j/s functions instead of c/s will likely ruin it’s native cross-compatibility, essentially shooting yourself in the foot.

The more you hack the c/s core, the less point there is in using it, that’s the way I see it.

My fiddling is living proof of this. With every j/s change I make, something tends to break (usually in IE).

1 Like

We make no effort to make IE work with demo CS. Unfortunately for me, nor Chrome. Firefox is the only browser we make sure works.

@CJW, I emailed the CoG support email address, and Adam Morse responded regarding licenses. If you send a support email and address it to Adam, it should be redirected appropriately. I think Dan stays busy with the technical side of things, and Adam handles the legal side of things.

And you’re right about the JS ruining the native cross-compatibility with Kindle; Dan sent me a reply on that question, as I noted in the Kindle Considerations thread.

Thanks! I’ll get on that tomorrow, I think I may have replied to one of Jason’s emails, so it might not have gone through.

Yeah that really sucks, but it wasn’t unexpected. What do you reckon that means for you? Less customizing, dropping kindle or taking the extra time?

Modified my code today to remove the changes from the original “scene.js” and it works perfectly. I wasn’t aware that one could override functions in JavaScript. I have to say, so far from the little bit I’ve meddled with JS, I really like how user friendly and flexible it is. I think I might give some serious consideration to learning it more indepth when I have some free time.

As cool and useful as this is, I’m really starting to get a lot of cases where it’s not loading before labels are called, I don’t know if this is due to stress, or where I’m calling labels or what not, but it’s something people should probably be aware of, even if they’re not currently getting them.

It seems to happen in most browsers, Chrome in IE9 for sure.
It’s not every time; it’s totally random, like sometimes it manages to load first, sometimes it doesn’t.

@CJW, I’ll take the extra time and avoid using JS except for those sorts of things that are only of benefit on the web. I’ll still make use of the global CS code mechanism that @stainedofmind came up with for development purposes, and append the contents of the global scene file to the other scene files when it comes time, assuming the global scene code works fine for development purposes in Firefox.

@stainedofmind, JS is user friendly until you start worrying about cross-browser compatibility. You can use some third-party code, such as jQuery, to help with that issue, but I still haven’t heard from CoG about the legalities of using jQuery code with the non-commercial CS license. For any JS code to be of use to the CS game development community, it would need to be compatible with the CoG non-commercial license.