0 votes
by (430 points)
closed by

I'm making a game where you can set up different kind of buildings. I already have the basics of this system like limitations when it comes to the number of workers needed to start the construction or the money you must have in your wallet to make it. But from there I had a lot of problems with the count down. I mean, some buidlings may take 40 days to be finished while others can be built in differents amounts of time. When I try to set this variables there is no problem:

<<set $construcciones = 0>>  <---- amount of constructions
<<set $dias1 = 0>> <---- time it takes to finish a building
<<set $investigaciondias to false>>  <---- future feature that will allow the player to build 2 building at the same time, it requires research
<<set $dias2 = 0>> <---- this is the second amount of time



<<set $fortaleza to false>> <---- this variable will be set to true once the build is finished
<<set $fortalezac to false>> <---- this variable will be set to true while the building is under construction. Once finished (when the days are set to 0) this variable will return to be false
<<set $fortalezad = 3>> <--- time it takes to finish the building

But when I want the game to change a building under construction to its finished state it simply does not work.  Any ideas to make it work, or to improve the system? Thank you in advance.

closed with the note: Question answered

2 Answers

+1 vote
by (159k points)
selected by
 
Best answer

1. You have a number of variables that are related to the same item and you are using a variable naming convention to group sets of those variables together, this can quickly lead to confusion & mistakes.

Another method you can use to group related variables together is to use an Javascript Generic Object, the following basic example show what such an object could look like. It includes a <<set>> macro defining the values of the object and its properties, some <<print>> related macros to show how to output the values of the object's properties, and finally how to compare two object properties using an <<if>> macro.

<<set $building to {
	"max-workers": 2,
	"cost": 100,
	"build-days": 10,
	"days-elapsed": 0
}>>

Maximum Workers allowed: <<= $building["max-workers"]>>
Amount of money needed to build: <<= $building["cost"]>>
Number of days needed to build it: <<= $building["build-days"]>>

Number of construction days that have passed: <<= $building["days-elapsed"]>>

Has construction of the building finished? \
<<if $building["days-elapsed"] is $building["build-days"]>>\
Yes\
<<else>>\
No\
<</if>>

... obviously you would use property names that made sense to your story project. 

2. You are also using numbers in your variable names to distinguish one set of related variables from another, and this can also to confusion & mistakes.

Another way to do this is to use a collection object type (like Array, Map, Generic Object, etc) to group your "building" objects together.

2a. The following basic example shows how to initially define an Array to hold two instances of the "building" object, how to later add a third "building" object to that Array, and how to reference the properties of each of the "building" objects with the array.

<<set $buildings to [
	{
	"max-workers": 2,
	"cost": 100,
	"build-days": 10,
	"days-elapsed": 0
	},
	{
	"max-workers": 3,
	"cost": 80,
	"build-days": 6,
	"days-elapsed": 2
	}	
]>>
<<set $buildings.push({
	"max-workers": 1,
	"cost": 10,
	"build-days": 2,
	"days-elapsed": 1
})>>

Building 1 - Maximum Workers allowed: <<= $buildings[0]["max-workers"]>>
Building 2 - Maximum Workers allowed: <<= $buildings[1]["max-workers"]>>
Building 3 - Maximum Workers allowed: <<= $buildings[2]["max-workers"]>>

... you will notice that the first element of the Array is referenced using an index of 0 (zero) and that the second element is referenced using an index of 1 (one), this is because Javascript Arrays are zero-based.

2b. The following example is similar to 2a except it is using an Generic Object instead of an Array to contain the different "building" objects,

<<set $buildings to {
	"first": {
		"max-workers": 2,
		"cost": 100,
		"build-days": 10,
		"days-elapsed": 0
	},
	"second": {
		"max-workers": 3,
		"cost": 80,
		"build-days": 6,
		"days-elapsed": 2
	}
}>>
<<set $buildings["third"] to {
	"max-workers": 1,
	"cost": 10,
	"build-days": 2,
	"days-elapsed": 1
}>>

First Building - Maximum Workers: <<= $buildings["first"]["max-workers"]>>
Second Building - Maximum Workers: <<= $buildings["second"]["max-workers"]>>
Third Building - Maximum Workers: <<= $buildings["third"]["max-workers"]>>

WARNING: The above examples are only very basic overviews of on how to use the Generic Object and Array data types to achieve what you want.

by (63.1k points)
@AlexCLD,

Further suggestion, you may want to use the PassageReady special passage to increment the time until building completion, and "add" a new building when it's been completed. Without seeing how that aspect of your code works, I can't really give any specifics, though. Just know that PassageReady fires it's code silently as the next passage is rendered, and is great for time-keeping and upkeep like this.
by (430 points)

Thanks for all the information! Sorry about not answering before, but I have been a bit busy since then. I will show you all the code that I'm using right now to build my system because it seems like It is better to do it this way and not hiding even the smallest amount of code:

 

First of all, I have a panel where the player can choose the buildings he or she wants to add to the kingdom:

 

<<if $money >= 2500 and $buildings == 0 and $workers >= 5>> [[Construir una fortaleza (2500 doblones)|fortaleza]]<<endif>>

<<if $money >= 1800 and $buildings == 0 and $workers >= 15>> [[Construir una iglesia (1800 doblones)|iglesia]]<<endif>>

 

The player needs to have all that stuff before starting anything. And when he select a builiding this is what happens:

<<set $fortressconstruction to true>>
<<set $buildings to +=1>>
<<set $days to 40>>
<<goto "construccion">>

 

So in other pannel the player can see the amount of time needed to finish the building and the fact that the builiding is still under construction. However, I had many problems when the days value change to "0" because the code I used seemed to be useless:

Bla, bla, bla. Story goes here. Bla, bla, bla.

Changing passage.

<<set $days -=40>>
<<if $days == 0>> <<set $fortressconstruction to false>> <<set $fortress to true>> <<set $buildings to 0>>
<<endif>>

 

What happens is that the fortress continue to be under construction even when the "days" variable was set to 0. It does not even show the fact that the build was finished:

The kingdom has the following improvements:
<<nobr>>
<<if $fortress>>A <b>fortresst</b>.<<endif>>

Even when the code can't be simplified sad

 

Sorry for my bad english, as you were able to see, it is not my native language. Thanks alot for your help smiley

by (159k points)

The first thing I would try to is change the condition being used to determine if construction has finished:

<<if $days <= 0>>\
	<<set $fortressconstruction to false>>\
	<<set $fortress to true>>\
	<<set $buildings to 0>>\
	<<set $days to 0>>\
<</if>>

... to handle the potential case of the $days value somehow going below zero.

The other potential issue with the original of the above condition is that once $days reaches zero the related code keeps being executed even when there are no building being built. I would suggest change the condition so that it also checks if there are buildings being built.

<<if $buildings > 0 and $days <= 0>>\
	<<set $fortressconstruction to false>>\
	<<set $fortress to true>>\
	<<set $buildings to 0>>\
	<<set $days to 0>>\
<</if>>

Other than that it is difficult to debug a system without having access to that system, if you were willing to somehow make a copy of your story HTML file available then I'm sure one of the community members will be willing to spend time trying to determine what may be happening.

by (430 points)
Thanks for the quick answer! :D I will try the new code and tell you if it is working now.
by (430 points)
Finally I was able to implement this kind of system, so your code worked without problems. Thank you very much indeed, you saved my life :')
0 votes
by (430 points)
Any ideas about it?
...