Need Help With Scripting
I'm trying to replace certain prop_weighted_cube entities using scripts, but for some reason it isn't working. Here is a copy of the script I created.
function setCubes(){
baboo <- null;
while((baboo = Entities.FindByName(baboo,"Test_Cube")) != null){
if ( baboo.IsValid() ){
baboo.SetModel("models/props/metal_boy.mdl");
}
}
didSetCubes <- true;
}
setCubes();
printl("If you're seeing this, the <Holiday Name Here> script loaded without a hitch. Yay" + UniqueString() );
I have this set to runscriptcode and to runscriptfile by a logic auto when the map spawns, and have the env_entity maker which makes the Test_Cube runscriptcode and runscriptfile after .1 seconds of spawning an entity.
When the logic_script is triggered I get the following error.
"script not found (script/vscript/o.nut)"
I have saved the .nut file into the vscripts folder under portal2 though, any ideas on how to get this to work?
Thanks in advance
"Entity CubeSwitch found error in RunScript0"
CubeSwitch is the name of the logic_script using the code above. Do you know why this would happen?
baboo.SetModel("models/props/metal_boy.mdl");
That may be a problem if it isn't a custom prop.
ChickenMobile wrote:
What is metal_boy?baboo.SetModel("models/props/metal_boy.mdl");
That may be a problem if it isn't a custom prop.
metal_boy is the name of the new model to replace the cube. It is a custom model.
EDIT: also if you are only using names inside a function you should declare it as variable and not a global variable.
e.g. baboo <- null to local baboo = null
Sorry for not understanding that fully :/.
I changed the code to this and still got same error
function setCubes(){
var baboo <- null;
while((baboo = Entities.FindByName(baboo,"Test_Cube")) != null){
if ( baboo.IsValid() ){
baboo.SetModel("models/props/metal_boy.mdl");
}
}
didSetCubes <- true;
}
setCubes();
printl("If you're seeing this, the valentine script loaded without a hitch. Yay" + UniqueString() );
didSetCubes <- false;
DBG <- 1 //set to 0 once finished debugging
//Change all the models of the cubes to custom ones
function setCubes() {
local baboo = null
//retrieve all cubes with the name Test_Cube and set their model to metal_boy
while((baboo = Entities.FindByName(baboo,"Test_Cube")) != null){
if(DBG)printl("found cube!")
if ( baboo.IsValid() ) baboo.SetModel("models/props/metal_boy.mdl")}
}
didSetCubes = true;
}
setCubes() //Set all cubes models on level start
if(DBG)printl("setted the cubes! " + UniqueString())
I will add IsValid to the list of Portal 2 functions on the wiki.
I copy and pasted exactly what you put in the code, and ran the map with it, and I got the same exact error. I put this in:
didSetCubes <- false;
DBG <- 1 //set to 0 once finished debugging
function setCubes() {
local baboo = null
while((baboo = Entities.FindByName(baboo,"Test_Cube")) != null){
if(DBG)printl("found cube!")
if ( baboo.IsValid() ) baboo.SetModel("models/props/metal_boy.mdl")}
}
didSetCubes = true;
}
setCubes()
if(DBG)printl("setted the cubes! " + UniqueString())
How did you actually implement yours in your test map? I'm just using a logic_script with the entityscripts property this code, and having the logic_script RunScriptCode .1 seconds after an env_entity_maker spawns an entity. Is there something wrong in doing this?
I assume your script is scripts/vscripts/o.nut
Notes on code:
||I didn't understand what the IsValid() function check was for but I understand now. It is just a backup to check if the entity found with the name is actually a valid entity (or one that exists).
I believe if you delete the if ( baboo.IsValid() ) it will still work fine. Though I would keep it in anyway.
There is no point in creating the didSetCubes global variable if you are not going to reference it later. Because the function setCubes() is executed on startup, I don't see why you need this at all.
Lastly, try to keep variable names descriptive. I'm not sure if baboo means anything in another language but I would have called it currentCube or foundCube. Remember you can add comments to your code for someone else to better understand by two slashes //||
I don't know where it is getting that o.nut from. Any idea?
One thing that might be helpful, I'm saving it as a .nut file, and the file type shows it as a .nut, but when I save it, I save it as a .txt file using Notepad++
.nut files are just pure code, it shouldn't matter what sort of encoding it is saved with.
Double check that your logic_script is actually pointing towards your script file.
script2.jpgClick the Manage button and highlight your script in the menu and click open source. If it doesn't open any file or the wrong one, then change it to suit. If there are multiple files in the dialog, then delete (-) the wrong one and add (+) the right one in.
script-2.jpgLastly: when you spawn the cube and change the cube model, it would be more efficient to change the model of the cube that you have just spawned.
This could probably be done by creating a trigger which the cube passes through when spawned.
OnStartTouch > !activator > RunScriptCode > setCubeModel(self)
And in your script file:
function setCubeModel(cube){
cube.SetModel("models/props/metal_boy.mdl")
}
So the final code would look something like this:
function setCubeModel(cube){
cube.SetModel("models/props/metal_boy.mdl")
}
function setCubes() {
local baboo = null
while((baboo = Entities.FindByName(baboo,"Test_Cube")) != null){
if ( baboo.IsValid() ) setCubeModel(baboo)
}
}
setCubes()
printl("setcubes " + UniqueString());
Also why is your steamapps folder on your desktop? I doubt that would even work.
EDIT: I see your problem. Remove the scripts/vscripts in the path and it will work. It looks for scripts relative to that path.
e.g. chicken_scripts/o.nut NOT scripts/vscripts/chicken_scripts/o.nut
