Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Switch case issue
Claymix Offline
Junior Member

Posts: 4
Threads: 1
Joined: Jan 2019
Reputation: 0
#1
Switch case issue

I've been working on a custom story for awhile now and I've had a switch case bit of code that has been driving me nuts. For some reason, two of the cases trigger instantly, regardless of what I have it set to. To me, it seems like a weird glitch, although I'm sure it's actually a simple mistake I've made.  The naughty cases are case 1 and case 3. Both cases are over instantly when they are triggered, causing the next case (2 and 4) to trigger at the same time. The weird thing is that case 3 has nearly always been that way, but case 1 only recently started to misbehave and I haven't touched this part of the code at all!


Here is the switch case script. Sorry if the formatting is weird, it didn't paste quite nicely. I tried to clean it up.

PHP Code: (Select All)
void Dream(string &in asTimer)
{
 AddLocalVarInt("DreamStep"1);
 float fEventSpeed 5;
 switch(GetLocalVarInt("DreamStep"))
 {
  case 1             
   AddEntityCollideCallback
("servant_brute_2""BruteDoor""BruteBreakDoor"true1);
   AddEntityCollideCallback("Player""EnterRoomArea""EnterRoom"true1);
   SetPlayerHealth(50);
   FadeOut(0);
   SetMessage("ChapterOneMisc""IntroQuote"11.5f);
   SetPlayerActive(false);
   TeleportPlayer("DreamStart");
   SetSanityDrainDisabled(true);
   PlayMusic("DreamWhisper.ogg"false115false);
   SetPlayerMoveSpeedMul(0.2f);
   SetPlayerRunSpeedMul(0.3f);
   SetPlayerLookSpeedMul(0.3f);
   FadeGlobalSoundSpeed(0.6f1);
   fEventSpeed 12;
   AddDebugMessage("Case1 initializing"true);
  break;

  case 2:
   for(int g 1<= 51g++)
   SetEntityActive("13_burner_" +gtrue);
   SetPlayerActive(true);
   PlaySoundAtEntity("""sanity_damage.snt""Player"0.0ffalse);
   FadeIn(0.1f);
   FadeImageTrailTo(21);
   PlayMusic("29_amb.ogg"true115false);
   fEventSpeed 10;
   AddDebugMessage("Case2 initializing"true);
  break;

  case 3:
   SetEntityActive("servant_brute_2"true);
   CreateParticleSystemAtEntity("""ps_hit_wood.ps""DoorBreak"false);
   PlaySoundAtEntity("""break_wood.snt""BruteDoor_1"0false);
   SetEntityActive("FirstDoor"false);
   StartPlayerLookAt("servant_brute_2"11"");
   AlertEnemyOfPlayerPresence("servant_brute_2");
   fEventSpeed 5;
   AddDebugMessage("Case3 initializing"true);

  case 4                                         
   fEventSpeed 
5;
   AddDebugMessage("Case4 initializing"true);
  break 

  case 5
:
   CheckPoint ("DeathRespawn""PlayerStartBed""DeathRespawn""DeathRespawn""DeathRespawn");
   StartPlayerLookAt("AreaDreamLook"0.5f1"");
   AddTimer(""2"StopLook");
   TeleportPlayer("Dream1");
   AlertEnemyOfPlayerPresence("servant_brute_2");
   SetSwingDoorClosed("dreamdoor"falsefalse);
   SetSwingDoorDisableAutoClose("dreamdoor"true);
   AddPropForce("dreamdoor"003000"world");
   fEventSpeed 3;
   AddDebugMessage("Case5 initializing"true);
  break;
 }
 if(GetLocalVarInt("DreamStep") < 5)  AddTimer("Dream1"fEventSpeed"Dream");



(Case 4 is blank because it has always triggered at the same time as 3)

Thanks so much!
(This post was last modified: 01-15-2019, 01:40 PM by Mudbill.)
01-15-2019, 05:05 AM
Find
Mudbill Offline
Muderator

Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation: 179
#2
RE: Switch case issue

First off, I see this is a timer function. How are you calling it? Can you post the initial call to this function as well? Could it be calling the timer double at times?

The fEventSpeed variable should be set using floats instead of ints. When you set a float to a non-decimal number, it's actually set to an int and implicitly cast to a float. So fMyFloat = 5; is discouraged. Use fMyFloat = 5.0f; instead. This can help you avoid unexpected precision results.

The first time this function is called, DreamStep is set to 1, then it runs case 1 and breaks. After that, it adds a new timer which is meant to be 12 seconds, but due to the above, this might not be the case. Try printing out fEventSpeed before you call the timer.

PHP Code: (Select All)
if(GetLocalVarInt("DreamStep") < 5)
{
    AddDebugMessage("fEventSpeed = " fEventSpeedfalse);
    AddTimer("Dream1"fEventSpeed"Dream");


(This post was last modified: 01-15-2019, 01:59 PM by Mudbill.)
01-15-2019, 01:59 PM
Find
Claymix Offline
Junior Member

Posts: 4
Threads: 1
Joined: Jan 2019
Reputation: 0
#3
RE: Switch case issue

(01-15-2019, 01:59 PM)Mudbill Wrote: First off, I see this is a timer function. How are you calling it? Can you post the initial call to this function as well? Could it be calling the timer double at times?

The fEventSpeed variable should be set using floats instead of ints. When you set a float to a non-decimal number, it's actually set to an int and implicitly cast to a float. So fMyFloat = 5; is discouraged. Use fMyFloat = 5.0f; instead. This can help you avoid unexpected precision results.

The first time this function is called, DreamStep is set to 1, then it runs case 1 and breaks. After that, it adds a new timer which is meant to be 12 seconds, but due to the above, this might not be the case. Try printing out fEventSpeed before you call the timer.

PHP Code: (Select All)
if(GetLocalVarInt("DreamStep") < 5)
{
    AddDebugMessage("fEventSpeed = " fEventSpeedfalse);
    AddTimer("Dream1"fEventSpeed"Dream");


OH GOOD GRIEF. You're right. The timer is being called twice because I got cute with the code. I set up an if/else statement in the OnStart that checked if debug mode was on and if the player had the 1.3 Justine patch. From there, it called the "Dream" timer. But I also had a separate timer earlier in the OnStart that I had used originally to skip to a certain part of the cutscene. During testing, I changed this timer to "Dream" as well on accident so that there were two of them!


Anyway, I changed the ints to floats... good catch! I was lazy when I wrote the code.

Thanks a million, Mudbill. You're a life saver. I knew it was something stupid I'd done. Code can unforgivingly make you look like an idiot sometimes.  Rolleyes 

I'm glad that the community here is still active, considering I joined custom story creation late in the game!
01-15-2019, 09:44 PM
Find
Mudbill Offline
Muderator

Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation: 179
#4
RE: Switch case issue

Forums aren't too active these days, but we still hang around, especially on the Discord server.

01-16-2019, 09:45 PM
Find
Claymix Offline
Junior Member

Posts: 4
Threads: 1
Joined: Jan 2019
Reputation: 0
#5
RE: Switch case issue

Would you suggest using the Discord for custom story development questions?
01-16-2019, 09:58 PM
Find
Mudbill Offline
Muderator

Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation: 179
#6
RE: Switch case issue

Absolutely. You'd be able to get answers much quicker.
If you wish to join, I feel like I need to nudge that some of the people there can be very unserious and joke a lot. But we will help where possible.

01-17-2019, 06:20 PM
Find
Claymix Offline
Junior Member

Posts: 4
Threads: 1
Joined: Jan 2019
Reputation: 0
#7
RE: Switch case issue

Cool beans! Thanks for the tip. A joking atmosphere is fine by me.
(This post was last modified: 01-17-2019, 06:26 PM by Claymix.)
01-17-2019, 06:26 PM
Find




Users browsing this thread: 1 Guest(s)