Simulating Objects

Hi people,

I’m still a bit new to this so apologies for dumb questions. ChoiceScript does not, as far as I can tell, support objects.

I found Objects in ChoiceScript but it looks like some form of extension with commands like *define_object_type and *create_object, which I can’t find in the official documentation. The internal link to this code gives a 404 so I can’t follow it further.

Anyway, what I came up with is the following and I’d appreciate any feedback on whether this is a good idea or not.

Sample problem: D&D-type monsters with consistent attributes. The monster might be one of many, so the monster ‘object’ must be generic to avoid code repetition.

What I came up with is:

*create monster
*create wyvern_name “Wally”
*create wyvern_strength 50
*create dragon_name “Dave”
*create dragon_strength 100

*set monster “wyvern”

The rest of my code can now be generic where:

Name of the current monster is: ${{"${monster}" & “_name”}}
Strength of the current monster is: ${{"${monster}" & “_strength”}}

It’s a little clumsy-looking.

Can anyone suggest a better way of doing this?

Thanks,
Stephen

1 Like

You have a good use case there. Unfortunately, I don’t think there’s a straightforward way of doing objects. Your way is as good as any. I would add an index so that I could keep multiple copies of a type of object.

*create class_index_prop "" 


*comment example:
*create dragon_1_name "Wally"
*create dragon_1_healthpoint 500
*create dragon_1_hitpoint 15
*create dragon_1_droploot_name "Ring of Enduring"
*create dragon_1_droploot_buffer "health" 
*create dragon_1_droploot_effect "+5"

*create dragon_2_name "Judas" 
*create dragon_2_healthpoint 250
*create dragon_2_hitpoint 25
...

If you want to simulate class behaviour, create separete scenes with class methods. Such as a dragon.txt and passing the index as reference. I wouldn’t take it too far though. Try replacing OOP for FP whenever possible.

1 Like

You can simplify the above to: ${{monster&"_name"}}.
The second ${} is unnecessary.

For me the hardest part of this has always been that you can’t create variables (and therefore objects) dynamically. So, much like with arrays, you end up having to "*create" a pool of objects/array elements that you can re-use. Which gets messy.

4 Likes

@cup_half_empty
Array, yes. That might be useful, thanks.

I did experiment with making this very generic:
*gosub_scene utils get_attribute “wyvern” “strength”
which works for anything (and you could add an “index” param) but it’s very verbose when you use it all over the place.
Creating separate files for each ‘class’ is probably overkill in my case. ChoiceScript isn’t really cut out for it.
Thanks for your help.

@CJW
${{monster&"_name"}}

Thanks. I got lost in all the referencing/dereferencing :slight_smile:

3 Likes

If you do happen to go all out with this, and come up with a system you think is beneficial and reusable, could you please let us know? I’d likely try and encourage you to let us include it in CSLIB: 🧰 [TOOL] CSLIB - ChoiceScript Library.

Best of luck with your projects :slight_smile:

1 Like

Not sure if I’ll go that far, but you’ve got me thinking about it.

You could certainly do things like:
*gosub_scene func get_by_name “monster” “Wally”

I have about four other projects going at the moment, two with deadlines, so it might not happen for a while…

2 Likes