Sugarcube 2.36.1 , Trying to make a fundamental game mechanic work

This may be a bit of a nightmare to explain, but i decided since i’ve been lurking on this specific site for maybe a month and a half that i should just kinda actually ask a question or two and see if people have answers.

I’m working on using Sugarcube to make a playable game, since the engine is kinda incredibly easy for one to understand and pick up, (I should know, I only started work on messing with this stuff probably about 2 months ago. wild.) Basically, It’s a dungeon crawler experience where you control around 5 characters who go on quests which you chose. however i’m running into a problem where the end of day cycle i created which then explains what happened does not actively affect ALL adventerer’s, it only interacts with the first one, and even then its semi rare.
Here’s a code snipit

<<set $fordl2 to $fordl + random(1,10)>>
<<set $daft +random(1,10)>>
<<if $daft gte $fordl2>> you win!!! <<set $rep to $rep + random(1,5)>> Because of this you gained Rep with the town. <<else>> you lose!!! <</if>>
gold xp
<<for $forew to 0; $forew lt $fordl; $forew++>>
<<set $foreodint to random(1,5)>>
<<if $Zg.team is "forest" && $foreodint is 1>> 
$Zg.Name ended up finding gold, and gaining experience.
<<set $gold to $gold + random(1,5)>> <<set $Zg.xp to $Zg.xp + random(1,5)>> 
<<else>> <<set $foreodint++>> <</if>>

<<if $Pg.team is "forest" && $foreodint is 2>> 
$Pg.Name ended up finding gold, and gaining experience.
<<set $gold to $gold + random(1,5)>> <<set $Pg.xp to $Pg.xp + random(1,5)>> 
<<else>><<set $foreodint++>> <</if>>

<<if $Kg.team is "forest" && $foreodint is 3>> 
$Kg.Name ended up finding gold, and gaining experience.
<<set $gold to $gold + random(1,5)>> <<set $Kg.xp to $Kg.xp + random(1,5)>> 
<<else>> <<set $foreodint++>> <</if>>

<<if $Lg.team is "forest" && $foreodint is 4>> 
$Lg.Name ended up finding gold, and gaining experience.
<<set $gold to $gold + random(1,5)>> <<set $Lg.xp to $Lg.xp + random(1,5)>> 
<<else>> <<set $foreodint++>> <</if>>

<<if $Hg.team is "forest" && $foreodint is 5>> 
$Hg.Name ended up finding gold, and gaining experience.
<<set $gold to $gold + random(1,5)>> <<set $Hg.xp to $Hg.xp + random(1,5)>> 
<<else>> <<set $foreodint++>> <</if>>

<</for>>

ALL of these have coresponding characters they’re interacting with, but here’s the object code for just 1.

<<set $Zg = {
	Name : "Z",
	name :  "z",
	class :"Thief",
    lvl : 1,
	gld : "1", 
	xp : 10,
    hp : 10,
    mayor : "false",
    team : "false",
    height : "6'8"
}>>

On top of that the code for the events are pretty simple but,

<<set $fordl2 to $fordl + random(1,10)>>
<<set $daft +random(1,10)>>
$daft refers to the value of adventurers level’s combined together.
$fordl refers to the area’s level of difficulty,
Basically, $daft and $fordl fight to have a higher value, which determines if they win or lose.
Then, $fordl (the area difficulty level) is put into a for loop up to its value, to give the adventurer’s rewards. . … BUT THIS IS WHERE IM HAVING A PROBLEM. as it only interacts with the first one, and even then its semi rare.

What am i doing wrong?

this won’t work but this: <<set $daft += random(1,10)>> or <<set $daft to random(1,10)>> should.
See the Documentation for all the way you can set a variable.

Another question is : is $fordl set anywhere? (cause it is not included in your shared code)

it is. It’s just not particularly the problem child or something i believe is causing the problem. For what i know. it’s basically just a set value that shouldn’t affect the outcome too much besides determining if it should just loop it twice or so more. $fordl is a determined value that happens 1 - 2 passages before this, and is an unavoidable requirement for this passage to happen.

does that mean this doesnt work???

should <<set $daft +random(1,10>> be <<set $daft to $daft + random(1,10)>> or does that not work either??

No that one is correct. You can set a variable as the [choose math[ of values of different variable.
It is only for this one <<set $daft +random(1,10)>> that it was an issue.
As the Documentation shows both:
<<set $daft += random(1,10)>> and <<set $daft to $daft + random(1,10)>> will work.

(when it doubt about what operator to use, check the documentation :wink: )

to is a different name for = so <<set $fordl2 to $fordl + random(1,10)>> is the same as <<set $fordl2 = $fordl + random(1,10)>>

And then += is a shortcut for setting a variable to itself plus something else, so <<set $daft += random(1,10)>> is short for <<set $daft = $daft + random(1,10)>>

And <<set blah blah blah>> runs whatever code you give it (another name for it is <<run ...>> so it doesn’t necessarily set anything. When you write <<set $daft + random(1,10)>>, you’re adding $daft and the random number, but then not telling it to store the result anywhere, so it just disappears.


But that shouldn’t be the problem with your loop; it’ll just affect whether they win or lose, right? I’m not sure what’s going on there; it looks ok to me on a quick read. Two things to check:

  • In your example, $Zg.team is false, not “forest”, so unless that gets changed they wouldn’t get any rewards.
  • You could print the loop index and the random number inside the loop to make sure it’s looping as you expect: loop $forew, random $foreodint or something.

yes, thats kinda the problem and not the problem.
It only ““seemingly”” reads for $Zg and affects $Zg . whilst not all of the code is shown, when a player is allowed to choose who goes on a mission, they are allowed to select up to 5 characters. However, what’s happening is that the code will randomly select a number from the parameters and will not play any other outcome other than if Zg.team is "forest’ and the randomly selected value is 1. … however its all contained within a loop, so why isnt it doing the other IF statements to determine if they’re true??

Solved!.. ish. its a bit of a weird one, and or i just decided to give up and use a different technique but instead i did this: <<if $Zg.team is "forest">> <<if $foreodint == 1>> $Zg.Name ended up finding gold, and gaining experience. <<set $gold += random(1,5)>> <<set $Zg.xp += random(1,5)>> <</if>> <<else>><<set $foreodint = 2>> <</if>> X 5 (because 5 characters, but i dont want to just spam code.) which seemed to work.

@Koipond
Just curious, do you ever use the console log to output variables to help you debug your code?

I think that it is doing them, but they evaluate to false.

Following the logic in the code from the original post, what will happen when foreodint gets a random value of 2 at the beginning of the loop?

The first if checks whether it’s true that ($Zg.team is “forest” && $foreodint is 1).

That’s false, because foreodint is 2 (in our example here).

So the else branch runs and increments foreodint: set $foreodint++.

Now foreodint is 3.

The second if checks whether it’s true that ($Pg.team is “forest” && $foreodint is 2).

That’s false, because foreodint is 3.

So the else branch runs and increments foreodint: set $foreodint++.

Now foreodint is 4.

The third if checks whether it’s true that ($Kg.team is “forest” && $foreodint is 3).

That’s false, because foreodint is 4.

So the else branch runs … and so on.

(And it’s similar for all other random values of foreodint, except for the value 1, which fulfills the first if).

1 Like