Need Help With Scripting

Avatar
Tmast98
210 Posts
Posted Jul 08, 2013
Hi,

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

Advertisement
Registered users don’t see ads! Register now!
Avatar
FelixGriffin
2,680 Posts
Posted Jul 08, 2013
Replied 3 hours later
You've mistyped the path to the script, it should be scripts/vscripts/whatever.nut.
Avatar
Tmast98
210 Posts
Posted Jul 08, 2013
Replied 15 minutes later
Okay, I fixed that, but I am getting this as well now

"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?

Avatar
ChickenMobile
2,460 Posts
Posted Jul 09, 2013
Replied 1 hour later
What is metal_boy?

baboo.SetModel("models/props/metal_boy.mdl");

That may be a problem if it isn't a custom prop.

Avatar
Tmast98
210 Posts
Posted Jul 09, 2013
Replied 14 minutes later

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.

Avatar
Gemarakup
1,183 Posts
Posted Jul 09, 2013
Replied 5 minutes later
Oh, did you check your model in the model viewer?
Avatar
Tmast98
210 Posts
Posted Jul 09, 2013
Replied 1 minutes later
Yeah, the model is there and fully works. It works on it's own as a prop_physics, but I need the cube to respawn using droppers, and prop_physics don't have dissolve inputs, nor does manually just putting it in work, so the best way I figured would just script the cubes to reskin themselves.
Avatar
ChickenMobile
2,460 Posts
Posted Jul 09, 2013
Replied 11 minutes later
Is IsValid() even a Portal 2 function? If you're trying to check if baboo is set to your cube you should compare it to null

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

Avatar
Tmast98
210 Posts
Posted Jul 09, 2013
Replied 8 minutes later
Yeah its valid, its a Portal 2 function. I'm new to scripting so the rest of that just went over my head. I think I am wanting baboo to be set to the cube? So how do I compare it to null?

Sorry for not understanding that fully :/.

Avatar
Tmast98
210 Posts
Posted Jul 09, 2013
Replied 6 minutes later
Oh I think I get it, since Test_Cube is not a global entity, but a name of a specific entity, I have to declare this differently? Now how do I go about doing this?

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(&quot;models/props/metal_boy.mdl&quot;);
            }

    }
    didSetCubes &lt;- true;

}

setCubes();
printl("If you're seeing this, the valentine script loaded without a hitch. Yay" + UniqueString() );

Avatar
ChickenMobile
2,460 Posts
Posted Jul 09, 2013
Replied 38 minutes later
I got the following to work:

didSetCubes &lt;- false;
DBG &lt;- 1 //set to 0 once finished debugging

//Change all the models of the cubes to custom ones
function setCubes() {&nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp;local baboo = null

&nbsp; &nbsp;//retrieve all cubes with the name Test_Cube and set their model to metal_boy
&nbsp; &nbsp;while((baboo = Entities.FindByName(baboo,&quot;Test_Cube&quot;)) != null){
&nbsp; &nbsp;&nbsp; &nbsp;if(DBG)printl(&quot;found cube!&quot;)
&nbsp; &nbsp;&nbsp; &nbsp;if ( baboo.IsValid() ) baboo.SetModel(&quot;models/props/metal_boy.mdl&quot;)}
&nbsp; &nbsp;}
&nbsp; &nbsp;didSetCubes = true;
}

setCubes() //Set all cubes models on level start
if(DBG)printl(&quot;setted the cubes! &quot; + UniqueString())

I will add IsValid to the list of Portal 2 functions on the wiki.

Avatar
Tmast98
210 Posts
Posted Jul 09, 2013
Replied 10 minutes later
Ill check if this setup works for me. Thanks for the help, I really appreciate it.
Avatar
Tmast98
210 Posts
Posted Jul 09, 2013
Replied 5 minutes later
Hey,

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?

Avatar
ChickenMobile
2,460 Posts
Posted Jul 09, 2013
Replied 10 minutes later
You do not even need to RunScriptCode as it will auto run because of setCubes() at the end of the code. To test out whether a script is working manually is to type in the console: ent_fire bignet runscriptfile o. It will tell you if there is any errors and where in the code or if the script you are referencing actually doesn't exist.

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 //||

Avatar
Tmast98
210 Posts
Posted Jul 09, 2013
Replied 1 minutes later
No it's not, that's what I can't figure out. It is scripts/vscripts/cubeswitch.nut

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++

Avatar
ChickenMobile
2,460 Posts
Posted Jul 09, 2013
Replied 4 minutes later

.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.jpg

Click 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.jpg

Lastly: 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){
&nbsp; &nbsp;cube.SetModel(&quot;models/props/metal_boy.mdl&quot;)
}


So the final code would look something like this:

function setCubeModel(cube){
&nbsp; &nbsp;cube.SetModel(&quot;models/props/metal_boy.mdl&quot;)
}

function setCubes() {&nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp;local baboo = null

&nbsp; &nbsp;while((baboo = Entities.FindByName(baboo,&quot;Test_Cube&quot;)) != null){
&nbsp; &nbsp;&nbsp; &nbsp;if ( baboo.IsValid() ) setCubeModel(baboo)
&nbsp; &nbsp;}
}

setCubes()
printl(&quot;setcubes &quot; + UniqueString());
Images 2
Post image 1
Post image 2
Avatar
Tmast98
210 Posts
Posted Jul 09, 2013
Replied 12 minutes later
Hey, here is everything on the comp, I can't get this thing working, maybe looking at it will help you see what is wrong.
Avatar
Tmast98
210 Posts
Posted Jul 09, 2013
Replied 4 minutes later
Oh, also there is only one file, and when I hit open source, it does not open it. How do I change it to suit? Under the manage list it is currently under scripts/vscripts/cubeswitch.nut
Avatar
ChickenMobile
2,460 Posts
Posted Jul 09, 2013
Replied 9 minutes later
I have a feeling that your windows is being decieving and saving the file as cubeswitch.nut.txt. Enable file extensions on windows and remove the .txt from the end of your file.

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

Advertisement
Registered users don’t see ads! Register now!
Avatar
josepezdj
2,386 Posts
Posted Jul 09, 2013
Replied 4 minutes later
@Tmast98: when you are going to save the file in Notepad++ just select "All Types(.*)"*:

img