Can you save a script in a variable and have the variable make the unit? (DONE)

Started by TLFP, January 12, 2016, 07:51:48 PM

Previous topic - Next topic

TLFP

Question in title. The reason I want this is so I don't have to keep inputting Xthing.crpl into a script. :P
So like this:$Xvar:"Xscript"
#blablabla...
#here's the part I'm interested in
<-Xunit "Xvar" Addscripttounit

This is not real code, just an example.
I've tried to do this and all it does is add "Xvar.crpl" to the core.
Currently ghosting, don't mind me.
Christian, my list of various content creation groups can be found here

GameGibu

The script has to exist in the scripts folder in the world editor to make AddScriptToUnit(unit ScriptName) work.

Having a month's knowledge of CRPL, I  can tell you that you can't write scripts within scripts...

Sorry!
-GameGibu

Actually I'm not sure if this even answers your question, but it's a good answer to its matching question.
Bringing you Custom maps since 2971!
☆CRPL Master☆

J

Just so you can let your imagination go wild, this is real code (assuming you have a script Xscript.crpl):
$Xvar:"Xscript.crpl"
#blablabla...
#here's the part I'm interested in
<-Xunit <-Xvar Addscripttounit

You can't write code inside code (just do it in code instead of a string), but you can have a ton of scripts stored and have a script attach a random one of them or one given through the in-game input fields. Just don't forget the ".crpl".

edit: Just realised I'm now finally one of the top-10 posters :P

TLFP

#3
Quote from: J on January 13, 2016, 05:14:29 PM
Just so you can let your imagination go wild, this is real code (assuming you have a script Xscript.crpl):
$Xvar:"Xscript.crpl"
#blablabla...
#here's the part I'm interested in
<-Xunit <-Xvar Addscripttounit

You can't write code inside code (just do it in code instead of a string), but you can have a ton of scripts stored and have a script attach a random one of them or one given through the in-game input fields. Just don't forget the ".crpl".

edit: Just realised I'm now finally one of the top-10 posters :P
Thanks! And I don't have a script called that, just as an example.  :D
How would you do it randomly because I have 5 scripts that I want a unit to build around it separately and with a delay and not mess everything up. Usually what happens is 2 separate scripts are attached to the same unit and it will only build one of the units (with or without the error).
Currently ghosting, don't mind me.
Christian, my list of various content creation groups can be found here

GameGibu

To randomize:
RandInt(0 5) "scriptSuffix" concat ->RandScriptName
#then
<-unit <-RandScriptName AddScriptToUnit


Hope that helps!
-GameGibu

Bringing you Custom maps since 2971!
☆CRPL Master☆

Grayzzur

Lists and random numbers.


#Create a list
CreateList ->scriptList
#Put all your script names into it (don't forget the ".crpl" on the filenames)
<-scriptList "someScript1.crpl" AppendToList
<-scriptList "someScript2.crpl" AppendToList
<-scriptList "someOtherScript.crpl" AppendToList
<-scriptList "yetAnotherScript.crpl" AppendToList
<-scriptList "someScript3.crpl" AppendToList

#NOTE: everything above here should likely be in a ONCE block

#using GetListCount to feed the random number generator instead of 5 in case you want to add or remove from the list
#this should spit out one of the script names from the list above:
<-scriptList RandInt(0 GetListCount(<scriptList)) GetListElement
#You can either put that line into the AddScriptTounit command, or save it to a variable to make the next part easier to read
->randomScript

#your unit creation stuff followed by the
#...
<-someUnitID <-randomScript AddScriptToUnit
"Fate. It protects fools, little children, and ships named 'Enterprise.'" -William T. Riker

Grayzzur

Quote from: GameGibu on January 14, 2016, 09:11:14 AM
To randomize:

RandInt(0 5) "scriptSuffix" concat ".crpl" concat ->RandScriptName
#then
<-unit <-RandScriptName AddScriptToUnit


Hope that helps!
-GameGibu
That works too, but don't forget to concat ".crpl" to the end of the script name as well.

Note that RandInt(A B) generates numbers from A to (B-1). In the above example, 0-4. This works well with my use of a list above, as there are 5 list elements, numbered 0-4.
"Fate. It protects fools, little children, and ships named 'Enterprise.'" -William T. Riker

TLFP

#7
Quote from: Grayzzur on January 14, 2016, 09:13:35 AM
Quote from: GameGibu on January 14, 2016, 09:11:14 AM
To randomize:

RandInt(0 5) "scriptSuffix" concat ".crpl" concat ->RandScriptName
#then
<-unit <-RandScriptName AddScriptToUnit


Hope that helps!
-GameGibu
That works too, but don't forget to concat ".crpl" to the end of the script name as well.

Note that RandInt(A B) generates numbers from A to (B-1). In the above example, 0-4. This works well with my use of a list above, as there are 5 list elements, numbered 0-4.
The code I'm trying to randomize is intended to be a PAC with sleeper units map. It's supposed to create ONE core if there is creeper under it, destroy creeper under it, and attach ONE script to it per 10 seconds. Instead, it attaches 0-2 scripts to a core and does it 1 every .5 seconds and causes LAG. Here's the code I'm trying to randomize $NeededAmount:50
#Self CONST_MAXAMMO <-NeededAmount SetUnitAttribute
if(<-creeperAmount <-NeededAmount  gte)
0 5 RandInt ->Creep
150 delay
"CRPLCore" 33 26 12 RandCoordsInRange CreateUnit ->unit
if(<-Creep 1 eq)
<-unit "CCannon.crpl" AddScriptToUnit
endif
if(<-Creep 2 eq )
<-unit "CSniper.crpl" AddScriptToUnit
endif
if(<-Creep 3 eq)
<-unit "CSprayer.crpl" AddScriptToUnit
endif
if(<-Creep)
<-unit "CMortar.crpl" AddScriptToUnit
endif
endif
<-creeperAmount <-creeperAmount sub
delay 30
if(<-time neq0)
<-time <-time sub
endif
I'm still not finished with it as I want there to be a bar indicating how much creeper is left to go AND not cause serious LAG.
If you're wondering how I made this sorta work I changed all the custom images that go along with it.
EDIT: This is what happens:
Currently ghosting, don't mind me.
Christian, my list of various content creation groups can be found here

Grayzzur

   if(<-Creep)
   <-unit "CMortar.crpl" AddScriptToUnit
   endif

No conditional there being applied to Creep, so anytime Creep is zero it won't do anything and anytime it's ANY other number you get a CMortar added. (In addition to the scripts you add if it's 1, 2 or 3.)

<-creeperAmount <-creeperAmount sub
<-time <-time sub

Not sure what you're trying to do with these lines. You're putting a value on the stack twice and subtracting, giving you a zero, and you're leaving the zero on the stack and not doing anything with it. If the intent is to reduce the variable by some amount, it would look like this, to say subtract 10 and store the answer back in the same variable:
<-creeperAmount 10 sub ->creeperAmount
"Fate. It protects fools, little children, and ships named 'Enterprise.'" -William T. Riker

TLFP

Quote from: Grayzzur on January 14, 2016, 11:09:13 AM
   if(<-Creep)
   <-unit "CMortar.crpl" AddScriptToUnit
   endif

No conditional there being applied to Creep, so anytime Creep is zero it won't do anything and anytime it's ANY other number you get a CMortar added. (In addition to the scripts you add if it's 1, 2 or 3.)

<-creeperAmount <-creeperAmount sub
<-time <-time sub

Not sure what you're trying to do with these lines. You're putting a value on the stack twice and subtracting, giving you a zero, and you're leaving the zero on the stack and not doing anything with it. If the intent is to reduce the variable by some amount, it would look like this, to say subtract 10 and store the answer back in the same variable:
<-creeperAmount 10 sub ->creeperAmount
I'm trying to reset those variables so I don't have the creeps being made all the time. The time variable is supposed to delay the creeps being made, because the delay doesn't work. On top of that I want the core to subtract creeper every time it makes a creep.
Currently ghosting, don't mind me.
Christian, my list of various content creation groups can be found here

Builder17

Where this gets creeper amount using GetCreeper?
Do you want it SetCreeper 0 always when it creates unit?
Edit:There is never GetCreeper command so how it knows is there enough creeper under it?



TLFP

Quote from: Builder17 on January 14, 2016, 11:59:58 AM
Where this gets creeper amount using GetCreeper?
Do you want it SetCreeper 0 always when it creates unit?
Yes so you don't have free creeps.
Currently ghosting, don't mind me.
Christian, my list of various content creation groups can be found here

Builder17

There is no GetCreeper command so how it knows is there enough creeper under it .
And it also never sets creeper in current coordinates into zero.

TLFP

Quote from: Builder17 on January 14, 2016, 12:35:31 PM
There is no GetCreeper command so how it knows is there enough creeper under it .
And it also never sets creeper in current coordinates into zero.
I updated the code:$NeededAmount:50
#Self CONST_MAXAMMO <-NeededAmount SetUnitAttribute
if(<-creeperAmount <-NeededAmount  gte)
0 5 RandInt ->Creep
150 delay
"CRPLCore" 33 26 12 RandCoordsInRange CreateUnit ->unit
if(<-Creep 1 eq)
<-unit "CCannon.crpl" AddScriptToUnit
endif
if(<-Creep 2 eq )
<-unit "CSniper.crpl" AddScriptToUnit
endif
if(<-Creep 3 eq)
<-unit "CSprayer.crpl" AddScriptToUnit
endif
if(<-Creep)
<-unit "CMortar.crpl" AddScriptToUnit
endif
endif
CurrentCoords 0 Setcreeper
<-creeperAmount <-creeperAmount sub ->creeperAmount
delay 30
if(<-time neq0)
<-time <-time sub ->time
endif
Currently ghosting, don't mind me.
Christian, my list of various content creation groups can be found here

Builder17

Should you go looking better into CRPL section of wiki?
knucklecracker.com/wiki/doku.php?id=crpl:start