Development > Newbie Coding

Scripting questions, don't know if that constitutes coding

(1/2) > >>

joe davis:
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: --- checkbox effects_box
{
current *cvar:snd_volume
pos "85 170"
size "20 18"
image "ui/checkbox_blue"
}

--- End code ---
and

--- Code: --- checkbox music_box
{
current *cvar:snd_music_volume
pos "85 195"
size "20 18"
image "ui/checkbox_blue"
}

--- End code ---

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: ---*cvar:snd_volume = 0.5
--- End code ---

B. How can I check if the checkbox is checked or not?  This did not seem to work:

--- Code: ---onClick {if (*node:this@current < 1) { *cvar:snd_volume = 0.5}
         if (*node:this@current > 0) { *cvar:snd_volume = 0.0}
        }

--- End code ---

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?

Duke:
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.

geever:
I agree with Duke about the problem. For menuscript related questions:


--- Quote from: joe davis on February 15, 2010, 12:34:44 am ---The questions this brings to mind is:
A. can I assign a value to the *cvar(s) in the onClick event?  like
--- Code: ---*cvar:snd_volume = 0.5
--- End code ---

--- End quote ---

yes. that's the syntax.


--- Quote from: joe davis on February 15, 2010, 12:34:44 am ---B. How can I check if the checkbox is checked or not?  This did not seem to work:

--- Code: ---onClick {if (*node:this@current < 1) { *cvar:snd_volume = 0.5}
         if (*node:this@current > 0) { *cvar:snd_volume = 0.0}
        }

--- End code ---

--- End quote ---

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


--- Quote from: joe davis on February 15, 2010, 12:34:44 am ---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)?  

--- End quote ---

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.


--- Quote from: joe davis on February 15, 2010, 12:34:44 am ---D. If the user clicks on the bar how could I code the onClick event in the bar to check or uncheck the checkbox?

--- End quote ---

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.


--- Quote from: joe davis on February 15, 2010, 12:34:44 am ---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?

--- End quote ---

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

-geever

joe davis:
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: --- 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 }
          }
}
--- End code ---


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.

--- End quote ---
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.
--- End quote ---
  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.)
--- End quote ---
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: ---*cvar:previous_sound_setting = *node:thatnode@current
--- End code ---
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: ---*cvar:snd_volume = *cvar:previous_sound_setting
--- End code ---
  Will the *cvar:previous_sound_setting still be set to 0.23?

geever:

--- Quote from: joe davis on February 15, 2010, 09:34: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. 

--- End quote ---

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: ---     cmd "echo \"foo far\";"

--- End code ---


--- Quote from: joe davis on February 15, 2010, 09:34:06 pm ---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? 

--- End quote ---

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


--- Quote from: joe davis on February 15, 2010, 09:34:06 pm ---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?

--- End quote ---

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.


--- Quote from: joe davis on February 15, 2010, 09:34:06 pm ---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.

--- End quote ---

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


--- Quote from: joe davis on February 15, 2010, 09:34:06 pm ---  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?

--- End quote ---

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


--- Quote from: joe davis on February 15, 2010, 09:34:06 pm ---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: ---*cvar:previous_sound_setting = *node:thatnode@current
--- End code ---
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: ---*cvar:snd_volume = *cvar:previous_sound_setting
--- End code ---
  Will the *cvar:previous_sound_setting still be set to 0.23?

--- End quote ---

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

-geever

Navigation

[0] Message Index

[#] Next page

Go to full version