Jump to content
Sign in to follow this  
chaoticgood

Scripting for multiplayer problems

Recommended Posts

Hello, I have 3 problems that I am unable to solve.

1. My script runs fine on my client but when a friend joins the listen server after it's started, the script does not execute for him.

2. When I go back to the lobby and choose a different slot, the newly spawned character does not run the script.

3. When I go back to the lobby and choose a different slot, an AI is left behind in the old slot even though AI is disabled in the description.ext.

so here it is: init.sqf

execVM "functions.sqf";
sleep 1;
execVM "Script.sqf";

functions.sqf

playerInit = 
{
_this setPos [3757.36,7958.89,0];
removeAllWeapons _this;
removeVest _this;
removeHeadgear _this;
_this addMagazine "16Rnd_9x21_Mag";
_this addMagazine "16Rnd_9x21_Mag";
_this addWeapon "hgun_Rook40_F";
_teamColour = assignedTeam _this;
hint format ["Team Colour: %1",_teamColour];
_this addAction ["Spawn Dialog Test",{createDialog "SpawnTeleport"}];
};

script.sqf

_p = player;
_p call playerInit;
_p addMPEventHandler ["MPRespawn",{(_this select 0) call playerInit;}];

So what it's supposed to do is: the player spawns in, he gets "initialised" by the function, then the eventhandler gets added to him so if he respawns, it will "initialise" him again.

Edited by chaoticgood

Share this post


Link to post
Share on other sites

I assume that #1 is about JIP. Add this to the top of the init:

if (!isServer) then {waitUntil {!(isNull player)};};

I do not know what is the server is doing when you change player slots mid-mission; you may or may not be running the init.sqf again (seems like not). I would recommend restarting the mission to test again. If you want to switch between units put 'respawn=5' in your description.ext. Also, you can just define the function in the init instead of calling a script to define the function. You could even #include "functions.sqf" if you want.

Share this post


Link to post
Share on other sites

Thanks for the help. I added the line and it still is not working as intended. I assume the line you told me to add makes sure the function and script only run on the client and NOT the server.

The respawn type is set to BASE and I've set that up in the editor so it works correctly. The problem comes when people JIP, the script doesn't run for them as far as I can tell. Any advice on how to do so would be appreciated. Thanks again.

Share this post


Link to post
Share on other sites

--Im not good at scripting but from the things I read--

Init.sqf

T_INIT = false;
T_Server = false;
T_Client = false;
T_JP = false;
if (isServer) then //Stuff that is only for dedicated server
{
 T_Server = true;
 if (!(isNull player)) then { T_Client = true }; //stuff for ingame Server
 T_INIT = true;
} else {
 T_Client = true;
 if (isNull player) then //stuff for client
 {
     T_JIP = true;
     [] spawn { waitUntil { !(isNull player) }; T_INIT = true };
 } else {
     T_INIT = true;
 };
};
if(T_JP == true)then{execVM "myscript";};

my source: http://community.bistudio.com/wiki/6thSense.eu:EG#Determining_if_machine_is_Ingame_Server.2C_Ded_Server.2C_Player_or_JIP_Player

might be wrong but that should work, I did not try it myself.

Share this post


Link to post
Share on other sites

I replaced my init.sqf with the one you gave me Beigen. However it gave me an error on the last line:

if(T_JP |#|== true)then{execVM "myscript";}; 

Error ==:type Bool, Expected Number, String, Object, ect.

I also replaced "myscript" with my own funcions.sqf and script.sqf

EDIT: could the problem be that I'm using a listen server and not a dedicated?

Edited by chaoticgood

Share this post


Link to post
Share on other sites
Error ==:type Bool, Expected Number, String, Object, ect.

This variable is bool.

if (T_JP) then {..};

this is, if variable is positive, also true

if (!T_JP) then {...};

this is, if variable is negative, also false

You can use '==' only for example by object, string and etc...

Share this post


Link to post
Share on other sites

Ok, Beigen's script works with JTS_2009's fix applied to it.

Can someone walk me through as to why Beigens script works? My script is ran on JIP players now whereas before it did not.

Share this post


Link to post
Share on other sites
Can someone walk me through as to why Beigens script works? My script is ran on JIP players now whereas before it did not.

These are the lines in his script that will execute only for JIP players:

T_JIP = true;
[] spawn { waitUntil { !(isNull player) }; T_INIT = true };

He is using the same method as I posted, except opening a new thread and using global variables to sync. The second line forces the client to wait until enough data has been exchanged with the server to initialize the player. However, the script never uses 'T_INIT', and it defines a variable 'T_JIP' that is never used ('T_JP' is used, but never altered by the if block). It never uses T_Client or T_Server either.

You do not need variables to determine who is client and server because you would just use 'isServer'. 'T_INIT' would always be true if you put the waitUntil loop as the first line of your init. Unless you have some special code for JIP players, they will execute the init exactly like other clients after they initialize. There is also 'isMultiplayer' and 'isDedicated'. I suggest that you think about what you need to know and check for only that information.

Unless you have modified the code beyond JTS_2009's fix, I am surprised that it works. There is a lot that is unnecessary and does not quite make sense. If you want a code review and explanation, please post your entire working init. Nevertheless, I am glad it works for you.

Share this post


Link to post
Share on other sites

Alright! Thanks for the help everyone, my script now works as intended.

I changed my init.sqf to look like this:

waitUntil{!(isNull player)};
execVM "functions.sqf";
sleep 0.1;
execVM "Script.sqf";

I thought it was odd that your first script you gave me zenophon, did not work, so I tried it with just part of the line. Now the script runs on players and JIP players which is wonderful!

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×