Basic Cubescript: Variables
#1
Hi all.

I come from a C++ background so I am wondering what syntax declares a variable that can be recalled later.

From what I've seen alias declares variables/commands and echo can print the contents of that but I can't seem to get that to work.

I like the Cubescript reference but I found it hard to learn the bare basics of the syntax from it.

E.G.


[cubescript]// This file gets executed every time you start AssaultCube.

// This is where you should put any scripts you may have created for AC.

alias test 67 //I'm trying to declare a variable and assign a value to it

alias testscript
[
echo test //I'm trying to print the value of that variable
]

docident testscript "Test Script" //I'm trying to document this so it come up with the description when I type it[/cubescript]

It would appear [ and ] are the equivalent of { and } and semicolons are not required?

When I create a variable how do I specify between strings, integers, and floats?
Thanks given by:
#2
alias is weird compared to more normal languages.

It can be thought of like a container (like a box, nothing computer-science related), you can put anything in it. You can put methods (definitions, functions), variables (strings, numbers), or anything else in it.

Look at the scripts thread: http://forum.cubers.net/thread-38-post-199.html#pid199

Look at the quickknife script. I'll copy it here:
[cubescript]
alias quickknife [
if (!= (curweapon) 6) [melee]
attack
onrelease [ sleep 1 [ if (= (curweapon) 0) [weapon (prevweapon)] ] ]
]
bind Q quickknife
[/cubescript]

The main alias here is quickknife. It gets executed whenever you hit Q. However, you can make this alias, and then do:
[cubescript]
echo $quickknife
[/cubescript]
and then it will print out the script to the screen. You can think of an alias as a container that gets parsed whenever it gets executed by () or other execution methods.

If you aren't scared away from cubescript by now, check out some guides made by some of the best: http://cubescripts.tk/forums_smf2/index.php?board=7.0

You can also join the IRC to ask some questions. irc.gamesurge.net #cubescript
Thanks given by:
#3
I saw the quick knife script earlier and that'd what I learned from. The script I posted earlier doesn't seem to work, though. It doesn't print anything, even if I type echo $test.

The link you gave doesn't appear to work for me. Maybe I'll try it again later.

Can that autoexec file create global aliases for AC? Hmm.

Edit: typing
echo $test
manually works.
The testscript command doesn't seem to work, nor does the documentation appear in the console.

Hmm. Does this forum support inline code? Or does it make everything block style? E.G. the code earlier in this post.
Thanks given by:
#4
try putting 67 in []'s or ""'s

I'll port those forums over to my real host somtimes. They are on a free host, and I haven't done it yet because porting them requires a ton of MySQL hackery :( (maybe because I have to make myself admin, and reset my password)

Yes, it can. Every alias in AC is global O.O
Thanks given by:
#5
So I can't have a local, helper alias then? Like inside the while loops (I didn't see any for loops) That's okay I guess, but then I'd have to make sure they all have unique names (can't use simple x and y...
Thanks given by:
#6
And then prefixes became your best friend :P

Also, try putting the [ for test script on the same line as the declaration.
Thanks given by:
#7
Prefixes are a good idea.
This is what I have now:

[cubescript]// This file gets executed every time you start AssaultCube.

// This is where you should put any scripts you may have created for AC.

alias test 67

alias testscript[

echo $test

]

docident testscript "Test Script"[/cubescript]
The 67 worked as is according to the manual echo $test... test.
just the function alias seems to not declare correctly.

Edit: putting a space (I guess it's the delimiter) between the function name and the '[' did it. Now just the documentation doesn't work.
Thanks given by:
#8
whynot?
:3
* Ronald_Reagan tests

It works, just make sure that you have a / infront of it.

Also, when making docs and menus, make sure you remember that it is a bit procedural. You make a menu (or doc), and each item after that goes into that menu (or doc), until you make a new menu (or doc).
Thanks given by:
#9
Mine does not work, even with the slash. It outputs 67 but does not show any documentation :(
Thanks given by:
#10
The documentation is what you get when you type a command, and dont hit enter. It would be like "/echo". There is an option to turn off the documentation, I think the key is F1 to toggle it. Test it with echo.
Thanks given by:
#11
It works with all commands, it is toggled on. /testscript doesn't get the doc showing though :(
Thanks given by:
#12
I see it :P
Sounds like a bug. Dont worry about it for now, docs aren't very critical. Menus and UI is mostly what makes scripts good or not I think. I also again encourage you to continue this conversation on IRC :)
Thanks given by:
#13
I wanted to be able to document scripts with parameters. The documentation doesn't show in the current version or the SVN. Oh well.
Thanks given by:
#14
Try this link:
http://pbclan.tk/Cubescripts/index.php
Thanks given by:
#15
Yes, that worked. Did you just migrate? I see there are few posts.
Thanks given by:
#16
I migrated just now.

That site actually has a long history of new hosts and such. It precedes (or close to, maybe a little after) me in the AC scene actually.
Thanks given by:
#17
Documentation using "docident" requires more arguments. Look at the default docs for a good example of the proper structure.
Or, if you'd like, you can see how I documented the tools.cfg commands -- based on the pattern visible in the default docs.

A word of wisdom: You'll want to avoid the quotes as much as possible, since they aren't "sided" -- meaning the engine will not nest them in a predictable way like it does with square brackets and parentheses.
Thanks given by:
#18
(28 Nov 11, 10:38AM)V-Man Wrote: Documentation using "docident" requires more arguments. Look at the default docs for a good example of the proper structure.
Or, if you'd like, you can see how I documented the tools.cfg commands -- based on the pattern visible in the default docs.

I see the script needs to be "installed" via execution rather than inside the autoexec itself. I copied the following anyway and didn't get any documentation to show up. Looks like some useful stuff in there :)
[cubescript]alias kickbots [
loop o $numargs [
kickbot (findpn (getalias (concatword "arg" (+ $o 1)))) ]]

docident [kickbots] [Kicks multiple bots out of a bot game.];
docargument [C] [Bot's client number];
docargument [...] [Up to 24 at a time.]
docexample [kickbots 2 4 6 8] [kicks bots with client number 2, 4, 6, and 8.];
docremark [For duplicate client numbers, will only kick one bot each.];[/cubescript]

Where in the tools.cfg does it call docsection.cfg?

(28 Nov 11, 10:38AM)V-Man Wrote: A word of wisdom: You'll want to avoid the quotes as much as possible, since they aren't "sided" -- meaning the engine will not nest them in a predictable way like it does with square brackets and parentheses.

Good advice. I didn't know the brackets could handle strings.
Thanks given by:
#19
What's missing there is the "docsection" command, telling the game that you're going to add to the in-game documentation (though I don't know exactly how crucial this is).
I'm assuming you're putting that code into autoexec.cfg?
If that's the case, it should show up in the in-game documentation.
In-game, type
/kickbots
but don't hit enter. If the documentation doesn't show up, hit F1.
The command
/docvisible 1
should also perform this function.

tools.cfg doesn't specifically call the documentation, there is a separate documentation file for it, "tools docsection.cfg". It is run after the default docs are run, starting its own docsections:
[code=CubeScript]
docsection [Math Tools]

docident [isfloat] [Checks whether a given input is a floating-point number.];
docargument [X] [The item to check];
docexample [echo (isfloat $fov)] [returns 1 since the fov value is in float format.];

// ...

docsection [Alias Tools]

docident [addcheck] [Checks a given alias and, if the given alias does not already have a given string, adds it in using add2alias (see docs regarding that default script).];
docargument [A] [The alias name];
docargument [C] [The command string to search and add in if it is not already in the given alias];
docexample [addcheck commandlist [echo "commands completed!"]] [At the end of executing the alias "commandlist", the given message appears.];
[/code]
Thanks given by:
#20
It's exactly the same problem I had with my testscript and that's almost the same advice Ronny gave me :/

Quote:What's missing there is the "docsection" command
This is problaby my problem, though. Thanks for showing me this -- I'll try it later.

Quote:tools.cfg doesn't specifically call the documentation, there is a separate documentation file for it, "tools docsection.cfg". It is run after the default docs are run, starting its own docsections:

I noticed a scripts directory that automatically executes all its contents on startup. Is that how your docsection gets called? Otherwise I don't see how the game would ever load it if nothing calls it.
Thanks given by:
#21
Ah!
In 1.1.0.4, the custom "scripts" directory is automatically executed if you have made an automatically executed scripts folder.
Basically, you put
[code=CubeScript]
execdir scripts
[/code]
in autoexec.cfg and all your scripts in there get run at startup, including the tools docsection.
Thanks given by:
#22
B_Man (whoops?), I tested the code he gave me (the docident stuff), and it worked through the commandline.
Thanks given by:
#23
(29 Nov 11, 12:58AM)Ronald_Reagan Wrote: B_Man (whoops?)

Talk to Wifey.
Thanks given by:
#24
I'm having some serious trouble with this syntax.
This script I made used to work to an extent, going one-way and echoing the value but not toggling it. Now I don't know what's wrong.

I'm trying to emulate a Boolean variable and act on it.
I'm trying to do this:

bool slow = false;

void toggle_slo_mo
{
    slow = !slow;
    
    if (slow)
    gamespeed = 10;
    
    else
    gamespeed = 100;
}

[cubescript]
alias slow 0 //0 = normal speed 1 = slo_mo

alias toggle_slo_mo
[

if ( = $slow 0 ) //if slow is off
[
push slow 1; //turn slow on
gamespeed 10 //slow-mo
]
//else...
[
pop slow; //turn slow off
gamespeed 100 //normal-speed
]

echo $slow //debug
]

bind LALT toggle_slo_mo //no minimap for you
[/cubescript]

But it doesn't even echo the value, neither via LALT nor via manual typing the /toggle_slo_mo

Is a line-break a delimiter for commands as well as the semicolon?
I had tried removing it before and it ended up trying to combine the commands.

What methods are there for just assigning values to previously created aliases?

alias ali
ali = value? ali value? alias ali value?
Thanks given by:
#25
A hard return performs the same punctuation function as a semicolon (so ending a line with a semicolon is redundant).
You'll want the alias definitions, at least the starting brackets, to be on the same line to show the engine that you're explicitly beginning a new argument. After you open an argument, you can put whatever you want in it, including hard returns, without actually ending that particular argument (in other words, arguments can have multiple arguments within them; brackets group them as one). In general, the arguments of commands need to start on the same line as the argument before it.
So, for your script:

[cubescript]
alias slow 0 //0 = normal speed 1 = slo_mo

alias toggle_slo_mo [
if ( = $slow 0 ) [ //if slow is off

push slow 1; //turn slow on
gamespeed 10] [ //slow-mo
// else...
pop slow; //turn slow off
gamespeed 100 //normal-speed
]

echo $slow //debug
]

bind LALT toggle_slo_mo //no minimap for you
[/cubescript]

Note that closing brackets can come after any number of hard returns, as long as they are there (but the game does try to parse all white space, so you'll want only enough to make the script readable).

I didn't test the script, so I'm not sure how it'll work. XD

Think of an alias as a container. It only has what you tell it to have in it.
[cubescript]
alias examplealias 3
alias examplealias (- 5 2) // this will be calculated and stored when "alias" is run
examplealias = [- 5 2] // this will be stored as a function that finds "- 5 2" each time "examplealias" is run
// I see you found push and pop as well.
push examplealias (div 6 2)
pop examplealias
[/cubescript]

Almost forgot some other modifying commands:

[cubescript]
+= examplealias 2
-=f examplealias 2
*=f examplealias (divf $examplealias 3)
add2alias examplealias "bottles of beer on the wall"
[/cubescript]

(and there are some nice ones in tools.cfg as well.)

A nice note:
The "if" command's first argument, the evaluation, needs only a 1 or a 0. Thus, you can have for the evaluation something that will either be 1 or 0 (for example, $slow in your script). Then you'll want to swap the other two arguments:

[cubescript]
alias slow 0 / /0 = normal speed 1 = slo_mo

alias toggle_slo_mo [
if $slow [pop slow; gamespeed 100] [push slow 1; gamespeed 10]
echo $slow]

bind LALT toggle_slo_mo //no minimap for you
[/cubescript]
Thanks given by:
#26
Or if your me, your code ends up like this:
[cubescript]
alias slow 0 //0 = normal speed 1 = slo_mo
alias toggle_slo_mo [ if $slow [pop slow; gamespeed 100] [push slow 1; gamespeed 10] echo $slow]

bind LALT toggle_slo_mo //no minimap for you
[/cubescript]

Thanks given by:
#27
haha, that's useful for when you're entering a scripting contest that has a limit on the number of lines. :-P
Thanks given by:
#28
Or you never explored why CS is so picky, and in what ways it is picky.
Thanks given by:
#29
(01 Dec 11, 03:50AM)V-Man Wrote: haha, that's useful for when you're entering a scripting contest that has a limit on the number of lines. :-P

Or when we start the International Obfuscated CubeScript Code Contest. Oh boy, that would be fun :D
Thanks given by:
#30
Awesome! The script now works as intended! You should try it with bots -- it's really fun jumping around a corner and splattering/headshotting a bot in slow motion and watching the gibs come out :)

I think I'll try to update the script by storing the sensitivity(s) and make them lower when in "bullet time" :P

I also thought about using megabind so that alt toggles in while watching a replay but not while playing. I'm not sure what key I can use that's easy to hit while actually playing. When you're typing a message do letter keybinds activate? I don't think they do meaning it'd be okay to bind a letter.

Thanks V-Man for the education on CS syntax -- some of that should be in the docs/reference.
Thanks given by: