public static T Call <T>(this Godot.Object gdobj, Godot.Object caller,
                             string methodName, params object[] args)
    {
        T   response = default(T);
        int length   = args != null ? args.Length : 0;

        object[] newArgs = new object[length + 1];
        System.Array.Copy(args, 0, newArgs, 0, length);
        Optional optional = new Optional();

        newArgs[length] = optional;
        caller.Call(methodName, newArgs);

        if (!optional.IsDataReceived())
        {
            if (OS.IsDebugBuild())
            {
                StringBuilder msg = new StringBuilder(methodName);
                msg.Append("() called by ");
                msg.Append(caller.GetType().ToString()).Append(" from ");
                msg.Append(gdobj.GetType().ToString());
                msg.Append(" could not return a value!");
                GD.PushWarning(msg.ToString());
            }
        }
        else
        {
            response = optional.Get <T>();
        }

        // This fix abusive usage of Game.Objects causing a lot of memory leaks.
        optional.Free();
        return(response);
    }
    public void Move(string direction)
    {
        var vectorPosition = inputs[direction] * gridSize;

        rayCast2D.CastTo = vectorPosition;
        rayCast2D.ForceRaycastUpdate();
        InterpolateMove(direction, vectorPosition);
        if (!rayCast2D.IsColliding())
        {
            GetNode <Tween>("Tween").Start();
            main.moves += 1;
        }
        else
        {
            Godot.Object collider = rayCast2D.GetCollider();
            if (collider.GetType().ToString() == "Box")
            {
                Box boxCollider = collider as Box;
                if (boxCollider.IsInGroup("boxes"))
                {
                    if (boxCollider.Move(direction))
                    {
                        GetNode <Tween>("Tween").Start();
                        main.moves += 1;
                    }
                }
            }
        }
    }
Beispiel #3
0
        /// <summary>
        /// Run tools on the given object.
        /// </summary>
        /// <remarks>
        /// This will register signals as per <see cref="SignalAttribute"/>.
        /// </remarks>
        /// <param name="obj">The object.</param>
        public static void SetupObjectTools(this Godot.Object obj)
        {
            var type = obj.GetType();

            foreach (var nested in type.GetNestedTypes(BindingFlags.Public | BindingFlags.NonPublic))
            {
                foreach (var attr in nested.GetCustomAttributes())
                {
                    switch (attr)
                    {
                    case SignalAttribute signalAttr:
                        DefineSignalFromType(obj, nested, signalAttr);
                        break;
                    }
                }
            }
        }
Beispiel #4
0
        public void Run()
        {
            // For now rely on asking Godot if the instance is valid (there doesn't seem to be easy access to the flag
            // on the object to see if the disposed flag is set, though we could call something like GetInstanceId and
            // see if that throws disposed exception)
            if (!IsInstanceValid(objectToCheck))
            {
                if (logFailure)
                {
                    GD.Print("Object of type ", objectToCheck.GetType().FullName,
                             " was already disposed before invoke");
                }

                return;
            }

            underlyingAction.Invoke();
        }
Beispiel #5
0
    private bool _check_is_grounded()
    {
        foreach (RayCast2D raycast in Raycasts.GetChildren())
        {
            if (raycast.IsColliding())
            {
                Godot.Object myCollider = raycast.GetCollider();
                if (myCollider.GetType() != typeof(Godot.TileMap))
                {//GD.Print("Ray collided with: "+ myCollider.GetType());
                 //Only kill if we haven't been hit before
                    if (hitstun == 0)
                    {
                        myCollider.Set("DEAD", true);
                    }
                }



                return(true);
            }
        }
        return(false);
    }
Beispiel #6
0
        public void ProcessScript(string Command)
        {
            List <string> CommandParts;
            string        Arguments     = null;
            string        TargetCommand = default;

            if (Command.Contains('(') && Command.Contains(')') && Command[Command.Length - 1] == ')')
            {
                for (int i = 0; i < Command.Length - 1; ++i)
                {
                    if (Command[i] == '(')
                    {
                        TargetCommand = Command.Substring(0, i);

                        if (i + 1 != Command.Length - 1)
                        {
                            Arguments = Command.Substring(i + 1, Command.Length - (i + 2));
                        }

                        break;
                    }
                }

                // Check for command bind
                string ResultCommand = ProcessCommandBinding(TargetCommand);

                if (ResultCommand != null)
                {
                    TargetCommand = ResultCommand;
                }

                CommandParts = TargetCommand.Split('.').ToList();

                if (CommandParts.Count != 5)
                {
                    ConsoleSys.Error("Invalid command syntax");
                    ConsoleSys.Error("Have you tried using \"Help()\"?");
                    return;
                }

                if (TargetCommand != "Unary.Common.Shared.ConsoleSys.Message" &&
                    TargetCommand != "Unary.Common.Shared.ConsoleSys.Warning" &&
                    TargetCommand != "Unary.Common.Shared.ConsoleSys.Error" &&
                    TargetCommand != "Unary.Common.Shared.ConsoleSys.Panic")
                {
                    ConsoleSys.Message("> " + Command);
                }
            }
            else
            {
                ConsoleSys.Error("Invalid command syntax");
                ConsoleSys.Error("Have you tried using \"Help()\"?");
                return;
            }

            string ModID = ModIDUtil.ModID(TargetCommand);

            if (!RegisteredCommands.ContainsKey(ModID))
            {
                ConsoleSys.Error(ModID + " is an invalid ModID");
                ConsoleSys.Error(GetModIDs());
                return;
            }

            List <string> Systems;

            if (CommandParts[2] == "Shared")
            {
                Systems = Sys.Ref.Shared.Order;
            }
            else if (CommandParts[2] == "Client")
            {
                Systems = Sys.Ref.Client.Order;
            }
            else if (CommandParts[2] == "Server")
            {
                Systems = Sys.Ref.Server.Order;
            }
            else
            {
                ConsoleSys.Error(CommandParts[2] + " is an invalid System type");
                ConsoleSys.Error(GetSystemTypes());
                return;
            }

            string  ModIDEntry = TargetCommand.GetPartsFromBeginToIndex('.', 3);
            SysType Type;

            if (!Systems.Contains(ModIDEntry))
            {
                ConsoleSys.Error(CommandParts[2] + " is an invalid System ModIDEntry");
                ConsoleSys.Error(GetSystems(CommandParts[2], Systems));
                return;
            }

            Godot.Object TargetSystem = default;
            SysManager   TargetManager;

            if (CommandParts[2] == "Shared")
            {
                TargetManager = Sys.Ref.Shared;
            }
            else if (CommandParts[2] == "Client")
            {
                TargetManager = Sys.Ref.Client;
            }
            else
            {
                TargetManager = Sys.Ref.Server;
            }

            Type = TargetManager.GetType(ModIDEntry);

            switch (Type)
            {
            case SysType.Object:
                TargetSystem = TargetManager.GetObject(ModIDEntry);
                break;

            case SysType.Node:
                TargetSystem = TargetManager.GetNode(ModIDEntry);
                break;

            case SysType.UI:
                TargetSystem = TargetManager.GetUI(ModIDEntry);
                break;
            }

            Type TargetType = TargetSystem.GetType();

            MethodInfo[] Methods = TargetType.GetMethods().Where(m => m.DeclaringType == TargetType).ToArray();

            MethodInfo TargetMethod = null;

            foreach (var Method in Methods)
            {
                if (Method.Name == CommandParts[4])
                {
                    TargetMethod = Method;
                    break;
                }
            }

            if (TargetMethod == null)
            {
                ConsoleSys.Error(CommandParts[4] + " is an invalid method");
                ConsoleSys.Error(GetMethods(Methods));
                return;
            }

            ParameterInfo[] ArgumentsInfo  = TargetMethod.GetParameters();
            string          ArgumentValues = '[' + Arguments + ']';
            JArray          Array;

            try
            {
                Array = JArray.Parse(ArgumentValues);
            }
            catch (Exception)
            {
                ConsoleSys.Error(Arguments + " is an invalid set of arguments");
                ConsoleSys.Error(GetArguments(ArgumentsInfo));
                return;
            }

            if (Array.Count != ArgumentsInfo.Length)
            {
                ConsoleSys.Error("Invalid ammount of arguments");
                ConsoleSys.Error(GetArguments(ArgumentsInfo));
                return;
            }

            List <object> FinalArguments = new List <object>();

            for (int i = 0; i < Array.Count; ++i)
            {
                try
                {
                    object Argument = JsonConvert.DeserializeObject(Array[i].ToString(Formatting.None), ArgumentsInfo[i].ParameterType);
                    FinalArguments.Add(Argument);
                }
                catch (Exception)
                {
                    ConsoleSys.Error(Array[i].ToString() + " is not matching an argument type of " + ArgumentsInfo[i].ParameterType);
                    ConsoleSys.Error(GetArguments(ArgumentsInfo));
                    return;
                }
            }

            object CallResult = TargetSystem.Call(CommandParts[4], FinalArguments.ToArray());

            if (CallResult != null)
            {
                ConsoleSys.Message(CallResult.ToString());
            }
        }