# Code for Objects in CS?

**URL:** <https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539>\
**Category:** Game Development\
**Created:** [July 9, 2017, 6:04am UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539 "2017-07-09T06:04:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![AidanG](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/aidang/32/33024_2.png) [@AidanG](https://forum.choiceofgames.com/u/AidanG)\
**Post date:** [July 9, 2017, 6:04am UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/1 "2017-07-09T06:04:56Z")

</div>

I see that several people have created object-oriented add ins for CS, but the code for all of them is gone. Does anyone have copies of any of these, code that allows for objects or classes, or a workaround that doesn’t involve 50 lines per object?

---

<div class="post-metadata">

**Author:** ![Szaal](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/szaal/32/112202_2.png) [@Szaal](https://forum.choiceofgames.com/u/Szaal)\
**Post date:** [July 9, 2017, 6:48am UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/2 "2017-07-09T06:48:51Z")

</div>

Umm… sorry, as I’m actually not an expert in coding, but what do you mean with objects/classes?

The only add-ins/add-ons/plug-ins/mods I know for CScript are fading your story’s background to another color, a save plugin, and “dedicated writing tools” for CScript.

There’s also a “prefab” or “blueprint” [inventory system](https://forum.choiceofgames.com/t/resource-please-break-test-makeuseof-my-simpler-inventory-system/18123) by @CitizenShawn  
Is that 👆 what you mean?

---

<div class="post-metadata">

**Author:** ![AidanG](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/aidang/32/33024_2.png) [@AidanG](https://forum.choiceofgames.com/u/AidanG)\
**Post date:** [July 9, 2017, 7:28am UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/3 "2017-07-09T07:28:58Z")

</div>

Sort of. More like this:

> [@Objects in ChoiceScript](https://forum.choiceofgames.com/t/objects-in-choicescript/661/10):
>
> Thanks for clarifying Eposic, after looking at it more closely I do believe, to some degree, this may also help us get around choicescript not supporting arrays. In my case in-particular, that will help to no end. I’ll experiment and get back to you on any pros/cons I uncover, but damn man, fine work as always. If I use it you will most certainly get due credit slight_smile EDIT: One thing I did notice is that I can’t give two characters “The Great Terbutje”, but I can give two Characters a …

---

<div class="post-metadata">

**Author:** ![Szaal](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/szaal/32/112202_2.png) [@Szaal](https://forum.choiceofgames.com/u/Szaal)\
**Post date:** [July 9, 2017, 8:14am UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/4 "2017-07-09T08:14:34Z")

</div>

Oh, dayum. I know what you’re looking for, kind of.

It’s quite old stuff, as I see it. Even I barely heard of CoG or similar thing yet, at that time.

I’m not sure if anyone has the copy of it nor even raised similar topic as I recall,  
but let me summon @CJW.

* * *

Do you still have the copy?

---

<div class="post-metadata">

**Author:** ![CJW](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/cjw/32/111787_2.png) [@CJW](https://forum.choiceofgames.com/u/CJW)\
**Post date:** [July 9, 2017, 10:25am UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/5 "2017-07-09T10:25:23Z")

</div>

Even if I did, I wouldn’t advise using it. The original author is long gone, and it may not even work properly with the current version of CS.

Recent CS developments have made this sort of thing a lot easier though, so I’d recommend sticking with vanilla CS if you can (or roll your own thing).

If anyone is interested I’d be happy to produce a tutorial/guide on using an OO paradigm in CS. I’ve been meaning to do so for a while, actually.

---

<div class="post-metadata">

**Author:** ![AidanG](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/aidang/32/33024_2.png) [@AidanG](https://forum.choiceofgames.com/u/AidanG)\
**Post date:** [July 9, 2017, 4:43pm UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/6 "2017-07-09T16:43:47Z")

</div>

I absolutely would be interested, yes please! I do not look forward to trying to mimic random characters and encounters with 387463829 variables (random-character-837-name, random-character-837-strength, … ). There are ways around this, but OO would be sooooo much easier. In particular, some way to mimic an entity/component system would be awesome…

---

<div class="post-metadata">

**Author:** ![RETowers](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/retowers/32/21477_2.png) [@RETowers](https://forum.choiceofgames.com/u/RETowers)\
**Post date:** [July 9, 2017, 5:18pm UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/7 "2017-07-09T17:18:25Z")

</div>

But are you preserving that data? Why not just reuse the variables?

---

<div class="post-metadata">

**Author:** ![AidanG](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/aidang/32/33024_2.png) [@AidanG](https://forum.choiceofgames.com/u/AidanG)\
**Post date:** [July 9, 2017, 5:41pm UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/8 "2017-07-09T17:41:47Z")

</div>

Reuse the variables yes, when possible. It’s not always possible though, and a pain to create 5 unique random characters with 25 traits each using this method. One at a time is fine with reusing the variables and a generation subroutine, but… sometimes you need more than 1 at a time. Sometimes you need 20! Or more!

That was supposed to be an amusing example, though, not an absolutely accurate one.

---

<div class="post-metadata">

**Author:** ![CJW](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/cjw/32/111787_2.png) [@CJW](https://forum.choiceofgames.com/u/CJW)\
**Post date:** [July 9, 2017, 8:01pm UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/9 "2017-07-09T20:01:15Z")

</div>

I mean, there are always going to be certain limitations. Creating xxx variables is always difficult (I’m still holding out for a \*create\_array command!). The only way you can streamline that sort of thing is a generation script.

Loops and array like syntax/clever naming will allow you to reference/handle multiple “objects” simultaneously.

EDIT: OO should allow you model ‘real-world’ “objects” and behaviour, in a more logical and efficient manner, but it’s not (inherently) meant to be a way to support handling a larger _quantity_ of data. I.e. is this really the right tool for the job?

---

<div class="post-metadata">

**Author:** ![AidanG](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/aidang/32/33024_2.png) [@AidanG](https://forum.choiceofgames.com/u/AidanG)\
**Post date:** [July 9, 2017, 8:23pm UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/10 "2017-07-09T20:23:56Z")

</div>

If you can explain how to use clever naming, I’m all for it. I don’t the think objects are the only way, just the most obvious. If I can use clever naming and still only use 25 lines, instead of 125…

---

<div class="post-metadata">

**Author:** ![CJW](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/cjw/32/111787_2.png) [@CJW](https://forum.choiceofgames.com/u/CJW)\
**Post date:** [July 9, 2017, 8:26pm UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/11 "2017-07-09T20:26:15Z")

</div>

Can you give me an example of exactly what you want to achieve?

---

<div class="post-metadata">

**Author:** ![Carlos.R](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/carlos.r/32/75837_2.png) [@Carlos.R](https://forum.choiceofgames.com/u/Carlos.R)\
**Post date:** [July 9, 2017, 8:35pm UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/12 "2017-07-09T20:35:28Z")

</div>

How about something like this?

```choicescript
*temp prefixcount 1
*temp suffixcount 1
*temp swordcount 1
*temp sword @{swordcount Sword|Broadsword|Greatsword|Katana}
*temp prefix @{prefixcount Holy|Unholy|Icy|Flaming}
*temp suffix @{suffixcount of Speed|of Strength|of Stamina|of Leeching}

```

Should be able to do a lot with this basic model, I think?  
Hope this helps.

Edit: “Multireplace” is introduced here:

> [@New features in ChoiceScript: change text size/color, hyphen spacing, @{} variable replacement](https://forum.choiceofgames.com/t/new-features-in-choicescript-change-text-size-color-hyphen-spacing-variable-replacement/26217):
>
> In the latest version of ChoiceScript available on github, there are new features for you to try. Change text size/color. You can now change the size and color of text, [as we announced a few days ago](https://forum.choiceofgames.com/t/change-the-color-and-size-of-our-text/26128). There’s a new “Settings” button which exposes the new options. Multireplace. There’s a new way to insert text based on variables, @{}. (I’m calling it “multireplace” for now, but maybe somebody else will come up with a better name for it.) It’s a little bit like ${} replacement, but you gi…

---

<div class="post-metadata">

**Author:** ![AidanG](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/aidang/32/33024_2.png) [@AidanG](https://forum.choiceofgames.com/u/AidanG)\
**Post date:** [July 9, 2017, 8:57pm UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/13 "2017-07-09T20:57:14Z")

</div>

I’m building an rpg like game, never ending… well, unless you die.

There are random encounters and battle, with random people, some of whom can be recruited. I can create a generation script, but… you can fight more than one at a time. I’d need to create create multiples, which can be done without an object / array / etc, but… that’s 125 lines for 5 characters with 25 traits / stats / items each. I can reuse a single set of variables, if it’s always one on one, but… that’s not the goal/desire. Also, if I recruit one of these random characters, it’s 25 lines just to copy to one of the empty party slots.

A couple lines with an object would be sooooo much easier.

Is that a clear enough example? I can elaborate yet more, but I don’t know that it would really help…

---

<div class="post-metadata">

**Author:** ![AidanG](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/aidang/32/33024_2.png) [@AidanG](https://forum.choiceofgames.com/u/AidanG)\
**Post date:** [July 9, 2017, 9:11pm UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/14 "2017-07-09T21:11:20Z")

</div>

CThanks Carlos, but that’s just a method for assigning random values to a variable. Do that 25 times for a character, and then do THAT 5 times for 5 random characters, and you can see what I’m trying to avoid. Consider a much simpler variable scheme…

Char1\_name  
Char1\_element  
Char2\_name  
Char2\_element  
Char3\_name  
Etc.

I have to set every variable for every character, instead of using one object that automatically generates the random values for the object. Make sense?

---

<div class="post-metadata">

**Author:** ![Carlos.R](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/carlos.r/32/75837_2.png) [@Carlos.R](https://forum.choiceofgames.com/u/Carlos.R)\
**Post date:** [July 9, 2017, 9:58pm UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/15 "2017-07-09T21:58:41Z")

</div>

Ok, but why not just make loops and tell it to quit the loop when certain conditions have been met?

---

<div class="post-metadata">

**Author:** ![AidanG](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/aidang/32/33024_2.png) [@AidanG](https://forum.choiceofgames.com/u/AidanG)\
**Post date:** [July 9, 2017, 11:00pm UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/16 "2017-07-09T23:00:49Z")

</div>

If I could get the loop into the variable name some how (but I don’t see how… I’d be surprised, since very few languages allow dynamic variable names… that’s what objects and arrays are for)…

Think of it like this…

Loop until… (or whatever the syntax is for loops… haven’t needed it yet)

\*set char1\_name Fred

And on…

But for next character, still have to explicitly say char2\_name. The loop does nothing for me. I’d still have to explicitly set every variable.

---

<div class="post-metadata">

**Author:** ![CJW](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/cjw/32/111787_2.png) [@CJW](https://forum.choiceofgames.com/u/CJW)\
**Post date:** [July 9, 2017, 11:06pm UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/17 "2017-07-09T23:06:59Z")

</div>

CS has an “array” syntax:

```
*set n "1"
*set charname[n] value
*comment same as *set charname_1 value
```

---

<div class="post-metadata">

**Author:** ![AidanG](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/aidang/32/33024_2.png) [@AidanG](https://forum.choiceofgames.com/u/AidanG)\
**Post date:** [July 9, 2017, 11:11pm UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/18 "2017-07-09T23:11:28Z")

</div>

Woot! That’s all I need then.

Is there proper documentation somewhere? I thought there was no array anything (hence the wishing for a \*create\_array command.

---

<div class="post-metadata">

**Author:** ![CJW](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/cjw/32/111787_2.png) [@CJW](https://forum.choiceofgames.com/u/CJW)\
**Post date:** [July 9, 2017, 11:13pm UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/19 "2017-07-09T23:13:32Z")

</div>

> [@New ChoiceScript features for programmers](https://forum.choiceofgames.com/t/new-choicescript-features-for-programmers/8423):
>
> In the latest version of ChoiceScript, new features are available for programmers. They’re documented in the [Advanced ChoiceScript](https://www.choiceofgames.com/make-your-own-games/choicescript-advanced/) section of the documentation, which means that you probably don’t need to use them. (I like to discourage people from getting too fancy in ChoiceScript games, because adding complexity rarely adds to the fun of the game.) Curly braces now replace \*setref and \*gotoref. For years, you’ve been able to use curly braces {} around variable names to return values by refe…

---

<div class="post-metadata">

**Author:** ![Nonvita](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/nonvita/32/29708_2.png) [@Nonvita](https://forum.choiceofgames.com/u/Nonvita)\
**Post date:** [July 10, 2017, 2:07am UTC](https://forum.choiceofgames.com/t/code-for-objects-in-cs/28539/20 "2017-07-10T02:07:12Z")

</div>

You can also combine the array syntax with curly braces {} (just gonna copy/paste some code from a game I was working on before getting sidetracked):

```
*label cost_disp_calcs
*temp n_1 "tax_inc"
*temp n_2 "perm_inc"
*temp n_3 "maint_cost"
*temp n_4 "city_funds"
*temp loopn 0
*temp n "n"
*temp thou "_thou"
*temp hun "_hun"
*temp p "_p"

*label calc_loop
*set loopn (loopn + 1)
*set {n[loopn]&thou} (({n[loopn]} - ({n[loopn]} modulo 1000)) / 1000)
*if ({n[loopn]&thou} > 0)
	*set {n[loopn]&p} true
*if ({n[loopn]&thou} = 0)
	*set {n[loopn]&p} false
	*set {n[loopn]&thou} ""
*if ({n[loopn]} modulo 1000) = 0
	*set {n[loopn]&hun} 000
*if (({n[loopn]} modulo 1000) > 0) and (({n[loopn]} modulo 1000) < 10)
	*set {n[loopn]&hun} ("00"&({n[loopn]} modulo 1000))
*if (({n[loopn]} modulo 1000) > 9) and (({n[loopn]} modulo 1000) < 100)
	*set {n[loopn]&hun} ("0"&({n[loopn]} modulo 1000))
*if ({n[loopn]} modulo 1000) > 99
	*set {n[loopn]&hun} ({n[loopn]} modulo 1000)
*if (loopn = 4)
	*if not(started)
		*goto dialogue_start
	*else
		*return
*goto calc_loop

```

So the point of this loop was literally just to make the display of large numbers look pretty (13,500 instead of 13000) across several variables…

So, for example, {n[loopn]&thou} would set the variables “tax\_inc\_thou”, “perm\_inc\_thou”, “maint\_cost\_thou”, and “city\_funds\_thou”.
