I need help with player-created objects in-game and syntax

Hi!

I kind of need a hand with some syntax, and/or perhaps a suggestion of how to do this better.

An idea I had was about players that periodically discover spells while they sleep through dream sequences every three or four days. I’m new to SugarCube and coding, and after searching about the place I couldn’t seem to find a solution.

I’ve been wondering as I’ve searched high and low, but was there a way to have an object literal that they could directly create themselves?

An object that then gets placed into an array that they can pull up when it gets to combat/obstacle sections.

I was originally thinking on making a few dozen object literals with empty properties… But I thought I might try first this community about better solutions as they might be more applicable and useful for learning how to use SugarCube properly.

That and there wouldn’t be a hard limit, and the issue may come up again elsewhere.

I’m sort of aiming for a bit of a Morrowind feel with spellcrafting. The ability to select a school of magic, and then at the end of the process using a place macro on a finalization passage, to calculate things like mana cost based on selected params.

So something a bit like this…

<<set $Spellbook = []>>
<<for _i = 0; _i < $Spellbook.length; _i + 1>>
<<capture _i>>
	<<set _i = {
	"name" : "",
	"school" : "",
	"effect" : "",
	"pos" : "",
	"show" : "",
	"mana" : "",
	"description" : ""
}>>
<<set $Spellbook.push( "_i" )>>
<</capture>>
<</for>>

Only … ah, less broken.

Thanks.

It would be memory intensive to store the entire object and properties in an array. You generally want to use the setup object for these spell properties that don’t change.

1 Like

Ah.

So in the storyinit kind of like;

<<setup.positioning = [ "Reach", "Near", "Far" ]>>

… and so on.

And that would be the more efficient way in terms of memory usage?

Thanks!

I was thinking just put a spell object on the setup object that contains all the user inputted details.Also have a string array of the spells to interact with it and fetch the details.

First StoryInit to initialize the spell object (all spells made will be put on the parent spells object). Also I initialize the spellbook array to hold just the string names of made spells.

<<set $spells = {}>>  \ parent object for spells created
<<set $spellbook = []>>

Next, to be put in the Javascript section, is the code we pass so the details are stored on the setup object. I tweaked some code from Hiev I found to make this work.

setup.spellattributes = function (name, school, reach, effect) {
	return {name: name, school: school, reach: reach, effect: effect};
};

Next our Spell Create passage (also start game here). We want to define the options the user can pick. I did 3 barebone properties here just to show you. The 3 temp arrays at the top will be used to populate the listboxes at the bottom that the player can pick. The only choice the user can type is the name. If the name entered is over 2 characters and spell name is not already used, a new spell will be created.

<h2>WELCOME TO SPELLMAKER!</h2>
<<set _spellschool = ["necromancy", "elemental", "restoration"]>>\
<<set _spellreach = ["short", "medium", "far"]>>\
<<set _spelleffect = ["damage", "poison", "heal"]>>\

SELECT SPELL SCHOOL:
<<listbox "_schoolselect" autoselect>>
	<<optionsfrom _spellschool>>
<</listbox>>

SELECT SPELL REACH:
<<listbox "_reachselect" autoselect>>
	<<optionsfrom _spellreach>>
<</listbox>>

SELECT SPELL EFFECT:
<<listbox "_effectselect" autoselect>>
	<<optionsfrom _spelleffect>>
<</listbox>>

NAME SPELL:
<<textbox "_nameselect" "">> 

<<button "Create spell">>
<<if  $spellbook.includes(_nameselect)>>
<<replace #spelltest>>
You already have a spell named this!
<</replace>>
<<elseif _nameselect.length < 3>>\
<<replace #spelltest>>
The spell name must be at least two characters!
<</replace>>
<<else>>
<<set $spells[_nameselect] =  new setup.spellattributes(_nameselect, _schoolselect, _reachselect, _effectselect)>>
<<set $spellbook.push(_nameselect)>>
<<replace #spelltest>>
_nameselect created!
<</replace>>
<</if>>
<</button>>

<span id = "spelltest">
</span>

[[Go to Spellbook|Spellbook]] 

Finally the Spellbook page that shows all your spells in a listbox and when “cast spell” is pushed, the spell details are fetched. I just printed the details out to show its working, but in reality you would want to also put something like this on the combat page and do all the calculations here behind the scenes. Maybe someone far wiser than me can check this work to see if it accomplishes what you want :slight_smile:

SPELLBOOK

<<listbox "_spellselect" autoselect>>
<<optionsfrom $spellbook>>
<</listbox>>

<<button "Cast spell">>   /Do stuff to enemies or self, deduct mana, show a pic, etc...
<<replace #spelldetail>>
<<if _spellselect == "">>
No spell selected!!!
<<else>>
Spell cast: $spells[_spellselect].name
Spell effect: $spells[_spellselect].effect
Spell school: $spells[_spellselect].school
Spell effect: $spells[_spellselect].reach
<</if>>
<</replace>>
<</button>>

<span id = "spelldetail"></span>

[[Go to Spell Create|Spell create]] 
1 Like

Nice nice!

Thank you! I’ll be sure to tinker with it once I get time this upcoming weekend! That is way more organized and freeform than what I was thinking of, and it gives the player way more liberty to create their own tools during gameplay!