Suggestions how best to do this...

There are no stupid questions, just stupid answers.
27 posts Page 1 of 3
flysouth
Posts: 105
Joined: Sun May 15, 2016 10:16 am
by flysouth » Tue Aug 16, 2016 12:10 am
Hi

I want to detect if the player gets near to an object that they can interact with, I have done some keyboard driven tests using

Code: Select all

%result = %control.getLookAtPoint(%range,$TypeMasks::StaticShapeObjectType); %obj = getWord( %result, 0); %Message = "Looking at: " @ %obj.getName(); commandToClient(%client, 'ActionMessage', %Message);
and it works fine.
I now want to set a schedule to do this every 250ms for each player.
I see how the scheduling is done but if I wanted this to happen for all players on a multiplayer system, what would the best way be to set up the schedules?
Would this also work for single player games with client and server on same machine?
Johxz
Posts: 444
Joined: Sat Feb 07, 2015 11:37 pm
by Johxz » Tue Aug 16, 2016 4:53 am
hmmm I not tested on MP maybe you can do something like this

//file: game/scripts/server/player.cs
function PlayerData::onAdd(%this, %obj)
{
   // Vehicle timeout
   %obj.mountVehicle = true;

   // Default dynamic armor stats
   %obj.setRechargeRate(%this.rechargeRate);
   %obj.setRepairRate(0);
   
   %obj.schedule(250, "scheduledUpdate");  // <<=========== HERE
}
I not sure if is better to use ContainerRayCast to detect if the player is near to an object and check if the object is a staticshape, or if the object have a dynamicfield with getFieldValue("SOME_NAME") or something like that...
flysouth
Posts: 105
Joined: Sun May 15, 2016 10:16 am
by flysouth » Tue Aug 16, 2016 3:37 pm
Thanks for the reply. I have now run into a problem with setting the schedule. I can get it all working if I put the schedule in command.cs but I think it must be assigned to the player. So in PlayerData::onAdd I call my schedule function for the first time. But when I re-call the schedule from within the function it gives a message of scheduledLook: Unknown command. This error is coming from line 6 below....
This is the function:
function Player::scheduledLook(%this)
{
	echo("schedule called");
	CommandToServer('ActionLook');
	cancel($ActShedId);
	$ActShedId = schedule(250,%this, "scheduledLook");
}
okay I have got it to work by changing the function to this:
function Player::scheduledLook(%this)
{
	echo("schedule called");
	CommandToServer('ActionLook');
	cancel($ActShedId);
	$ActShedId = %this.schedule(250, "scheduledLook");
}
According to my understanding of the wiki they should both work....
http://wiki.torque3d.org/scripter:schedules-and-timers
irei1as
Posts: 84
Joined: Fri Feb 27, 2015 5:13 pm
by irei1as » Wed Aug 17, 2016 10:49 am
The first doesn't work because the function is defined as Player::scheduledLook(%this). That means it's a method (thing) of the Player class (namespace thing). So in order to be called you need a player (%this is the player in your working code so %player.scheduledLook() is called after the 250 ms using that %this.schedule(...)).
For it to work in the first way (plain schedule without player) it should be defined as
function scheduledLook() { ... }
as a global function.
flysouth
Posts: 105
Joined: Sun May 15, 2016 10:16 am
by flysouth » Wed Aug 17, 2016 11:17 am
For it to work in the first way (plain schedule without player) it should be defined as
function scheduledLook() { ... }
as a global function.
Why? The first method passes %this to the scheduling code. Surely the intention of the second parameter is to pas the object to the scheduling code so that it can call the method? If this is not the case then why do we pass that object?
If you look at the link provided, I used it the same way they did in their example.
irei1as
Posts: 84
Joined: Fri Feb 27, 2015 5:13 pm
by irei1as » Wed Aug 17, 2016 12:12 pm
Ah, there is a small difference on how both schedule work. They're not the same.
EDIT: Hmm, I think that reference is wrong.

In short:
···schedule(250,object,"aaa") calls function aaa() as long as object exists. If object is deleted then the schedule stops. It's used just for that condition.
···object.schedule(250,"aaa") calls function object.aaa(%this) if object exists. The function has access to object using %this.

They call different functions.

If you want to test, try to create both functions and use the different types of schedules...
Copy-paste this in the console:

Code: Select all

function Player::hello(%this) { echo("This is with the Player.schedule(250,\"hello\")"); } function hello() { echo("This is with the alone schedule(250,Player,\"hello\")"); }
And then use:
(LocalClientConnection.player).schedule(250,"hello");
and
schedule(250,LocalClientConnection.player,"hello")
flysouth
Posts: 105
Joined: Sun May 15, 2016 10:16 am
by flysouth » Wed Aug 17, 2016 1:21 pm
Thanks for the example but....
If I change schedule(250,LocalClientConnection.player,"hello") to schedule(250,0,"hello") it still works. This seems to indicate that for the stand alone function it does not care about that object. From this I take it that the object was intended for use when the function was a method of an object such as in the example at the link provided above???
chriscalef
Posts: 398
Joined: Mon Feb 09, 2015 7:48 pm
by chriscalef » Thu Aug 18, 2016 6:48 pm
Just passing through here, so I may have missed something, but are you aware that PlayerData and Player are not the same object? PlayerData is the datablock, Player is the player object. Just a thought.
flysouth
Posts: 105
Joined: Sun May 15, 2016 10:16 am
by flysouth » Fri Aug 19, 2016 10:19 am
To my noob understanding of TS, in this case %this holds the reference to player in both examples.... or perhaps I am missing something here?
Azaezel
DEVGRU
Posts: 496
Joined: Tue Feb 03, 2015 9:50 pm
 
by Azaezel » Fri Aug 19, 2016 6:38 pm
%this is (as noted, a rule not always followed by the example code) the nomenclature used for the first entry in a callback, typically referencing the attached simobject's ID. for datablocks that means the datablock, for object instances, that means the particular instance. Why you'll see quite a few callbacks with somethinseomthingBlock(%this,%obj){dostuff}. formers the shared data, latter is the per-object data.

Our end what we do is tack on triggers programatically so if nobody is around an interactive object we save some load, but if you wanna iterate connections:

Code: Select all

function myfunc() { if(isObject(ClientGroup)) //are we using the default sample client connection group? { %clientCount = ClientGroup.getCount(); //how many for (%clientIndex = 0; %clientIndex < %clientCount; %clientIndex++) { %client = ClientGroup.getObject(%clientIndex);//get the id from the vector-offset %obj = %client.player; //get the player body reference set in the (stock sample) camecore.cs * //(insert castray codez here) } }}
*https://github.com/GarageGames/Torque3D ... re.cs#L939

You could also start a schedule in a player::onAdd, that reschedules itsself:

Code: Select all

function PlayerData::onAdd(%this, %obj) { %milliseconds = 1000; %somevar = true; %obj.schedule(%milliseconds,"myMethod",%somevar); //note here it's obj } function Player::myMethod(%this,%passedvar) { %milliseconds = 1000; %somevar = %passedvar; %this.schedule(%milliseconds,"myMethod",%somevar); //and here it's %this }
27 posts Page 1 of 3

Who is online

Users browsing this forum: No registered users and 3 guests