Change the entity's velocity dynamically

I have made an entity, actually a box, which has a certain velocity. Then, I wish I could set its velocity during the game, by some pop up window.

I have already made a vgui script in lua\autorun\client. But the question is how to transport the data get from this vgui to the entity, which is stored in lua\entities<entity name>\init.lua?

Thank you very much!

you would need to make a global ( i duno the word, but the thing that says its what it is, like ply is player) name for the entity then do something like yourentity:SetVelocity(tonumber(mahtextbox))

Now what I am doing is save the data get by vgui to a file, and retrieve it in the entity defining ‘init.lua’ file. Do you mean that set the number get by vgui to be global? Is there any way to set it global?

Thanks very much!

Velocity is on the server, vgui is on the client. You need to get from client to server.

Make the owner execute a console command, which is client to server communication, with the argument being the text field. Inside the console command, make the line

[lua]varname = args[1][/lua]

This will make a global variable, because there is no local. Finally, you can use varname as a variable inside the entity.

Note that this method has a ton of security holes.

Your entity exists on the server and your VGUI on the client so you first need to send the data from your VGUI to the server.
The best way would be a console command which sends the new velocity, the function attached to it on the server would then change your entity’s velocity.

As an example when creating your entity use a unique name and don’t put local in front of it (If there are several of your entity put them in a table instead). That way you can access it from anywhere on the server’s code.

Then comes the console command :
[lua]local function SetMyEntVelocity(ply,cmd,args)
if not ply:IsAdmin() then return end – Remove this line if this is intended for use by all players

MyEntity:SetVelocity(Vector(args[1],args[2],args[3])) -- This is assuming you called your entity MyEntity and that you sent 3 integers representing a vector in your console command

end
concommand.Add(“setmyentvelocity”,SetMyEntVelocity)[/lua]

On the client you would do this :
[lua]local NewVel = Vector(54,-22,11111)-- Create your new velocity at some point
RunConsoleCommand(“setmyentvelocity”,NewVel .x,NewVel .y,NewVel .z)-- And send it[/lua]

Of course this is just an example, don’t expect it to work right out of the chatbox.