project-navigation
Personal tools

Author Topic: Scripting questions, don't know if that constitutes coding  (Read 5355 times)

Offline joe davis

  • Rookie
  • ***
  • Posts: 55
    • View Profile
Scripting questions, don't know if that constitutes coding
« on: February 15, 2010, 12:34:44 am »
So, I am a Newb and would like to study the game and have decided to start with scripting since that seems to be the easiest and the most visible in regards to having more instant visual feedback.  I figure I should try to learn how to crawl before trying to learn how to run.  

So I checked out the documentation on the wiki and have more questions.  

A user requested a modification and I wanted to see what it would entail to make a user modification for it.  The request was to add checkboxes next to the effects_str and the music_str to turn them off and on similar to the checkbox sound_init node.  He posted a pic  here: http://ufoai.ninex.info/forum/index.php?topic=4428.msg33974#msg33974

I added the checkboxes and have obtained general functionality, but I am not satisfied with my results and have generated a few questions, which I find to be a positive thing as my primary interest is more in studying than making this work.

Code: [Select]
checkbox effects_box
{
current *cvar:snd_volume
pos "85 170"
size "20 18"
image "ui/checkbox_blue"
}
and
Code: [Select]
checkbox music_box
{
current *cvar:snd_music_volume
pos "85 195"
size "20 18"
image "ui/checkbox_blue"
}

This works well enough in regards to setting the volume to 0.0 when it is unchecked.  
However I run into a couple of various concerns and snags:

1. When I re-check the checkbox from an unchecked state, each of the volumes respond differently.  
a. The effects volume is changed to 1.0 I would assume, ie. it is turned all the way up.
b. The music volume is changed to what appears to be 0.1 or just a sliver.

The questions this brings to mind is:
A. can I assign a value to the *cvar(s) in the onClick event?  like
Code: [Select]
*cvar:snd_volume = 0.5
B. How can I check if the checkbox is checked or not?  This did not seem to work:
Code: [Select]
onClick {if (*node:this@current < 1) { *cvar:snd_volume = 0.5}
         if (*node:this@current > 0) { *cvar:snd_volume = 0.0}
        }

C. How could I click another node from within an onClick event.  For example user clicks on checkbox and I check if supposed to be at 0.0 or 0.5 volume based whether checked or not and then I click on the appropriate bar, for example the effects_bar, at the appropriate location(ie 0.0 or 0.5)?  

D. If the user clicks on the bar how could I code the onClick event in the bar to check or uncheck the checkbox?

2. Is there a method of saving the original position of the sound prior to me hitting the checkbox to turn it off so that when I re-check the checkbox I can place it at the original position instead of the current positions of 1.0 and 0.1 or my proposed position 0.5?

Offline Duke

  • Administrator
  • PHALANX veteran
  • *****
  • Posts: 1037
    • View Profile
Re: Scripting questions, don't know if that constitutes coding
« Reply #1 on: February 15, 2010, 01:34:30 pm »
Although you stated that it's  just for learning purpose, I have to say that I don't like the approach.
Playing sound at volume 0 and not playing it can be quite a difference. So we should simply split the enabling cvar and handle it in the code.

Imho you should find an example that we can actually use once it has served your learning.

Offline geever

  • Project Coder
  • PHALANX Commander
  • ***
  • Posts: 2561
    • View Profile
Re: Scripting questions, don't know if that constitutes coding
« Reply #2 on: February 15, 2010, 02:19:08 pm »
I agree with Duke about the problem. For menuscript related questions:

The questions this brings to mind is:
A. can I assign a value to the *cvar(s) in the onClick event?  like
Code: [Select]
*cvar:snd_volume = 0.5

yes. that's the syntax.

B. How can I check if the checkbox is checked or not?  This did not seem to work:
Code: [Select]
onClick {if (*node:this@current < 1) { *cvar:snd_volume = 0.5}
         if (*node:this@current > 0) { *cvar:snd_volume = 0.0}
        }

The problem is probably with spaces. You should put spaces around brackets.

C. How could I click another node from within an onClick event.  For example user clicks on checkbox and I check if supposed to be at 0.0 or 0.5 volume based whether checked or not and then I click on the appropriate bar, for example the effects_bar, at the appropriate location(ie 0.0 or 0.5)?  

You don't need to. Set the cvar. If you wanna run the same event as it would run on clicking you can call *node:thatnode@onClick.

D. If the user clicks on the bar how could I code the onClick event in the bar to check or uncheck the checkbox?

Set the cvar assigned to it's current property.
Btw. using the same cvar for the checkbox and the volume bar is not a nice solution. I suggest using a different one for checkboxes.

2. Is there a method of saving the original position of the sound prior to me hitting the checkbox to turn it off so that when I re-check the checkbox I can place it at the original position instead of the current positions of 1.0 and 0.1 or my proposed position 0.5?

You can save them to other cvars..
(Assigning a value to a cvar also creates it if didn't exist.)

-geever

Offline joe davis

  • Rookie
  • ***
  • Posts: 55
    • View Profile
Re: Scripting questions, don't know if that constitutes coding
« Reply #3 on: February 15, 2010, 09:34:06 pm »
Thanks again for the awesomely quick reply and wonderful feedback.  I wish I could understand it better.  Sorry I'm slow and thanks for your patience.  I don't mean to question your knowledge or your experience, I only try to probe further so that I can understand better.

O.K., so the first thing was that I needed spaces around my parens and brackets, like geever said, and then it would run, Thanks geever.

However the code in the onClick event of the checkboxes didn't seem to work.  Does my code in onClick over ride the checkbox's code?  Does the checkbox's onClick event work?  I'm lost on why it's not working.  Here is what I did:
Code: [Select]
checkbox effects_box
{
current *cvar:snd_volume
pos "85 170"
size "20 18"
image "ui/checkbox_blue"
onClick { if ( *node:this@current < 1 ) { *cvar:snd_volume = 0.5 }
              if ( *node:this@current > 0 ) { *cvar:snd_volume = 0.0 }
          }
}



Duke, I am sorry man, that went right over my head. 

Why is there a big difference with playing sound at 0.0 and not playing it at all?  Do you think I should release those resources instead of using them at an imperceptible level? 

Are you saying I should use *cvar:snd_init = 0.0 instead of *cvar:snd_volume = 0.0 in the checkboxes onClick event?  Is that what you meant by split the enabling cvar and handle it in the code?

Side Note: I didn't touch the sound_init checkbox code but when I hit it in the game it shuts off the sound like I would expect but when I re-check it, it  doesn't come back on.  Does it work right on your copies?  Maybe I just broke mine.

Quote
If you wanna run the same event as it would run on clicking you can call *node:thatnode@onClick.
Ok, I understand the calling the onClick event to trigger it, and that is cool, but I am like majorly lost on the thatnode part.  I think I tried *node:parent.effects_box, and *node:options.options_sounds.effects_box and *node:effects_box, all with the @onClick and it wouldn't parse.  The Console said it had no clue what I was talking about and I responded, yeah that apparently makes two of us.  I don't know if I had a typo or if it's just my syntax but the console didn't like my node paths.

Quote
Btw. using the same cvar for the checkbox and the volume bar is not a nice solution. I suggest using a different one for checkboxes.
  You are most likely right in this opinion, and I don't mean to question your knowledge or experience, but why?  From my newbish position I am getting interaction with the bar and checkbox simply in response to the value of the cvar.  Since I am still confused on what I am doing, it is nice to tie it in that way.  The user clicks on the bar and if the value is higher than 0.0 and the checkbox is unchecked then the checkbox becomes checked, and also if the bar's value is 0.0 the checkbox becomes unchecked.  No additional coding required.  This also occurs when the checkbox is checked or unchecked, the bar is changed to reflect the status.  Though I still don't understand why the music volume is being set to what appears to be 0.1 instead of 1.0.  What am I missing by doing this?  Why do you feel that it's not a nice solution?  What benefit do I get by using a new cvar for the checkboxes?

Quote
You can save them to other cvars..
(Assigning a value to a cvar also creates it if didn't exist.)
I am still pending on this as I can't get my onClick event to work, but I was wondering if this would carry over after I exit the game?  For example I have it set to 0.23 and then click on the checkbox.  The checkbox saves the 0.23 to the
Code: [Select]
*cvar:previous_sound_setting = *node:thatnode@current where thatnode is the path to the relevant bar.  Then I play and exit out with the sound off.  Later that day I get back on the game but this time want sound so I click the checkbox which fires for example, the code:
Code: [Select]
*cvar:snd_volume = *cvar:previous_sound_setting  Will the *cvar:previous_sound_setting still be set to 0.23?

Offline geever

  • Project Coder
  • PHALANX Commander
  • ***
  • Posts: 2561
    • View Profile
Re: Scripting questions, don't know if that constitutes coding
« Reply #4 on: February 17, 2010, 11:31:06 pm »
However the code in the onClick event of the checkboxes didn't seem to work.  Does my code in onClick over ride the checkbox's code?  Does the checkbox's onClick event work?  I'm lost on why it's not working. 

What I'm not sure about how can you mix integer and float numbers there. And to be honest I'm a bit lost with these small patches. You can query the cvar values on console by typing their names. Check what value it get.
The onClick even't doesn't override anything (maybe itself) first the checkbox sets the cvar then runs the event IIRC. To check if an event runs you can insert echos:

Code: [Select]
     cmd "echo \"foo far\";"

Why is there a big difference with playing sound at 0.0 and not playing it at all?  Do you think I should release those resources instead of using them at an imperceptible level? 

When playing the sound on 0.0 volume we still decompress it and maybe send to the soundcard which is wasted resources for sure.

Are you saying I should use *cvar:snd_init = 0.0 instead of *cvar:snd_volume = 0.0 in the checkboxes onClick event?  Is that what you meant by split the enabling cvar and handle it in the code?

No that would turn of the whole soundsystem not just effects or music. There is no way to disable one of them at the moment that's why we don't have checkboxes for them. It needs C coding.

Side Note: I didn't touch the sound_init checkbox code but when I hit it in the game it shuts off the sound like I would expect but when I re-check it, it  doesn't come back on.  Does it work right on your copies?  Maybe I just broke mine.

It does work for sure, but music not started again immediately on enabling the sound system.

  You are most likely right in this opinion, and I don't mean to question your knowledge or experience, but why?
  What benefit do I get by using a new cvar for the checkboxes?

Cleaner script. You will know what has changed the state of the checkbox for example.

I am still pending on this as I can't get my onClick event to work, but I was wondering if this would carry over after I exit the game?  For example I have it set to 0.23 and then click on the checkbox.  The checkbox saves the 0.23 to the
Code: [Select]
*cvar:previous_sound_setting = *node:thatnode@current where thatnode is the path to the relevant bar.  Then I play and exit out with the sound off.  Later that day I get back on the game but this time want sound so I click the checkbox which fires for example, the code:
Code: [Select]
*cvar:snd_volume = *cvar:previous_sound_setting  Will the *cvar:previous_sound_setting still be set to 0.23?

No, only cvars which are added to the CVAR_ARCHIVE are saved. And that can only be done in C code.

-geever

Offline joe davis

  • Rookie
  • ***
  • Posts: 55
    • View Profile
Re: Scripting questions, don't know if that constitutes coding
« Reply #5 on: February 18, 2010, 04:37:11 am »
Awesome, I have been studying this earlier and since I was experiencing problems with the code for the onClick event I switched over to the onChange event.  The onChange event works fine, not sure why the onClick didn't.  Also with the path confusion, I was trying to call something that wasn't defined yet so I put the check box after the bar and that helped immensely. 

I don't know about the integer and float values.  They work, guess I could make them both float, huh.

I appreciate this information about the echo and querying cvars.  That is very useful information.  I didn't know about that, was that in the documentation somewhere?

Since there isn't anything coded in C for the effects or music I will stick with my quasi coded solution and lower the volume to 0.0.

As to the cvars, guess I am still confused on how they work.  I have resolved to using surrounding values until it makes more sense to me.  And in regards to saving the previous value, prior to clicking on the checkbox, since it isn't in the CVAR_ARCHIVE, I will use arbitrary values that work for me.

That being said ,I have my first functional solution.  Thank you for your assistance with this.

The onClick event didn't seem to work so used the onChange event and got it to work. 
Code: [Select]
checkbox effects_box
{
current *cvar:snd_volume
pos "85 170"
size "20 18"
image "ui/checkbox_blue"
onChange { if ( *node:this@current > 0 ) { *cvar:snd_volume = 0.5 }
else { *cvar:snd_volume = 0.0 }
             }
       
}

The music check box is different due to the music_snd_volume being coded differently appearantly.

Code: [Select]
checkbox music_box
{
current *cvar:snd_music_volume
pos "85 195"
size "20 18"
image "ui/checkbox_blue"
onChange { if ( *node:this@current > 0 ) { *cvar:snd_music_volume = 100.0 }
     else { *cvar:snd_music_volume = 0.0 }
             }
}

interestingly enough this doesn't put the volume at 100% which I thought it would at first.  The position of the sound seemed fine so left it there.

As to the path, it would appear that my problem with that was I needed to define the check box after the bar since I was trying to call a property of the bar from the checkbox.  Silly me, huh.  lol.

But in the end I decided to change the sound cvars values instead of the bars current value.  This was primarily due to the fact that I couldn't get it to work by calling the bars current value.  Regardless, this works.

So I learned:
  • the importance of spaces
  • defining something prior to trying to use it
  • and simple if else blocks
  • about some basic debugging methods mentioned above(ie querying cvars, and use of echo
   

I was confused 
  • on why the check boxes onClick event didn't seem to want to work
  • the sound volume for the effects and music were handled differently

Thanks for your assistance.

Offline geever

  • Project Coder
  • PHALANX Commander
  • ***
  • Posts: 2561
    • View Profile
Re: Scripting questions, don't know if that constitutes coding
« Reply #6 on: February 18, 2010, 10:42:43 pm »
I was confused 
* on why the check boxes onClick event didn't seem to want to work

Hmm, it was not implemented. Bayo must have been a bit lazy on these... ;)
It's working from r28632.

* the sound volume for the effects and music were handled differently

That must be a legacy, I don't know...

-geever

Offline Duke

  • Administrator
  • PHALANX veteran
  • *****
  • Posts: 1037
    • View Profile
Re: Scripting questions, don't know if that constitutes coding
« Reply #7 on: February 19, 2010, 12:54:28 am »
Duke, I am sorry man, that went right over my head. 

Why is there a big difference with playing sound at 0.0 and not playing it at all?  Do you think I should release those resources instead of using them at an imperceptible level? 
Think of a radio. Turn down the volume to 0 but do NOT switch it off. You don't hear a thing, but the radio will still consume electric power (for UFO power translates to performance).
Not to mention erroneous situations like playing a corrupted sound file at a volume other than 0 ...

General IT design rule: if you don't want a function to do anything for you, DO NOT CALL it !
Got the idea ?

Offline joe davis

  • Rookie
  • ***
  • Posts: 55
    • View Profile
Re: Scripting questions, don't know if that constitutes coding
« Reply #8 on: February 19, 2010, 06:35:38 pm »
Duke that makes sense, though I would like to get more details on what you mean by
Quote
Not to mention erroneous situations like playing a corrupted sound file at a volume other than 0 ...
  Is there a possible complication of me playing the volume at 0.0?  And how does this vary from me using the current gui layout to turn the volume of the effects or sound all the way down? 

  Basically all I am doing is adding a checkbox to turn the volume all the way down and, for aesthetic purposes, making sure that it plays well with the bar that is already there.  Are you seeing a possibility for future complications with the way that I am implementing it?

Offline Duke

  • Administrator
  • PHALANX veteran
  • *****
  • Posts: 1037
    • View Profile
Re: Scripting questions, don't know if that constitutes coding
« Reply #9 on: February 19, 2010, 11:32:14 pm »
I remember a situation with a different game. Whenever a certain crash occurred, the sound volume went to max and the last few millisecs of the sound were repeated until reboot. Very bad for my ears :(
Such things should not happen if sound is simply not active.

I can't tell you what can/will go wrong, but I know that the design I suggested will safely avoid some of the problems. Don't get me wrong: your approach for this sound thingy will probably never ever cause any problems. My point is: if you implement 20, 50 or 100 similar things with this design, it's almost certain that at least one of them will cause severe problems.